AcWing 1057. 股票买卖 IV
原题链接
中等
作者:
hai阿卢
,
2021-03-10 19:52:54
,
所有人可见
,
阅读 264
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5 + 10, M = 110;
int f[2][N][M], w[N];
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> w[i];
memset(f, -0x3f, sizeof(f));
for(int i = 0; i <= n; i++) f[0][i][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
f[0][i][j] = max(f[0][i - 1][j], f[1][i - 1][j] + w[i]);
f[1][i][j] = max(f[1][i - 1][j], f[0][i - 1][j - 1] - w[i]);
}
}
int res = 0;
for(int i = 1; i <= n; i++) res = max(res, f[0][n][i]);
cout << res << endl;
return 0;
}