Alex_tz307

Grupe X - 6.6.2 / pag 172

Sep 22nd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int v_max = 5e5 + 5;
  6.  
  7. bitset < v_max > sums;
  8.  
  9. int main() {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(nullptr);
  12.     cout.tie(nullptr);
  13.     int N;
  14.     cin >> N;
  15.     vector < int > a(N);
  16.     int sum = 0;
  17.     for(int& x : a) {
  18.         cin >> x;
  19.         sum += x;
  20.     }
  21.     for(int x : a) {
  22.         for(int i = 1; i <= sum; ++i)
  23.             if(sums[i])
  24.                 sums[i + x] = true;
  25.         sums[x] = true;
  26.     }
  27.     sum /= 2;
  28.     int x = sum;
  29.     while(!sums[x] && x > 0)
  30.         --x;
  31.     int y = sum;
  32.     while(!sums[y])
  33.         ++y;
  34.     if(sum - x < y - sum)
  35.         cout << sum - x;
  36.     else
  37.         cout << y - sum;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment