Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Contest Page | CodeChef[^]
Constraints:>
All elements are >=1 i.e. a[i]>=1 and (10>=t>=1) and (700>=n>=1)

What I have tried:

i have tried almost all types of test cases i could possibly do, all come out correct and i am unable to recognize where my code is failing.
#include<stdio.h>
int main()
{
    long long int n, t, k, i, j, max[701], a[701][701], sum, flag;
 
    scanf( "%lld", &t );

    for( k = 0 ; k < t ; k++ )
    { 
        scanf( "%lld", &n );

        for( i = 0 ; i < n ; i++ )
        {  
            for( j = 0 ; j < n ; j++ )
            { 
                scanf( "%lld", &a[i][j] );
                if( j == 0)
                    max[i] = a[i][0];
                if( a[i][j] > max[i] )
                    max[i] = a[i][j];
            }
        }

        sum = 0, flag = 0;

        for( i = 0 ; i < n-1 ; i++ )
        { 
            if( max[i] < max[i+1])
                sum = sum + max[i];
            else
            {
                flag = 1;
                break;
            }
        }

        if(flag == 1)
            printf("-1\n");
        else
        {
            sum = sum + max[n-1];
            printf("%lld\n", sum );
        }
    }
}
Posted
Updated 8-Jan-18 9:20am
v3
Comments
Richard MacCutchan 7-Jan-18 12:14pm    
What happens when you run it?
ahmedarifhasan 7-Jan-18 12:18pm    
it's running successfully,but the codechef compiler isn't accepting, ofcourse there's some error, which i am unable to recognize.
Richard MacCutchan 7-Jan-18 12:32pm    
Ask codechef. Unless you explain the problem it is difficult to guess a suggestion.
ahmedarifhasan 7-Jan-18 12:36pm    
well, can you tell a test case where this fails?
Richard MacCutchan 7-Jan-18 12:38pm    
Where what fails? You have not even explained what this code is supposed to do.

Quote:
Can someone please tell me why is this code not being accepted? Note:I am not asking for a solution, just tell me where this code fails

Your problem is that your code is the correct answer to another question.
you compute the maximum of each lines and then add the maximums while checking they respect the secondary constraint.

You have a goal:
Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead.

and a constraint:
You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1.

You have never been told to use the maximum of each line.
You have to use the maximum value that respect the constraint.

Input:
1
3
 6 2 3
11 5 6
 7 8 9

Output:
3 + 6 + 9 = 18


[Update]
You must understand that a full explanation means that you failed the contest.
You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1.

The constraint is constructed in such a way that it imply to compute the maximum allowed of each line in reverse order.
For practical considerations, you must reverse the constrain to:
For each i (1 ≤ i ≤ N-1), Ei should be strictly lower than Ei+1.

And last line have no constraint.
Input:
1
3
 6 2 3
11 5 6
 7 8 9

Solving:
C++
 7 8 9: no constraint,  7 8 9 qualified, maximum is 9.
11 5 6: constraint is < 9,  5 6 qualified, maximum is 6.
 6 2 3: constraint is < 6, 2 3 qualified, maximum is 3.
output is: 9+6+3=18

C++
1
3
6 10 12
8 15 7
8 19 10

Solving:
C++
8 19 10: no constraint, 8 19 10 qualified, maximum is 19.
8 15 7: constraint is < 19,  8 15 7 qualified, maximum is 15.
6 10 12: constraint is < 15, 6 10 12 qualified, maximum is 12.
output is: 19+15+12=46
 
Share this answer
 
v3
Comments
ahmedarifhasan 8-Jan-18 9:03am    
@ppolymorphe

If you look at example given:
Example
Input:

1
3
1 2 3
4 5 6
7 8 9

Output:

18
Explanation
Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.

if you notice they have mentioned "maximize", why would they?

they could have chosen 1+4+7=12 according to what you have said.
ahmedarifhasan 8-Jan-18 11:59am    
@ppolymorphe
Also,
"Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead."
And the maximum possible value if the condition :
"For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1."
is satisfied, then max value comes only if we take the maximum element of a particular sequence.
1
3
6 10 12
4 5 7
8 9 10
-1

ouput should be 6+7+10=23
 
Share this answer
 
Comments
Patrice T 8-Jan-18 13:46pm    
Does it explain the reason why the program is wrong?
ahmedarifhasan 8-Jan-18 14:03pm    
suppose we have
1
3
6 10 12
8 15 7
8 19 10
then what?
is it 6+7+19 or 6+7+10? or 6+7+8?
chitaranjan pradhan 8-Jan-18 14:26pm    
you have to check Ei is should be greater than Ei-1; and sum should be maximum ok.
from 6 10 12 :-12 is bigger and 8 15 7 :-15 is bigger and 12<15 satisfy
from 8 19 10:-19 is bigger and 15<19 is satisfy so sum will be 12+15+19=46
chitaranjan pradhan 8-Jan-18 14:17pm    
12+15+19=46
chitaranjan pradhan 8-Jan-18 14:26pm    
12+15+21=48

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900