Advertisement
luanaamorim

Untitled

Apr 7th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <iomanip>
  8. #include <map>
  9. #include <cstring>
  10. #include <set>
  11. #define ll long long
  12. #define INF ((int) (1e6))
  13. #define MAX (2e5)
  14. #define MOD 1000000007
  15. #define par pair<int, int>
  16. #define all(v) v.begin(), v.end()
  17. #define lsb(x) (x & -x)
  18. #define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  19.  
  20. using namespace std;
  21.  
  22. int memo[300][100][100], n, arr[100], soma;
  23.  
  24. int f(int tot, int idx, int qnt)
  25. {
  26. if (tot > (soma>>1)) return INF;
  27. if (idx > n) return memo[tot][idx][qnt] = ((qnt == (n>>1)) ? (soma-tot)-tot : INF);
  28.  
  29. if (~memo[tot][idx][qnt]) return memo[tot][idx][qnt];
  30.  
  31. return memo[tot][idx][qnt] = min(f(tot + arr[idx], idx + 1, qnt + 1), f(tot, idx + 1, qnt));
  32. }
  33.  
  34. int main()
  35. {_
  36. cin >> n;
  37. for (int i = 1; i <= n; i++)
  38. {
  39. cin >> arr[i];
  40. soma += arr[i];
  41. }
  42. memset(memo, -1, sizeof(memo));
  43.  
  44. cout << f(0, 1, 0) << endl;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement