#include <cstdio> #include <map> #include <iostream> #include<cstring> #include<bits/stdc++.h> #define ll long long int #define M 6 using namespace std; inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; int m,n; int a[1000007]; int dp1[1000007]; int dp2[1000007]; int main(){ //ios::sync_with_stdio(false); while(~scanf("%d%d",&m,&n)){ memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int maxn; for(int i=1;i<=m;i++){ //枚举子序列 maxn=-inf; for(int j=i;j<=n;j++){ //j = i是因为每个子序列最少1个元素 dp1[j]=max(dp2[j-1]+a[j],dp1[j-1]+a[j]); dp2[j-1]=maxn; maxn=max(maxn,dp1[j]); } } cout<<maxn<<endl; } }
hdu 1024 Max Sum Plus Plus(m段最大和)
原文:https://www.cnblogs.com/wmj6/p/10395813.html