Base Time Limit: 1 second Space Limit: 131072 KB Score: 0
Difficulty: Basic
collect
focus on
A sequence a[1],a[2],a[3],…,a[n] composed of N integers,
find the sequence such as a[i]+a[i+1]+…+a[j] The maximum value of the sum of consecutive subsections of . The sum is 0 when all of the given integers are negative.
For example: -2,11,-4,13,-5,-2, and the largest subsegments are: 11,-4,13. and 20.
Input
Line 1: Length N of integer sequences (2 <= N <= 50000) Lines 2 - N+1: N integers (-10^9 <= A[i] <= 10^9)
Output
Output the maximum subsection sum.
Input example
6 -2 11 -4 13 -5 -2
Output example
20
dp past, find the largest.
1 #include<iostream> 2 using namespace std; 3 int a[500050]; 4 int main(){ 5 int n; 6 cin>>n; 7 for(int i=0;i<n;i++) 8 cin>>a[i]; 9 long long sum=0,maxn=0,ss=0; 10 for(int i=0;i<n;i++){ 11 sum+=a[i]; 12 sum=max(sum,ss); 13 maxn=max(maxn,sum); 14 } 15 cout<<maxn<<endl; 16 return 0; 17 }