What’s wrong with the following code?

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

It is giving me WA.

```
#include <stdio.h>
int main()
{
long long int n, m, p, i, j, total_cost, cost;
scanf( "%lld %lld %lld", &n, &m, &p );
long long int ar[n][m];
for( i = 1; i <= n; ++i ){
for( j = 1; j <= m; ++j ){
ar[i][j] = j;
}
}
while( p-- ){
scanf( "%lld %lld", &i, &j );
ar[i][j] += 1;
}
/*
printf("\n");
for( i = 1; i <= n; ++i ){
for( j = 1; j <= m; ++j ){
printf("%d ", ar[i][j]);
}
printf("\n");
}
printf("\n");
*/
if( n == 1 && m == 1 ){
printf( "0\n" );
return 0;
}
if( m == 1 ){
for( i = 1; i <= n; ++i ){
printf( "0\n" );
}
return 0;
}
for( i = 1; i <= n; ++i ){
total_cost = 0, cost = 0;
for( j = m; j >= 2; --j ){
cost = ar[i][j] - ar[i][j - 1];
if( cost < 0 ){
printf( "-1\n" );
break;
}
total_cost += cost;
}
if( cost >= 0 ){
printf( "%lld\n", total_cost );
}
}
return 0;
}
```