题目地址:http://www.patest.cn/contests/mooc-ds/01-1
#include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> using namespace std; #define read() freopen("in.txt", "r", stdin) #define write() freopen("out.txt", "w", stdout) #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define cpy( a , x ) memcpy ( a , x , sizeof a ) #define max(a,b) ((a>b)?(a):(b)) #define LL long long #define MaxSize 100000 int gcd(int a,int b) { return b?gcd(b,a%b):a; } inline int lcm(int a,int b) { return a*b/gcd(a,b); } int MaxSubseqSum(int s[], int N) { int ThisSum = 0; int MaxSum = 0; for (int i = 0; i < N; ++i) { ThisSum += s[i]; if (ThisSum > MaxSum) { MaxSum = ThisSum; }else if (ThisSum < 0) { ThisSum = 0; } } return MaxSum; } int s[MaxSize]; int main() { read(); int n; while(scanf("%d",&n)!=EOF) { for (int i = 0; i < n; ++i) { scanf("%d",&s[i]); } printf("%d ",MaxSubseqSum(s,n) ); } return 0; }