-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11062.cpp
More file actions
52 lines (51 loc) · 1.06 KB
/
Copy path11062.cpp
File metadata and controls
52 lines (51 loc) · 1.06 KB
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
#include <stdio.h>
#include <algorithm>
using namespace std;
int dp[1001][1001];
int cards[1001];
int N;
int func(int left, int right, bool turn)
{
if (dp[left][right] != -1)
{
return dp[left][right];
}
else
{
if (turn)
{
dp[left][right] = max(func(left + 1, right, !turn) + cards[left], func(left, right - 1, !turn) + cards[right]);
}
else
{
dp[left][right] = min(func(left + 1, right, !turn), func(left, right - 1, !turn));
}
return dp[left][right];
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
dp[i][j] = -1;
}
}
for (int i = 0; i < N; i++)
{
scanf("%d", &cards[i]);
if (N % 2)
dp[i][i] = cards[i];
else
dp[i][i] = 0;
}
printf("%d\n", func(0, N - 1, true));
}
return 0;
}