i don’t know why this code is giving runtime error.
link for the code is
http://www.codechef.com/viewsolution/6992337
Or else the code is here:
#include
using namespace std;
long long int rdp[10005],ldp[10005];
int main()
{
int t;
long int n,i;
long long int a[1000];
cin>>t;
while(t–)
{
long long int sum=0,max1=0,max2=0;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
}
//cout<<“sum=”<<sum<<endl;
ldp[0]=sum;
for(i=1;i<=n;i++)
{
ldp[i]=ldp[i-1]-2a[i];
if(ldp[i]>max1)
max1=ldp[i];
}
//for(i=1;i<=n;i++)
//cout<<ldp[i]<<"\t";
//cout<<“max1=”<<max1<<endl;
rdp[n+1]=sum;
for(i=n;i>=1;i–)
{
rdp[i]=rdp[i+1]-2a[i];
//cout<<rdp[i+1]<<"\t"<<2*a[i]<<endl;
if(rdp[i]>max2)
max2=rdp[i];
}
//for(i=1;i<=n+1;i++)
//cout<<rdp[i]<<"\t";
//cout<<“max2=”<<max2<<endl;
cout<<max(max1,max2)<<endl;
}
}