Why does the USACO website keep telling me my answer on the sample input case is wrong?

I am currently trying to submit the following code into USACO 2019 Bronze Problem 1 (Cow Gymnastics). The website keeps saying that my code is outputting 2 for the sample input case, but it is actually outputting 4 (the correct answer). Why is this happening?

Here is a link to the problem and the sample input case that I am talking about:


Here is the code that I tried to submit:

# include <iostream>
using namespace std; 

// Strategy: 
// 1. Create a 2D matrix that holds a grid of size K * N
    // K rows, N columns
    // Make sure that the matrix is 1-based (for convenience -> the code will also work if you make the matrix 0-based)
// 2. Iterate through all the pairs that exist within a certain practice session 
    // Find pair (A, B) -> Cow A did better than Cow B 
    // Once you found the pair, increment [A][B]
// 3. Count the number of pairs that contain a value that is equal to K    
    // Cow A has consistently scored higher than Cow B throughout all practice sessions 

// You might also want to create a 2D matrix to hold the original input values 

int main() {
    freopen("gymnastics.in", "r", stdin); 
    freopen("gymnastics.out", "w", stdout); 

    int K, N, nVal; 
    cin >> K >> N; 
    int input[30][30]; 
    int a[30][30]; 

    // Set the values in 'a' equal to 0 
    // Make sure to modify only the indexes that we're going to use in our code

    for(int i = 1; i <= K; i ++) {
        for(int j = 1; j <= N; j ++) {
            a[i][j] = 0; 

    // Take in the inputs (one-based)
    for(int i = 1; i <= K; i ++) {
        for(int j = 1; j <= N; j ++) {
            cin >> nVal; 
            input[i][j] = nVal; 

    // Fill out 'A' 
    for(int i = 1; i <= K; i ++) {
        for(int j = 1; j <= N; j ++) {
            for(int k = j + 1; k <= N; k ++) {

    // Iterate through the indexes in 'a' that we have filled out + determine how many of them contain a value that is equal to K 

    int counter = 0; 

    // for(int i = 1; i <= N; i ++) {
    //     for(int j = 1; j <= N; j ++) {
    //         cout << a[i][j] << " "; 
    //     }
    //     cout << endl; 
    // }

    for(int i = 1; i <= N; i ++) {
        for(int j = 1; j <= N; j ++) {
            if(a[i][j] == K) {
                counter ++; 

    cout << counter << endl; 
    return 0; 

As mentioned in the above link, you should check for undefined behavior. I’ve added some more information about it in this PR, let me know if it helps.

I do not currently have any uninitialized variables nor missing return statements. I have tried running my code in the USACO Guide Self-Study compiler and found that I got all of my test cases right. I have also tried running the same code on Repl.it and got the same answers. It is just on the USACO website that I am getting a different output.

You do. I get a different result (3) when I run it on the USACO Guide IDE:


That’s strange. I have tried passing my code into the submission box for the Bronze Self-Study Course and I have gotten all my test cases correct. I am also having trouble understanding where I have an uninitialized variable.

You don’t initialize all the entries of a that you use to 0.