P1115 最大子段和

点击查看代码
#include<bits/stdc++.h>
using namespace std;

int n;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n;

    int x;
    int ans=-2e9;
    int dp=-2e9;
    for(int i=1;i<=n;i++){
        cin>>x;
        dp=max(dp+x,x);
        ans=max(dp,ans);
    }

    cout<<ans<<"\n";

    return 0;
}
posted @ 2026-03-09 23:37  AnoSky  阅读(0)  评论(0)    收藏  举报