题解 | 最大子段和

最大子段和

https://www.nowcoder.com/practice/f04519cd1d904f50b68f4229a126ab08

#include <bits/stdc++.h>
#define il inline

using namespace std;
using ll = long long;
using ull = unsigned long long;
using int128=__int128_t;

const ll N = 1e6 + 5, mod = 998244353, inf = 2e18;
const double esp=1e-3;
double PI=3.1415926;

il void solve(){
    ll n;
    cin>>n;
    ll ans=-inf,sum=0;
    for(int i=1;i<=n;i++){
        ll x;
        cin>>x;
        if(i==1){
            sum=x;
        }else{
            sum=max(x,sum+x);
        }
        ans=max(ans,sum);
    }
    cout<<ans;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    
    int t = 1;

    //cin >> t;

    while (t--) {
    
        solve();

    }

    return 0;
}

全部评论

相关推荐

11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务