Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int a[100];
- long long sum=0, x=0, y,n,dif, mn;
- int idx=0;
- vector<int> v,d;
- bool flag[100];
- void bt(int pos)
- {
- if(x>=(sum/2)){
- // for(int i=0;i<v.size();i++)
- // cout<<v[i]<<" ";
- // cout<<endl;
- y=sum-x;
- dif=abs(x-y);
- if(dif<=mn){
- d.push_back(dif);
- mn=dif;
- // printf("x=%lld y=%lld %d\n", x,y,d[idx]);
- idx++;
- }
- return;
- }
- for(int i=0;i<n;i++){
- if(flag[a[i]]==0){
- flag[a[i]]=1;
- v.push_back(a[i]);
- x+=a[i];
- bt(pos+1);
- x-=a[i];
- v.pop_back();
- flag[a[i]]=0;
- }
- }
- }
- int main()
- {
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- cin>>n;
- for(int i=0;i<n;i++){
- cin>>a[i];
- sum+=a[i];
- }
- mn=(sum/2);
- // cout<<"sum= "<<sum;
- // cout<<" Half= "<<(sum/2);
- // cout<<endl;
- bt(0);
- cout<<d[idx-1]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment