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
1
10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
lv[i]表示以第i个元素为连续序列最后一个元素时的最大和;
rv[i]表示以第i个元素为连续序列第一个元素时的最大和
mav[i]为倒数i个元素中最大连续子段和
#include<iostream>
#include<cstdio>
using namespace std;
#define inf INT_MIN
int a[50010],lv[50010],rv[50010],mav[50010];
int main()
{
int t;cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
lv[1]=a[1];rv[n]=a[n];
for(int i=2;i<=n;i++)
{
lv[i]=max(lv[i-1]+a[i],a[i]);
}
for(int i=n-1;i>=1;i--)
{
rv[i]=max(a[i],a[i]+rv[i+1]);
}
mav[n]=a[n];
for(int i=n-1;i>=1;i--)
{
mav[i]=max(rv[i],mav[i+1]);///对于最后i个元素组成的序列,第i个元素是最大连续子串中的元素或不是这两种状态
}
int ans=inf;
for(int i=2;i<=n;i++)
{
ans=max(ans,lv[i-1]+mav[i]);
}
printf("%d\n",ans);
}
}