Help on a train.usaco.org problem: Milking Cows in C++

On train.usaco.org, there’s a problem about milking cows. The problem is called milk2 and I have problems solving it. I was able to get through the first seven test cases but got stuck on the eighth. I can’t provide a link since it also allows access to my account, but I can copy+paste the question in.

Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

  • The longest time interval at least one cow was milked.
  • The longest time interval (after milking starts) during which no cows were being milked.

**NOTE:**Milking from time 1 through 10, then from time 11 through 20 counts as two different time intervals.

PROGRAM NAME: milk2

INPUT FORMAT

Line 1: The single integer, N
Lines 2…N+1: Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in)

3 300 1000 700 1200 1500 2100

OUTPUT FORMAT

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

SAMPLE OUTPUT (file milk2.out)

900 300

My code is as follows: (I use C++)

#include <iostream>
#include <string>
#include <fstream>
#include <bits/stdc++.h>

int main(){
    
    std::ofstream fout ("milk2.out");
    std::ifstream fin ("milk2.in");
    int numOfCows{0};
    fin >> numOfCows;
    int startMilking[5000];
    int endMilking[5000];
    int sortStart[5000];
    int sortEnd[5000];
    for(size_t i = 0; i < numOfCows; i++){
        fin >> startMilking[i] >> endMilking[i];
    }
    for(size_t i = 0; i < numOfCows; i++){
        sortStart[i] = startMilking[i];
        sortEnd[i] = endMilking[i];
    }

    std::sort(startMilking, startMilking+numOfCows);
    for(size_t i = 0; i < numOfCows; i++){
        for(size_t j = 0; j < numOfCows; j++){
            if(startMilking[i] == sortStart[j]){
                endMilking[i] = sortEnd[j];
            }
        }
    }

    /*for(size_t i = 0; i < numOfCows; i++){
        sortout << startMilking[i] << ' ' << endMilking[i] << std::endl;
    }*/

    int gap{0}; // if gap > 0, is idle time. if gap <= 0, is active time
    int maxIdle{0};
    int maxActive = endMilking[0] - startMilking[0];
    int tempMaxActive = maxActive;
    int maxEndTime = endMilking[0];

    for(size_t i = 1; i < numOfCows; i++){
        if(endMilking[i-1] > maxEndTime){
            maxEndTime = endMilking[i-1]; // sets new maxEndTime
        }
        
        if(startMilking[i] - maxEndTime > 0){
            gap = startMilking[i] - maxEndTime; // subtracts startTime from previous endTime to see if there are any gaps (in time) in between
        } else {
            gap = 0; // no gap
        }

        if(endMilking[i] > maxEndTime){ // this condition must be true (or else we shouldn't add it to the maxActive, and there would be no gap anyways)
            if(gap > 0){ // if a gap exists
                maxIdle = std::max(maxIdle, gap); // set new maxIdle
                maxActive = std::max(maxActive, tempMaxActive); // sets maxActive if there's a bigger one
                tempMaxActive = endMilking[i] - startMilking[i]; // resets tempMaxActive for another iteration
            } else { // if a gap doesn't exist
                if(startMilking[i] <= maxEndTime){ // if the startTime is less than or equal to the maxEndTime
                    tempMaxActive = endMilking[i] - maxEndTime + tempMaxActive; // set the tempMaxActive to itself plus endTime minus startTime
                }
            }
        }
    }

    maxActive = std::max(maxActive, tempMaxActive);

    fout << maxActive << ' ' << maxIdle << std::endl;

    return 0;
}

*On test case 8, it says

Run 8: Execution error: Your program did not produce an answer
        that was judged as correct. The program stopped at 0.088 seconds;
        it used 1724 KB of memory. At character number 1, your answer says
        '1' while the correct answer says '2'

Here are the respective outputs:
        ----- our output ---------
        21790_8
        ---- your output ---------
        15007_8
        --------------------------

\quad

Unfortunately, I don’t have a better and more accurate program to run this one against. I still don’t understand the last test case and I don’t want to read through all of it since it’s 5000 lines long.

You can try implementing your solution in a different way, or looking up someone else’s solution.