NC230838. The Witchwoods
描述
输入描述
The first line of input contains two integers and , indicating the number of snack stalls and the capacity of Bob's stomach.
The second line of input contains integers , the th of which indicates the pleasure of the th 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:]))