Why I am getting wrong answer in TLG?

#include<stdio.h>
#include<math.h>
int main()
{
int p[10000],q[10000],a[10000],i,j,n,r;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&q[i]); //p[i]->score of player1 q[i]->score of player2//
a[i]=abs(p[i]-q[i]);
}
for(i=1;i<n;i++)
{
if(a[i]>a[0])
{
a[0]=a[i];
p[0]=p[i];
q[0]=q[i];
}
}
if(p[0]>q[0])
{
r=1;
printf("%d %d",r,a[0]);
}
else
{
r=2;
printf("%d %d",r,a[0]);
}
printf("\n");
return 0;
}

I don’t mean to offend you, but can you yourself read your code the way it’s posted here? Please use Code option to paste the code, fully indented, adding comments is a good habit. Try posting ideone link. ALso, atleast mention the question you are referring to, then only we can tell any mistake :stuck_out_tongue:

See this test case:

5
12 100
100 13
1413 234
12 435
123 34

Your code gives output as 1 1179, but the correct output is 1 1178 (I know one of the input in the above case is >1000, but that doesnot make any difference).

Also, see this:

 5
12 100
100 13
141 234
12 435
123 34

Your code gives 2 423, but the correct output is 2 517. Your logic is wrong, you need to think over it once again and correct it. Also, there is no need of using so much of space you are allocating. :slight_smile:

Because i dont have the permission to comment i am answering this. @damn_me how come the answer is 1178? Please explain. Thanks in advance :slight_smile:

Hello,
i have understood damn_me’s idea for the solution ,i applied it but it still gives me a wrong answer.
http://www.codechef.com/viewsolution/6361933

http://www.codechef.com/viewsolution/6362454

i’ve tried all the possible combinations that i found and all that i could think of, for this problem, and i still received a wrong answer.
i don’t know what solutions to try anymore.

Thank you,
Bogdan

import java.util.* ;
class Main{
	public static void main(String []args){
		Scanner sc = new Scanner(System.in) ;
		int W,L,P1,P2,N,S1,S2;
		S1 = S2 = L = W = 0 ; 	
		N = sc.nextInt() ;
		while(N-- > 0){
			P1 = sc.nextInt() ;
			P2 = sc.nextInt() ;
			S1 += P1 ;
			S2 += P2 ;
			if(S1 > S2){
				if(S1-S2 >= L){
					L = S1 - S2 ;
					W = 1 ; 
				}			
			}else{
				if(S2-S1 >= L){
					L = S2 - S1 ; 
					W = 2 ;
				}
			}
		}
		System.out.print(W+" "+L) ;
	}
}

this will surely help you guys :slight_smile: .