fork download
  1. #include<stdio.h>
  2.  
  3. int subsetSum(int n, int selected, int k, int players, int sum, int a[])
  4. {
  5. if(sum==0 && selected==k) return 1;
  6. if(n>players || sum<0) return 0;
  7. return subsetSum(n+1,selected,k,players,sum,a) + subsetSum(n+1,selected+1,k,players,sum-a[n-1],a) ; // either select note or don't
  8. }
  9.  
  10. int main()
  11. {
  12. int i,j,T,K,Sum,a[11];
  13. scanf("%d",&T);
  14. while(T--)
  15. {
  16. Sum = 0;
  17. for(i=0; i<11; i++) scanf("%d",&a[i]);
  18. scanf("%d",&K);
  19. // Sort i.e find K maximum numbers
  20. for(i=0; i<K; i++)
  21. for(j=1; j<11-i; j++)
  22. if(a[j-1] > a[j])
  23. {
  24. int temp = a[j-1];
  25. a[j-1] = a[j];
  26. a[j] = temp;
  27. }
  28. for(i=0; i<K; i++) Sum += a[11-1-i];
  29. printf("%d\n",subsetSum(1, 0, K, 11, Sum, a) );
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 1792KB
stdin
2
1 2 3 4 5 6 7 8 9 10 11
3
2 5 1 2 4 1 6 5 2 2 1
6
stdout
1
6