fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  4. #define mofile(s) freopen(s,"r",stdin)
  5. #define outfile(s) freopen(s,"w",stdout)
  6. #define ll long long
  7. #define ii pair<ll,ll>
  8. #define iii pair<ll,ii>
  9. #define fi first
  10. #define se second
  11. #define tf bool
  12. #define ST stack
  13. #define Q deque
  14. #define Q queue
  15. #define S string
  16. #define Ma map
  17. #define UM unormideremid_map
  18. #define SE set
  19. #define str(x) to_string(x)
  20. #define all(a) (a).begin(),(a).end()
  21. #define FOR(i,l,r,mid) for(int i=l;i<=r;i+=mid)
  22. #define FOD(i,l,r,mid) for(int i=r;i>=l;i-=mid)
  23. #define xuong cout<<"\n"
  24. #define midebug(x) cout<<(x)<<" "
  25. #define ppcnt(x) __builtin_popcountll(x)
  26. #define parity(x) __builtin_parityll(x)
  27. #define leamid0(x) __builtin_clzll(x)
  28. #define LOG2 __lg(x)
  29. #define tr0(x) __builtin_ctzll(x)
  30. #define fiset(x) __builtin_ffsll(x)
  31. #define MASK(k) (1LL<<(k))
  32. #define BIT(x,k) ((x)>>(k)&1)
  33. #define pb push_back
  34. #define tron(x) setprecision(x)
  35. #define het return 0
  36. #define base_ 1000000000
  37. template<typename... T>
  38. void in(T&... args) { ((cin >> args), ...); }
  39. template<class X, class Y>
  40. bool maximize(X &x, const Y &y){return (x < y) ? x = y, 1 : 0;}
  41. template<class X, class Y>
  42. bool minimize(X &x, const Y &y){return (x > y) ? x = y, 1 : 0;}
  43. const int maxn=1e6+5;
  44. const ll tle=2e8;
  45. const ll INF=1e9+9;
  46. const int base=31;
  47. string bcc="abcmidefghijklmnopqrstuvwxyz";
  48. int midx[]={-1,0,1,0};
  49. int midy[]={0,1,0,-1};
  50. bool sang[10000005];
  51. ll pref[1005][1005],mt[1005][1005];
  52. void sieve(){
  53. for(int i=1;i<=10000000;++i) sang[i]=1;
  54. sang[0]=sang[1]=0;
  55. for(int i=2;i*i<=10000000;++i){
  56. if(sang[i]){
  57. for(int j=i*i;j<=10000000;j+=i) sang[j]=0;
  58. }
  59. }
  60. }
  61. void lis(){
  62. vector<int>t;
  63. vector<int>a;
  64. int n; cin>>n;
  65. for(int i=1;i<=n;++i){
  66. int ai; cin>>ai;
  67. a.pb(ai);
  68. }
  69. for(int x:a){
  70. auto it=lower_bound(all(t),x);
  71. if(it==t.end()) t.pb(x);
  72. else *it=x;
  73. }
  74. }
  75. void pfs2mid(){
  76. int n,m,k; cin>>n>>k; m=n;
  77. for(int i=1;i<=n;++i){
  78. for(int j=1;j<=m;++j) cin>>mt[i][j];
  79. }
  80. for(int i=1;i<=n;++i){
  81. for(int j=1;j<=m;++j) pref[i][j]=mt[i][j]+pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
  82. }
  83. }
  84. ll qu2mid(int x1,int y1,int x2,int y2){
  85. return pref[x2][y2]-pref[x1-1][y2]-pref[x2][y1-1]+pref[x1-1][y1-1];
  86. }
  87. void open(){
  88. if(fopen("mideptrai.INP","r")){
  89. mofile("mideptrai.INP");
  90. outfile("mideptrai.OUT");
  91. }
  92. }
  93. void gen(const vector<ll>&a,vector<ll>&s){
  94. int n=a.size();
  95. for(int i=0;i<(1<<n);++i){
  96. ll c=0;
  97. for(int j=0;j<n;++j){
  98. if((i>>j)&1) c+=a[j];
  99. }
  100. s.pb(c);
  101. }
  102. }
  103. int main(){
  104. ios_base::sync_with_stdio(0);cin.tie(0);
  105. int n; cin>>n;
  106. vector<ll>A(n);
  107. ll S=0;
  108. for(int i=0;i<n;++i){cin>>A[i];S+=A[i];}
  109. vector<ll>L,R;
  110. for(int i=0;i<n;++i){
  111. if(i<n/2) L.pb(A[i]);
  112. else R.pb(A[i]);
  113. }
  114. vector<ll>sL,sR;
  115. gen(L,sL); sort(sL.begin(),sL.end());
  116. gen(R,sR);
  117. ll ans=-1;
  118. for(ll x:sR){
  119. auto it=lower_bound(sL.begin(),sL.end(),(S/2.0)-x);
  120. if(it!=sL.end()){
  121. ll d=abs(S-2*(*it+x));
  122. if(ans==-1 || d<ans) ans=d;
  123. }
  124. if(it!=sL.begin()){
  125. ll d=abs(S-2*(*prev(it)+x));
  126. if(ans==-1 || d<ans)ans=d;
  127. }
  128. }
  129. cout<<ans<<"\n";
  130. het;
  131. }
Success #stdin #stdout 0s 5320KB
stdin
3
1 2 4
stdout
1