fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. int n;
  7. int a[35];
  8. int res = 0;
  9. int total = 0;
  10.  
  11. void backtrack(int i, int sumA, int sumB){
  12. if(sumA > total/2 || sumB > total/2) return;
  13.  
  14. if(i > n){
  15. if(sumA == sumB){
  16. res = max(res, sumA);
  17. }
  18. return;
  19. }
  20.  
  21. // cho An
  22. backtrack(i+1, sumA + a[i], sumB);
  23.  
  24. // cho Bình
  25. backtrack(i+1, sumA, sumB + a[i]);
  26.  
  27. // bỏ
  28. backtrack(i+1, sumA, sumB);
  29. }
  30.  
  31. signed main(){
  32. cin >> n;
  33.  
  34. for(int i = 1; i <= n; i++){
  35. cin >> a[i];
  36. total += a[i];
  37. }
  38.  
  39. backtrack(1, 0, 0);
  40.  
  41. cout << res;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
9 8 4 5 13
stdout
17