-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1097 - Removal Game.cpp
More file actions
32 lines (27 loc) · 870 Bytes
/
1097 - Removal Game.cpp
File metadata and controls
32 lines (27 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "bits/stdc++.h"
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> a(n), pref(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
pref[i + 1] = pref[i] + a[i];
}
vector<vector<pair<ll, ll>>> dp(n,
vector<pair<ll, ll>>(n, make_pair(-1, -1)));
for (int i = 0; i < n; i++) dp[i][i] = make_pair(a[i], 0);
for (int end = 1; end < n; end++) {
for (int start = end - 1; start >= 0; start--) {
dp[start][end].first = max(a[end] + dp[start][end - 1].second,
a[start] + dp[start + 1][end].second);
dp[start][end].second =
pref[end + 1] - pref[start] - dp[start][end].first;
}
}
cout << dp[0][n - 1].first << "\n";
}