USACO A Game

Code

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
33
34
35
36
37
38
39
/*
ID: wcr19961
PROG: game1
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;

typedef long long LL;
const int maxn = 110;
int a[maxn], sum[maxn];
int dp[maxn][maxn];

int Sum(int l, int r) {
return sum[r] - sum[l - 1];
}

int main() {
freopen("game1.in", "r", stdin);
freopen("game1.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
for (int i = n; i >= 1; --i) {
dp[i][i] = a[i];
for (int j = i + 1; j <= n; ++j) {
dp[i][j] = max(Sum(i + 1, j) - dp[i + 1][j] + a[i], Sum(i, j - 1) - dp[i][j - 1] + a[j]);
}
}
printf("%d %d\n", dp[1][n], sum[n] - dp[1][n]);
return 0;
}