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;
- long long idx=0;
- vector<long long>d;
- bool flag[111111];
- void bt(int pos)
- {
- if(pos==n || x>=(sum/2)){
- y=sum-x;
- dif=abs(x-y);
- if(dif<mn){
- d.clear();
- d.push_back(dif);
- mn=dif;
- idx++;
- }
- return;
- }
- x+=a[pos];
- bt(pos+1);
- x-=a[pos];
- bt(pos+1);
- }
- 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+1;
- bt(0);
- cout<<d[0]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment