列表

详情


NC230838. The Witchwoods

描述

Shenyang's night fair culture is developed very well. Every time Bob comes to Shenyang, he will definitely go to a night fair called The Witchwood. There are n snack stalls in The Witchwood, the ith of which gives him a_i pleasure.

Bob's stomach allows him to eat k snack stalls at most. So Bob wants to know the maximum pleasure he can get after visiting the night market.

输入描述

The first line of input contains two integers n  and k , indicating the number of snack stalls and the capacity of Bob's stomach.

The second line of input contains n integers , the ith of which indicates the pleasure of the ith snack stall.

输出描述

Print one integer denoting the maximum pleasure Bob can get.

示例1

输入:

5 2
9 8 10 2 4

输出:

19

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 4ms, 内存消耗: 412K, 提交时间: 2021-11-20 16:21:54

#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,i,b[1001],k=0;
	cin>>t>>n;
	for(i=0;i<t;i++)
	cin>>b[i];
	sort(b,b+t);
	for(i=t-1;i>=t-n;i--)
	k+=b[i];
	cout<<k;
}

pypy3 解法, 执行用时: 141ms, 内存消耗: 25816K, 提交时间: 2021-11-20 16:03:03

n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
print(sum(a[:k]))

Python3 解法, 执行用时: 64ms, 内存消耗: 7068K, 提交时间: 2021-11-20 16:03:41

n, k = map(int, input().split())
b = map(int, input().split())
print(sum(sorted(b)[-k:]))

上一题