-
Notifications
You must be signed in to change notification settings - Fork 34
/
1031-Easy-Game.cpp
90 lines (62 loc) · 1.15 KB
/
1031-Easy-Game.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <limits.h>
using namespace std;
long long dp[110][110][2];
int a[110];
int n;
long long explore(int l, int r, int chance)
{
long long x;
long long sum;
if(l > r) {
return 0;
}
if(dp[l][r][chance] != -1) {
return dp[l][r][chance];
}
if(chance == 0) {
sum = 0;
x = INT_MIN;
for (int i = l; i <= r; i++) {
sum = sum + a[i];
x = max(x, sum + explore(i+1, r, 1));
}
sum = 0;
for (int i = r; i >= l; i--) {
sum = sum + a[i];
x = max(x, sum + explore(l, i-1, 1));
}
dp[l][r][chance] = x;
return x;
}
if(chance == 1) {
sum = 0;
x = INT_MAX;
for (int i = l; i <= r; i++) {
sum = sum + -a[i];
x = min(x, sum + explore(i+1, r, 0));
}
sum = 0;
for (int i = r; i >= l; i--) {
sum = sum + -a[i];
x = min(x, sum + explore(l, i-1, 0));
}
dp[l][r][chance] = x;
return x;
}
}
int main()
{
int t;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
memset(dp, -1 , sizeof dp);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
printf("Case %d: %lld\n", cs, explore(0, n-1, 0));
}
}