fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define yes cout<<"YES\n";
  6. #define no cout<<"NO\n";
  7. const int N=3e5+7;
  8. ll T=1;
  9. ll a[N], n;
  10. ll rec(int i, ll s1, ll s2)
  11. {
  12. if(i > n)
  13. return abs(s1 - s2);
  14.  
  15. ll fp = rec(i + 1, s1 + a[i], s2);
  16. ll sp = rec(i + 1, s1, s2 + a[i]);
  17.  
  18. return min(fp, sp);
  19. }
  20. void solve()
  21. {
  22. cin >> n;
  23.  
  24. for(int i = 1; i <= n; i++){
  25. cin >> a[i];
  26. }
  27.  
  28. cout << rec(1, 0, 0);
  29.  
  30. }
  31. int main()
  32. {
  33. ios::sync_with_stdio(NULL);
  34. cin.tie(0);
  35. cout.tie(0);
  36.  
  37. // freopen("","r", stdin);
  38. // freopen("","w", stdout);
  39. // cin>>T;
  40. while(T--)
  41. solve();
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
3 2 7 4 1
stdout
1