fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int N = 5e3 + 5;
  12.  
  13. int n;
  14. int a[N];
  15.  
  16. ll dp[N][N]; // dp[l][r] = Chênh lệch điểm số tối đa mà người đi trước có thể đạt được khi chơi trên đoạn [l, r]
  17.  
  18. int main() {
  19. ios::sync_with_stdio(false);
  20. cin.tie(nullptr);
  21. cin >> n;
  22. for (int i = 1; i <= n; i++) cin >> a[i];
  23.  
  24. for (int l = n; l >= 1; l--) {
  25. for (int r = l; r <= n; r++) {
  26. if (l == r) {
  27. dp[l][r] = a[l];
  28. continue;
  29. }
  30. dp[l][r] = max(a[l] - dp[l + 1][r], a[r] - dp[l][r - 1]);
  31. }
  32. }
  33.  
  34. ll tot = 0;
  35. for (int i = 1; i <= n; i++) tot += a[i];
  36.  
  37. // Có p1 + p2 = tot
  38. // và p1 - p2 = dp[1][n]
  39. // => 2 * p1 = tot + dp[1][n]
  40. ll p1 = (tot + dp[1][n]) / 2; // Điểm số của người chơi thứ nhất
  41.  
  42. cout << p1 << '\n';
  43. }
Success #stdin #stdout 0.01s 5272KB
stdin
4
4 5 1 3
stdout
8