Advertisement
BaoJIaoPisu

Untitled

Aug 9th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minele min_element
  11. #define maxele max_element
  12. #define lb lower_bound //first pos >= val
  13. #define ub upper_bound // first pos > val
  14. #define cnt_bit __builtin_popcount
  15. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  16. //#pragma GCC optimize("Ofast")
  17. //#pragma GCC target("avx,avx2,fma")
  18. using namespace std;
  19.  
  20. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  21.  
  22. typedef long long ll;
  23. typedef pair<ll, ll> pll;
  24. typedef pair<int, int> pii;
  25.  
  26.  
  27. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  28. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  29. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  30.  
  31. const ll oo = 1e18;
  32. const ll maxN = 1e6;
  33.  
  34. /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
  35.  
  36. void maximize(int &a, int b) {
  37.     a = max(a, b);
  38. }
  39.  
  40. void minimize(int &a, int b) {
  41.     a = min(a, b);
  42. }
  43.  
  44. ll ans = oo;
  45. int n;
  46. ll a[120];
  47.  
  48. void dp(int id, ll x, ll y, ll z) {
  49.     if(id > n) {
  50.         ans = min(ans, max({x, y, z}) - min({x, y, z}));
  51.         return;
  52.     }
  53.  
  54.     dp(id + 1, x + a[id], y, z);
  55.     dp(id + 1, x, y + a[id], z);
  56.     dp(id + 1, x, y, z + a[id]);
  57. }
  58.  
  59. ll candy[10];
  60. bool f[104][2010][2010];
  61. const int l = 2000;
  62. ll d[(1 << 20) + 10];
  63.  
  64. void solve() {
  65.     cin >> n;
  66.     ll sum = 0;
  67.     for(int i = 1; i <= n; i++) {
  68.         cin >> a[i];
  69.         sum += a[i];
  70.     }
  71.  
  72.     if(n <= 15) {
  73.         dp(1, 0, 0, 0);
  74.         cout << ans;
  75.     } else {
  76.         if(sum <= l) {
  77.             f[0][0][0] = true;
  78.             for(int i = 1; i <= n; i++) {
  79.                 for(int x = 0; x <= l; x++) {
  80.                     for(int y = 0; y <= l; y++) {
  81.                         f[i][x][y] = f[i - 1][x][y];
  82.                         if(f[i][x][y]) continue;
  83.                         if(x >= a[i]) {
  84.                             f[i][x][y] |= f[i - 1][x - a[i]][y];
  85.                         }
  86.  
  87.                         if(y >= a[i]) {
  88.                             f[i][x][y] |= f[i - 1][x][y - a[i]];
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             int ans = 1e9;
  95.             for(int x = 0; x <= l; x++) {
  96.                 for(int y = 0; y <= l; y++) {
  97.                     if(!f[n][x][y]) continue;
  98.                     int z = sum - x - y;
  99.                     ans = min(ans, max({x, y, z}) - min({x, y, z}));
  100.                 }
  101.             }
  102.  
  103.             cout << ans;
  104.         } else {
  105.             auto Mask = [&](int id) -> int {
  106.                 return (1 << (n - id));
  107.             };
  108.  
  109.             ll tot = 0;
  110.             for(int i = 1; i <= n; i++) tot = tot + a[i];
  111.  
  112.             for(int msk = 0; msk < (1 << n); msk++) {
  113.                 ll sum = 0;
  114.                 for(int i = 1; i <= n; i++) {
  115.                     if(msk & Mask(i)) {
  116.                         if(d[msk ^ Mask(i)] + a[i] <= tot / 3) {
  117.                             d[msk] = max(d[msk], d[msk ^ Mask(i)] + a[i]);
  118.                         } else {
  119.                             d[msk] = max(d[msk], d[msk ^ Mask(i)]);
  120.                         }
  121.  
  122.                         sum = sum + a[i];
  123.                     }
  124.                 }
  125.  
  126.  
  127.                 ll B = tot - sum;
  128.                 ll A = d[msk];
  129.                 ll C = sum - A;
  130.                 ans = min(ans, max({A, B, C}) - min({A, B, C}));
  131.             }
  132.  
  133.             cout << ans;
  134.         }
  135.     }
  136. }
  137.  
  138. int main()
  139. {
  140.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  141.     #ifndef ONLINE_JUDGE
  142.     freopen("input.txt", "r", stdin);
  143.     freopen("output.txt", "w", stdout);
  144.     #else
  145.     //online
  146.     #endif
  147.  
  148.     int tc = 1, ddd = 0;
  149.     // cin >> tc;
  150.     while(tc--) {
  151.         //ddd++;
  152.         //cout << "Case #" << ddd << ": ";
  153.         solve();
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement