-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10714.cpp
More file actions
60 lines (56 loc) · 1.36 KB
/
Copy path10714.cpp
File metadata and controls
60 lines (56 loc) · 1.36 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
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N;
vector<long long> pieces;
vector<vector<long long>> dp;
long long getMax(int left, int right, int cnt)
{
if (dp[left % N][right % N] != -1)
{
return dp[left % N][right % N];
}
if (cnt % 2 == 0)
{
if (left % N == right % N)
{
return dp[left % N][right % N] = pieces[left % N];
}
return dp[left % N][right % N] = max(getMax(left + 1, right, cnt + 1) + pieces[left % N], getMax(left, right - 1, cnt + 1) + pieces[right % N]);
}
else
{
if (left % N == right % N)
{
return dp[left % N][right % N] = 0;
}
if (pieces[left % N] > pieces[right % N])
{
return dp[left % N][right % N] = getMax(left + 1, right, cnt + 1);
}
else
{
return dp[left % N][right % N] = getMax(left, right - 1, cnt + 1);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long answer = 0;
cin >> N;
pieces.resize(N);
dp.assign(N, vector<long long>(N, -1));
for (int i = 0; i < N; i++)
{
cin >> pieces[i];
}
for (int i = 0; i < N; i++)
{
answer = max(answer, getMax(i + 1, i - 1 + N, 1) + pieces[i]);
}
cout << answer << endl;
return 0;
}