Sunday, January 19, 2014

codechef LELEMON - "little elephant and lemonade" solution

codechef LELEMON - "little elephant and lemonade" : http://www.codechef.com/problems/LELEMON

codechef LELEMON - "little elephant and lemonade" solution:  http://ideone.com/Mh6hGf

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int cmp(const void *a, const void *b) {
return (*(int*)a - *(int*)b);}

int main() {
int t, n, m, p[105], c[105], cnt, tot, a, b;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
tot=0;
memset(p, 0 ,sizeof(p));
for(int i=0; i<m; i++) { scanf("%d", &a); p[a]++; }
for(int i=0; i<n; i++) {
scanf("%d", &b); 
memset(c, 0, sizeof(c));
cnt=0;
for(int j=0; j<b; j++) scanf("%d", &c[j]);
qsort(c, b, sizeof(int), cmp);
for(int k=0; k<p[i]; k++) { cnt += c[b-1-k]; if((b-1-k)==0) break; }
tot += cnt;
}
printf("%d\n", tot);
}
return 0;
}

No comments:

Post a Comment