Link to this code:
https://cses.fi/paste/7a156e7a466cf4b3dfb7c1/#include <bits/stdc++.h>
using namespace std;
#define in(a) \
for (auto &x : a) \
cin >> x;
#define out(a) \
for (auto x : a) \
cout << x << " ";
#define pb push_back
#define int long long
typedef vector<int> vi;
typedef pair<int, int> pii;
void solve() {
int n;
cin >> n;
vi v(n);
in(v);
// i j choice
vector<vector<int>> dp(n, vector<int>(n, 0));
int k = (n - 1) % 2;
for (int i = 0; i < n; i++) {
dp[i][i] = v[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i + len <= n; i++) {
int j = i + len - 1;
int k = (n - len) % 2;
int ok = k ^ 1;
if (v[i] - dp[i + 1][j] > v[j] - dp[i][j - 1]) {
dp[i][j] = v[i] - dp[i + 1][j];
} else {
dp[i][j] = v[j] - dp[i][j - 1];
}
}
}
int sum = 0;
for (auto x : v) {
sum += x;
}
cout << (dp[0][n - 1] + sum) / 2 << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}