fork(1) download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. int pick(int n, int *bucket, int dest, int lastIndex, int value, int toPick){
  4. if(toPick == 0) return value;
  5. int ans = 0;
  6. for(int t = lastIndex + 1, next;t < n - toPick + 1;t++){
  7. next = pick(n, bucket, dest, t, value + bucket[t], toPick - 1);
  8. if ((dest - next) * (dest - next) < (dest - ans)*(dest - ans)) ans = next;
  9. }
  10. return ans;
  11. }
  12.  
  13. int main(void) {
  14. int n, *input, total = 0;
  15. scanf("%d", &n);
  16. input = malloc(sizeof(int) * n);
  17. for(int t = 0;t<n;t++) {
  18. scanf("%d", &input[t]);
  19. total += input[t];
  20. }
  21. printf("%d", total - 2*pick(n, input, total / 2, -1, 0, n / 2));
  22. free(input);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 5320KB
stdin
6
10 20 30 40 30 20
stdout
10