fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const long long oo = 1e18 + 7;
  4. int n;
  5. long long sum;
  6. int a[5005];
  7. long long dp[5005][5005];
  8.  
  9. long long solve(int l, int r) {
  10. if (l > r) return 0;
  11. if (dp[l][r] != -oo) return dp[l][r];
  12. int res = l - 1 + n - r;
  13. long long cur;
  14. if (res % 2 == 0) {
  15. cur = -oo;
  16. cur = max(cur, solve(l + 1, r) + a[l]);
  17. cur = max(cur, solve(l, r - 1) + a[r]);
  18. } else {
  19. cur = oo;
  20. cur = min(cur, solve(l + 1, r) - a[l]);
  21. cur = min(cur, solve(l, r - 1) - a[r]);
  22. }
  23. return dp[l][r] = cur;
  24. }
  25.  
  26. main() {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(0); cout.tie(0);
  29. freopen("TEST.inp", "r", stdin);
  30. freopen("TEST.out", "w", stdout);
  31. cin >> n;
  32. for (int i = 1; i <= n; i++) {
  33. cin >> a[i];
  34. sum += a[i];
  35. }
  36.  
  37. for (int i = 1; i <= n; i++)
  38. for (int j = 1; j <= n; j++) dp[i][j] = -oo;
  39. long long ans = solve(1, n);
  40.  
  41. cout << (sum + ans) / 2 << '\n';
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty