P1115-最大子段和
1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < b;i ++) 3 typedef long long ll; 4 using namespace std; 5 int n; 6 inline ll read() 7 { 8 ll ans = 0; 9 char ch = getchar(), last = ' '; 10 while(!isdigit(ch)) last = ch, ch = getchar(); 11 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 12 if(last == '-') ans = -ans; 13 return ans; 14 } 15 inline void write(ll x) 16 { 17 if(x < 0) x = -x, putchar('-'); 18 if(x >= 10) write(x / 10); 19 putchar(x % 10 + '0'); 20 } 21 int a[200039]; 22 23 int main() 24 { 25 n = read(); 26 _for(i,0,n) 27 a[i] = read(); 28 29 int rnt = a[0]; 30 int trnt = a[0]; 31 _for(i,1,n) 32 { 33 if(trnt<0) 34 trnt = a[i]; 35 else 36 trnt += a[i]; 37 rnt = max(rnt,trnt); 38 } 39 write(rnt); 40 return 0; 41 }