题解 | 最大子段和

最大子段和

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

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	ll now=0,ans=-1e5;
	int n;cin>>n;
	for (int i = 1; i <= n; i++)
	{
		int x;cin>>x;
		now+=x;ans=max(ans,now);
		if(now<0)now=0;
	}
	cout<<ans;
	return 0;
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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