Skip to content

Commit

Permalink
cf2042A Greedy Monocarp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicfred committed Dec 9, 2024
1 parent dc163fd commit 9434d87
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions codeforces/cf2042a_greedy_monocarp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Vicfred
// https://codeforces.com/contest/2042/problem/A
// greedy
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(begin(a), end(a), greater<long long>());
int cummulative = 0LL;
bool found = false;
for (int i = 0; i < n; ++i) {
cummulative += a[i];
if (cummulative == k) {
found = true;
} else if (cummulative > k) {
cummulative -= a[i];
break;
}
}
if (found) {
cout << 0 << endl;
} else {
cout << k - cummulative << endl;
}
}
return 0;
}

0 comments on commit 9434d87

Please sign in to comment.