fork download
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. const int N = 505;
  4. int t;
  5. int n;
  6. int arr[N];
  7. bitset < N * N > bit;
  8. int solve(vector < int > &v){
  9. int sum = 0;
  10. for(int x : v){
  11. sum += x;
  12. }
  13. bit.reset();
  14. bit.set(0);
  15. for(int x : v){
  16. bit |= (bit << x);
  17. }
  18. int ans = sum;
  19. for(int i = 0 ; i < sum ; ++i){
  20. if(bit.test(i)){
  21. ans = min(ans , abs(i - (sum - i)));
  22. }
  23. }
  24. return ans;
  25. }
  26. int main(){
  27. scanf("%d" , &t);
  28. while(t--){
  29. scanf("%d" , &n);
  30. char ty[3];
  31. for(int i = 0 ; i < n ; ++i){
  32. scanf("%d %s" , arr + i , ty);
  33. }
  34. scanf("%d" , &arr[n]);
  35. if(n <= 2){
  36. printf("NO\n");
  37. continue;
  38. }
  39. vector < int > v1;
  40. vector < int > v2;
  41. v1.clear();
  42. v2.clear();
  43. for(int i = 0 ; i <= n ; i += 2){
  44. v1.emplace_back(arr[i]);
  45. }
  46. for(int i = 1 ; i <= n ; i += 2){
  47. v2.emplace_back(arr[i]);
  48. }
  49. printf("%d\n" , solve(v1) + solve(v2));
  50. }
  51. }
Success #stdin #stdout 0s 3448KB
stdin
CodeChef submission 10549991 (C++14) plaintext list. Status: AC, problem RWALK, contest SNCKEL16. By win_snackdown (win_snackdown), 2016-06-19 21:49:34.
stdout
Standard output is empty