Untitled

mail@pastecode.io avatar
unknown
c_cpp
11 days ago
407 B
0
Indexable
Never
#include <iostream>
#include <algorithm>

int main() {
	int n, m, count{},a;
	std::cin >> n >> m;
	int* treasures = new int[n];
	for (int i = 0; i < n; i++) {
		std::cin >> a;
		if (a > 0) {
			treasures[count++] = a;
		}
	}
	std::sort(treasures, treasures + count, std::greater<int>());
	n = 0;
	for (int i = 0; i < std::min(count,m); i++) {
		n += treasures[i];
	}
	std::cout << n;
}
	
Leave a Comment