My issue
Hi community, when my code is submitted, I always get a WA, but nothing seems to be wrong, even the few mistakes that the debugger gave me.
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n, greater<int>()); // to sort the array in descending order
int d=0, tot=0;
for (int i = 0; i < n; i++) {
tot += a[i];
if (tot >= m) {
d = i + 1;
break;
}
}
cout << ((d==0)?-1:d) << endl;
}
return 0;
}
Learning course: Sorting using C++
Problem Link: CodeChef: Practical coding for everyone