题目传送门
一道(dp),(f_{i,j})表示到第(i)位,已经踩准了(j)次的最佳答案.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,t,s[5001],b[5001];
int f[5001][5001],ans = -1000000000;
int main() {
scanf("%d%d",&n,&t);
for(int i = 1;i <= n; i++)
scanf("%d",&s[i]);
for(int i = 1;i <= n; i++)
scanf("%d",&b[i]);
if(t == 1) f[1][1] = max(b[1],0) + s[1];
else f[1][1] = s[1];
for(int i = 1;i <= n; i++) f[i][0] = 0 - s[i] + f[i-1][0],ans = max(f[i][0],ans);
for(int i = 2;i <= n; i++)
for(int j = 1;j <= i; j++) {
f[i][j] = max(f[i-1][j-1] + s[i] + ((j % t == 0) ? b[i] : 0),f[i-1][j] - s[i]);
ans = max(ans,f[i][j]);
}
printf("%d",ans);
return 0;
}