mhdew

TImus 1005

Apr 3rd, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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. int idx=0;
  7. vector<int> v,d;
  8. bool flag[100];
  9.  
  10. void bt(int pos)
  11. {
  12.     if(x>=(sum/2)){
  13. //        for(int i=0;i<v.size();i++)
  14. //            cout<<v[i]<<" ";
  15. //        cout<<endl;
  16.         y=sum-x;
  17.         dif=abs(x-y);
  18.         if(dif<=mn){
  19.             d.push_back(dif);
  20.             mn=dif;
  21. //            printf("x=%lld y=%lld %d\n", x,y,d[idx]);
  22.             idx++;
  23.         }
  24.         return;
  25.     }
  26.  
  27.     for(int i=0;i<n;i++){
  28.         if(flag[a[i]]==0){
  29.             flag[a[i]]=1;
  30.             v.push_back(a[i]);
  31.             x+=a[i];
  32.             bt(pos+1);
  33.             x-=a[i];
  34.             v.pop_back();
  35.             flag[a[i]]=0;
  36.         }
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42. //    freopen("input.txt", "r", stdin);
  43. //    freopen("output.txt", "w", stdout);
  44.     cin>>n;
  45.     for(int i=0;i<n;i++){
  46.         cin>>a[i];
  47.         sum+=a[i];
  48.     }
  49.     mn=(sum/2);
  50. //    cout<<"sum= "<<sum;
  51. //    cout<<"  Half= "<<(sum/2);
  52. //    cout<<endl;
  53.     bt(0);
  54.     cout<<d[idx-1]<<endl;
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment