-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODEM4.cpp
executable file
·71 lines (68 loc) · 1.65 KB
/
CODEM4.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
#include <bits/stdc++.h>
using namespace std;
int dp[31][31];
int given[31];
int n;
int presum[31];
int pre(int presum[], int i, int j)
{
if (i == 0)
{
return presum[j];
}
return (presum[j] - presum[i - 1]);
}
int solve()
{
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++)
{
dp[i][i] = given[i];
}
for (int k = 1; k < n; k++)
{
int i = 0, j = k;
while (i < n and j < n)
{
int left = given[i], right = given[j];
left = left + ((pre(presum, i + 1, j)) - dp[i + 1][j]);
right = right + ((pre(presum, i, j - 1)) - dp[i][j - 1]);
dp[i][j] = max(left, right);
i++, j++;
}
}
// int x = dp[0][n - 1];
return dp[0][n-1];
}
int solve1(int low, int high)
{
if (low > high)
return 0;
if (dp[low][high] != -1)
return dp[low][high];
int &x = dp[low][high];
if (low == high)
return x = given[low];
return x = max(given[low] + max(solve1(low + 1, high - 1), solve1(low + 2, high)),
given[high] + max(solve1(low + 1, high - 1), solve1(low, high - 2)));
}
int main(int argc, char const *argv[])
{
freopen("/mnt/d/cpp/inputf.in", "r", stdin);
freopen("/mnt/d/cpp/outputf.in", "w", stdout);
int t;
cin >> t;
while (t--)
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> given[i];
presum[0] = given[0];
for (int i = 1; i < n; i++)
presum[i] = presum[i - 1] + given[i];
memset(dp, -1, sizeof(dp));
cout << solve1(0, n - 1);
cout << " " << solve() << endl;
}
return 0;
}