-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
25 lines (24 loc) · 724 Bytes
/
program.cpp
File metadata and controls
25 lines (24 loc) · 724 Bytes
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
#include "../include/pre.h"
int maxCoins(vector<int>& nums)
{
const int N = nums.size();
vector<vector<int>> dp (N + 1, vector<int>(N + 1, 0));
auto expandNums = [&nums, N](int k){return (k >= 0 && k < N) ? nums[k] : 1;};
for (int l = 1; l != N + 1; l++) {
for (int s = 0; s != N - l + 1; s++) {
for (int k = 0; k != l; k++) {
dp[s][l] = std::max(
dp[s][l],
dp[s][k] + expandNums(s + k) * expandNums(s - 1) * expandNums(s + l) + dp[s + k + 1][l - k - 1]
);
}
}
}
return dp[0].back();
}
int main()
{
vector<int> test = {3, 1, 5, 8};
cout << maxCoins(test) << endl;
return 0;
}