codechef AMMEAT - "Andrew and the Meatballs" editorial : http://discuss.codechef.com/questions/8565/ammeat-editorial
codechef AMMEAT - "Andrew and the Meatballs" solution : http://ideone.com/hg9cK1
#include <iostream>#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
int t, n, cnt, flag;
long long m, p[10], s;
scanf("%d", &t);
while(t--) {
memset(p, 0, sizeof(p));
cnt=0;
s=0;
flag=0;
scanf("%d%lld", &n, &m);
for(int i=0; i<n; i++) scanf("%lld", &p[i]);
sort(p, p+n);
for(int i=n-1; i>=0; i--) {
s+=p[i];
cnt++;
if(s>=m) {
flag=1;
break;
}
}
if(flag) printf("%d\n", cnt);
else printf("-1\n");
}
return 0;
}
note: use sort() from <algorithm>, qsort() of <cstdlib> may give error, it was buggy for me when i wanted to sort long long. sort(): http://www.cplusplus.com/reference/algorithm/sort/
No comments:
Post a Comment