diff --git a/codeforces/cf2042a_greedy_monocarp.cpp b/codeforces/cf2042a_greedy_monocarp.cpp new file mode 100644 index 0000000..9e207f5 --- /dev/null +++ b/codeforces/cf2042a_greedy_monocarp.cpp @@ -0,0 +1,39 @@ +// Vicfred +// https://codeforces.com/contest/2042/problem/A +// greedy +#include +#include +#include + +using namespace std; + +int main() { + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + vector a(n); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + sort(begin(a), end(a), greater()); + 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; +}