Getting wrong answer !

Here is the question :-
Problem 1: Railway Catering Contracts, (K Narayan Kumar, CMI)
The government of Siruseri has just commissioned one of the longest and most modern railway routes in the world. This route runs the entire length of Siruseri and passes through many of the big cities and a large number of small towns and villages in Siruseri.
The railway stations along this route have all been constructed keeping in mind the comfort of the travellers. Every station has big parking lots, comfortable waiting rooms and plenty of space for eateries. The railway authorities would like to contract out the catering services of these eateries.
Unfortunately, catering contractors are not philanthropists and would like to maximise their profits. The Siruseri Economic Survey has done a through feasibility study of the different stations and documented the expected profits (or losses) for the eateries in all the railway stations on this route. Every contractor would like to run the catering service only in the profitable stations and stay away from the loss making ones.
On the other hand the authorities would like to ensure that every station was catered to. Towards this end, authorities offered to contract out stations in groups. They would fix a minimum size K and a contractor was only allowed to bid for any contiguous sequence of K or more stations.
For example suppose there are 8 stations along the line and their profitability is as follows:
Station 1 2 3 4 5 6 7 8
Expected Profits -20 90 -30 -20 80 -70 -60 125
If K was fixed to be 3, a contractor could pick stations 3, 4, 5 and 6. This would give him a total profit of -40 (i.e. a loss of 40). He could have picked 3, 4 and 5 giving him a profit of 30. On the other hand if he picked stations 2, 3, 4 and 5, he would make a profit of 120. You can check that this is the best possible choice when K = 3.
If K = 1, then the best choice would be to bid for just station 8 and make a profit of 125.
You have been hired by a contractor. Your task is to help him identify the segment of stations to bid for so at to maximize his expected profit.
Input format
The first line of the input contains two integers N and K, where N is the number of stations and K is the minimum number of contiguous stations that must form part or the bid. The next N+1 lines (lines 2,...,N+1) describe the profitability of the N stations. Line i+1 contains a single integer denoting the expected profit at station i.
Output format
A single integer P, indicating the maximum possible profit.
Test Data:
You may assume that 1 ≤ N ≤ 1000000 and 1 ≤ K ≤ N. You may further assume that in 50% of the inputs N ≤ 5000.
Example:
We illustrate the input and output format using the above example:
Sample Input 1:
8 3
-20
90
-30
-20
80
-70
-60
125
Sample Output 1:
120
Sample Input 2:
8 1
-20
90
-30
-20
80
-70
-60
125
Sample Output 2:
125


And here's my solution :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main() {
    int n,k,i,arr[10000],j,cp=0,gp=0,kc;
    cin>>n>>k;
    for (i=0;i<n;i++) cin>>arr[i];
    for (i=0;i<n;i++) {
        kc=0;
        for (j=i;j<n;j++) {
            kc++;
            cp+=arr[j];
            if (cp>gp && kc+1>k) gp=cp;
        }
        cp=0;
    }
    cout<<gp;
    system("PAUSE");
}

It works correctly for the two sample input and also gives me correct answer in 4 test case out of 10 . But i can't understand why does it gives wrong answer for rest 6 case even when it satisfies time & memory limit ???
Thankyou for help in advanced !
Please rename all your variables to something sensible that helps you read the code and understand the problem.
Last edited on
Well your program will fail for N > 10000 maybe that's the problem.
Also if all eateries make a loss your program gives incorrect answer.
Ok, yes Vin , you were right , that it gives wrong answer when there's a loss !
Any corrections ?
I doubt that fixing the program for all eateries making a loss will correct the program since it's unlikely that in the 6 test cases that failed the inputs were all losses but to fix this maybe initialise gp to a large negative number and not 0.

Give an example of an input for which the program fails.
Thankyou Vin , but i have got the dynamic solution to it (but at the cost of my speed) , i run a for loop and find the minimum most value in the given numbers and set gp's value to that much and now its working local examples , lets see if the server gives it full points (which it must !).
Topic archived. No new replies allowed.