AcWing 1059. 股票买卖 VI
原题链接
中等
作者:
wjie
,
2020-07-10 12:51:44
,
所有人可见
,
阅读 500
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int arr[N], f[N][2];
int main()
{
int n, c;
scanf("%d %d", &n, &c);
for (int i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
memset(f, -0x3f, sizeof(f));
f[1][0] = 0;
f[1][1] = -arr[1] - c;
for (int i = 2; i <= n; ++i)
{
f[i][0] = max(f[i-1][0], f[i-1][1]+arr[i]);
f[i][1] = max(f[i-1][1], f[i-1][0]-arr[i]-c);
}
printf("%d", f[n][0]);
return 0;
}