最大子段和

只要维护一个dp[i]就行了,dp[i]表示以i结尾的最大字段和,要么是第i个数本身,要么是第i个数加上dp[i-1],只要dp[i-1]>0,那么就肯定是后者
`#include

const int N=2e5+7;

int main(){
int n,lst[N]={0};
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&lst[i]);
}
for(int i=1;i<=n;i++){
if(lst[i-1]>0){
lst[i]+=lst[i-1];
}
}
int max=-1*N;
for(int i=1;i<=n;i++){
if(lst[i]>max){
max=lst[i];
}
}
printf("%d",max);
return 0;
}`

posted @ 2025-02-16 20:06  十柒*  阅读(1)  评论(0编辑  收藏  举报