# 3.3.5 A Game 游戏

3.3.5 A Game 游戏

``````6
4 7 2 9
5 2``````

``````18 11
``````

``````/*
ID:zhaofuk1
PROG: game1
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 110;
int Digit[MAXN];
int ans[MAXN][MAXN];
int dp(int x, int y, int sum)
{
int &an = ans[x][y];
if(an != -1) return an;
if(x == y) return an = Digit[x];
return an = max(Digit[x] + sum - Digit[x] - dp(x + 1, y, sum - Digit[x]), Digit[y] + sum - Digit[y] - dp(x, y - 1, sum - Digit[y]));
}

int main()
{
//freopen("game1.in", "r", stdin);
//freopen("game1.out", "w", stdout);
int n;
int x, y, sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &Digit[i]);
sum += Digit[i];
}
memset(ans, -1, sizeof(ans));
x = dp(0, n - 1, sum);
printf("%d %d\n", x, sum - x);
return 0;
}
``````

``````/*
ID:zhaofuk1
PROG: game1
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 110;
int Digit[MAXN];
int ans[MAXN][MAXN];
int dp(int x, int y, int sum)
{
int &an = ans[x][y];
if(an != -1) return an;
if(x == y) return an = Digit[x];
return an = max(Digit[x] + sum - Digit[x] - dp(x + 1, y, sum - Digit[x]), Digit[y] + sum - Digit[y] - dp(x, y - 1, sum - Digit[y]));
}

int main()
{
//freopen("game1.in", "r", stdin);
//freopen("game1.out", "w", stdout);
int n;
int x, y, sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &Digit[i]);
sum += Digit[i];
}
memset(ans, -1, sizeof(ans));
x = dp(0, n - 1, sum);
printf("%d %d\n", x, sum - x);
return 0;
}
``````