fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define ll long long
  5. #define endl '\n'
  6. ll N = 1e9+7;
  7. ll oo =1e13;
  8. const ll maxx = 1000000 + 3;
  9. ll n,k,q,x,y,z,l,r,mid,ans;
  10. string s1,s2;
  11. ll arr[30];
  12.  
  13. ll go(ll ind,ll g1,ll g2) {
  14. if (ind==n)return abs(g1-g2);
  15.  
  16. ll ch1=go(ind+1,g1+arr[ind],g2);
  17. ll ch2=go(ind+1,g1,g2+arr[ind]);
  18.  
  19. return min(ch1,ch2);
  20. }
  21. void solve() {
  22. cin>>n;
  23. for (ll i=0 ;i<n;i++)cin>>arr[i];
  24. cout<<go(0,0,0);
  25. }
  26. int main() {
  27. ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  28. ll t=1 ;
  29. // cin >>t;
  30. while (t--) {
  31. ans=0;
  32. solve();
  33. }
  34. }
Success #stdin #stdout 0s 5320KB
stdin
5
3 2 7 4 1
stdout
1