基准时间限制:1 秒 空间限制:131072 KB 分值: 0
难度:基础题
N个整数组成的序列a[1],a[2],a[3],…,a[n],
求该序列如a[i]+a[i+1]+…+a[j]的连续子段和的最大值。当所给的整数均为负数时和为0。
例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为20。
Input
第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N + 1行:N个整数(-10^9 <= A[i] <= 10^9)
Output
输出最大子段和。
Input示例
6 -2 11 -4 13 -5 -2
Output示例
20
题解:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #define LL long long using namespace std; int main() { int n; LL a[100005]; LL maxx=0,t=0; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; t+=a[i]; if(t>maxx) maxx=t; if(t<0) t=0; } cout<<maxx<<endl; return 0; }