1 3 1 2 3
2 6 -1 4 -2 3 -2 3
4 6 -1 4 -2 3 -2 3
#include <stdio.h>
#include <string.h>
#define N 1000005
int dp[N][N];
int num[N];
int t[2][N];
int n , m ;
int max(int a ,int b)
{
return a>b?a:b;
}
int not(int i){
return i==1?0:1;
}
int main(){
int i , j , k ;
while(scanf("%d %d",&m,&n)!=EOF){
//memset(num,0,sizeof(num));
//memset(t,0,sizeof(t));
for(i = 1 ; i <= n ; i ++){
scanf("%d",&num[i]);
//dp[0][0]=dp[1][0]=dp[0][1]=0;
//以下代码 超时 应该使用滚动数组
/*
for ( i = 1 ; i <=m ;i ++){
int max2=-32769;
for ( j = 1 ; j <=n ;j ++){
int max1=-32769;
for ( k = 1 ; k <j ;k ++){
max1=max(max1,dp[i-1][k]);
}
max2=max((dp[i][j-1]+num[j]),(max1+num[j]));
dp[i][j]=max2; printf("{%d,%d ->%d |%d| }\n",i,j,max2,max1);
}
}
*/
{
int max2=-0x7ffffff;
t[0][0]=-0x7ffffff;
for ( j = 1 ; j <=n ;j ++){
max2=max(max2, t[0][j-1]);
t[0][j]=max(t[0][j-1]+num[j],num[j]);
}
for ( j = 1 ; j <=n ;j ++){
if(t[0][j-1]>t[0][j])t[0][j]=t[0][j-1];
//printf("{|%d|}\n",t[0][j]);
}
}
unsigned int f=1;
for ( i = 2 ; i <=m ; i++){
int max1=-0x7ffffff;
for ( j = 1 ; j < n ;j++){
if(t[not(f)][j]<t[not(f)][j-1]){t[not(f)][j]=t[not(f)][j-1]; //printf("{%d_%d}%d..%d\n",t[not(f)][j],max1,i,j);
//printf("{%d,%d}\n",t[f][j],max1);
}
}
int max2=-0x7ffffff;
for ( j = 1 ; j <=n ;j ++){
max2=max( t[not(f)][j-1],max2);
//printf("{%d,%d ->%d |%d| }\n",i,j,max2,max1);
}
t[f][j-1]=max2+num[j-1];
}
printf("%d\n",max(t[0][n],t[1][n]));
/*
for ( i = 1 ; i <=m+1 ;i ++){
for ( j = 1 ; j <=n+1 ;j ++){
printf("[%d][%d]%d ",i,j,dp[i][j]);
}
}
*/
}
return 0;
}