Iamtui1010

chiakeo.cpp

Sep 29th, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<climits>
  5.  
  6. #define long long long
  7. #define nln '\n'
  8.  
  9. const long INF = LLONG_MAX;
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     // Input
  16.     cin.tie(0)->sync_with_stdio(0);
  17.     cout.tie(0)->sync_with_stdio(0);
  18.     //freopen("chiakeo.inp", "r", stdin);
  19.     long T = 1;
  20.     cin >> T;
  21.     while (T--){
  22.         long n;
  23.         cin >> n;
  24.         vector<long> a(n);
  25.         long sum = 0;
  26.         for (auto &i : a){
  27.             cin >> i;
  28.             sum += i;
  29.         }
  30.         // Dynamic planning
  31.         vector<long> sav(sum/2+1, INF);
  32.         sav[0] = -1;
  33.         for (long i = 1; i <= sum/2; ++i){
  34.             for (long j = 0; j < n; ++j)
  35.                 if (i >= a[j] && sav[i-a[j]] < j){
  36.                     sav[i] = j;
  37.                     break;
  38.                 }
  39.         }
  40.         // Trace
  41.         vector<bool> tic(n+1, 0);
  42.         long res = 0;
  43.         long m = sum/2;
  44.         while (m > 0){
  45.             if (sav[m] == INF){
  46.                 --m;
  47.                 continue;
  48.             }
  49.             tic[sav[m]] = 1;
  50.             res += a[sav[m]];
  51.             m -= a[sav[m]];
  52.         }
  53.         // Output
  54.         cout << sum-2*res << nln;  
  55.         /*for (long i = 0; i < n; ++i)
  56.             if (!tic[i])
  57.                 cout << a[i] << ' ';
  58.         cout << nln;
  59.         for (long i = 0; i < n; ++i)
  60.             if (tic[i])
  61.                 cout << a[i] << ' ';*/
  62.     }
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment