首页 > 代码库 > poj 2479

poj 2479

Maximum sum
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 34180 Accepted: 10588

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. 
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1101 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer. 

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;#define max(a,b) a>b?a:b#define MAXV 50010#define inf -10010int lt[MAXV],rt[MAXV],a[MAXV],rtm[MAXV];int main(){int t,n,i,temp;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);temp=inf;lt[1]=a[1];rt[n]=a[n];for(i=2;i<=n;i++){    lt[i]=max(a[i],lt[i-1]+a[i]);}for(i=n-1;i>=1;i--){rt[i]=max(a[i],rt[i+1]+a[i]);}rtm[n]=rt[n];for(i=n-1;i>=1;i--)    rtm[i]=max(rtm[i+1],rt[i]);int ma=inf;for(i=2;i<=n;i++){    ma=max(ma,lt[i-1]+rtm[i]);}printf("%d\n",ma);}return 0;}

  

poj 2479