洛谷P1353 [USACO08JAN]跑步Running
1 #include <bits/stdc++.h> 2 #define For(i,j,k) for(int i=j;i<=k;i++) 3 using namespace std ; 4 5 const int N = 10011,M=511,inf = 1e9 ; 6 int f[N][M],d[N] ; 7 int n,m ; 8 9 inline int read() 10 { 11 int x = 0 , f = 1 ; 12 char ch = getchar() ; 13 while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 14 while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; } 15 return x * f ; 16 } 17 18 int main() 19 { 20 n = read() ; m = read() ; 21 For(i,1,n) d[i]=read() ; 22 //For(i,0,n) f[1][i]=-inf ; f[1][1] = d[1] ; f[1][0] = 0 ; 23 For(i,1,n) { 24 For(j,1,min(i,m)) 25 f[i][0]=max(f[i][0],f[i-j][j]) ; 26 f[i][0]=max(f[i][0],f[i-1][0]) ; 27 for(int j=m;j>=1;j--) 28 f[i][j]=max(f[i][j],f[i-1][j-1]+d[i]) ; 29 } 30 printf("%d ",f[n][0]) ; 31 return 0 ; 32 }