You are not logged in. Please login at www.codechef.com to post your questions!

×

can u please tell me why it is giving wrong answer for reign?

//http://www.codechef.com/problems/CHODE

include<iostream>

include<stdio.h>

using namespace std;

long long int max(long long int x,long long int y) { return (y > x)? y : x; }

long long int maxSubArraySum(long long int a[],long long int size,long long int k,long long int n) { long long int max_so_far =a[0]; long long int i,pos=0; long long int curr_max =a[0];

long long int sum=0; for (i=1; i < size; i++) { //curr_max = max(a[i], curr_max+a[i]); if(curr_max+a[i]>a[i]) { curr_max=curr_max+a[i]; //pos=i; } else { curr_max=a[i]; //pos=i; }

    //max_so_far = max(max_so_far, curr_max);
    if(max_so_far<curr_max)
    {
    max_so_far=curr_max;
    pos=i;
    }
    else
    max_so_far=max_so_far;
    //pos=i;

} //cout<<"pos="<<pos<<"\\n"; cout<<"max_so_far="&lt;&lt;max_so_far&lt;&lt;" \\n";="" return="" max_so_far;="" long="" long="" int="" max_so_far1="a[pos+k+1];" long="" long="" int="" curr_max1="a[pos+k+1];" for="" (i="pos+k+2;" i="" <="" n;="" i++)="" {="" curr_max1="max(a[i],curr_max1+a[i]);" if(curr_max1+a[i]="">a[i]) { curr_max1=curr_max1+a[i]; //pos=i; } else { curr_max1=a[i]; //pos=i; }

    //max_so_far = max(max_so_far, curr_max);
    if(max_so_far1<curr_max1)
    {
    max_so_far1=curr_max1;
    //pos=i;
    }
    else
    max_so_far1=max_so_far1;
    //max_so_far1= max(max_so_far1,curr_max1);

} //cout<<"max_so_far1="<<max_so_far1<<"\n"; sum=max_so_far+max_so_far1; return sum; }

long long int maxSubArraySum1(long long int a[],long long int size,long long int k,long long int n) { long long int max_so_far =a[n-1]; long long int i,pos=n-1; long long int curr_max =a[n-1];

long long int sum=0; for (i=n-2; i>=size; i--) { //curr_max = max(a[i], curr_max+a[i]); if(curr_max+a[i]>a[i]) { curr_max=curr_max+a[i]; //pos=i; } else { curr_max=a[i]; //pos=i; }

    //max_so_far = max(max_so_far, curr_max);
    if(max_so_far<curr_max)
    {
    max_so_far=curr_max;
    pos=i;
    }
    else
    max_so_far=max_so_far;
    //pos=i;

} //cout<<"pos="<<pos<<"\\n"; cout<<"max_so_far="&lt;&lt;max_so_far&lt;&lt;" \\n";="" return="" max_so_far;="" long="" long="" int="" max_so_far1="a[0];" long="" long="" int="" curr_max1="a[0];" for="" (i="1;i&lt;=pos-k-1;" i++)="" {="" curr_max1="max(a[i],curr_max1+a[i]);" max_so_far1="max(max_so_far1,curr_max1);" if(curr_max1+a[i]="">a[i]) { curr_max1=curr_max1+a[i]; //pos=i; } else { curr_max1=a[i]; //pos=i; }

    //max_so_far = max(max_so_far, curr_max);
    if(max_so_far1<curr_max1)
    {
    max_so_far1=curr_max1;
    //pos=i;
    }
    else
    max_so_far1=max_so_far1;

} //cout<<"max_so_far1="<<max_so_far1<<"\n"; sum=max_so_far+max_so_far1; return sum; }

int main() { long long int t,n,k,i; long long int sum,sum1; cin>>t; //t=in(); while(t--) { //max=max1=-1000000001; sum=sum1=0; cin>>n>>k; //n=in(); //k=in(); long long int a[n+1]; for(i=0;i<n;i++) cin="">>a[i]; //a[i]=in();

sum = maxSubArraySum(a,n-k-1,k,n);
sum1=maxSubArraySum1(a,k+1,k,n);
cout<<max(sum,sum1)<<"\n";
}
return 0;

}

asked 17 Dec '13, 23:36

a9adi835812319's gravatar image

3★a9adi835812319
14325
accept rate: 20%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×102

question asked: 17 Dec '13, 23:36

question was seen: 545 times

last updated: 17 Dec '13, 23:36