mhdew

Timus 1005

Oct 24th, 2018
305
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. int a[100];
  5. long long sum=0, x=0, y,n,dif, mn;
  6. long long idx=0;
  7. vector<long long>d;
  8. bool flag[111111];
  9.  
  10. void bt(int pos)
  11. {
  12.     if(pos==n || x>=(sum/2)){
  13.         y=sum-x;
  14.         dif=abs(x-y);
  15.         if(dif<mn){
  16.             d.clear();
  17.             d.push_back(dif);
  18.             mn=dif;
  19.             idx++;
  20.         }
  21.         return;
  22.     }
  23.  
  24.     x+=a[pos];
  25.     bt(pos+1);
  26.     x-=a[pos];
  27.     bt(pos+1);
  28. }
  29.  
  30. int main()
  31. {
  32.    // freopen("input.txt", "r", stdin);
  33.     //freopen("output.txt", "w", stdout);
  34.     cin>>n;
  35.     for(int i=0;i<n;i++){
  36.         cin>>a[i];
  37.         sum+=a[i];
  38.     }
  39.     mn=sum+1;
  40.     bt(0);
  41.  
  42.     cout<<d[0]<<endl;
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment