/*
* Solution Template for Tennis Robot II
*
* Australian Informatics Olympiad 2024
*
* This file is provided to assist with reading and writing of the input
* files for the problem. You may modify this file however you wish, or
* you may choose not to use this file at all.
*/
#include
/* N is the number of bins. */
int N;
/* M is the number of instructions. */
int M;
/*
* X contains the number of balls in each bin. Note that this array starts from
* 1 (not 0), and so the values are X[1] to X[N].
*/
long long X[200005];
/*
* A and B contain the instructions. Note that the arrays start from 0, and so
* the instructions are (A[0], B[0]) to (A[M-1], B[M-1]).
*/
int A[200005];
int B[200005];
long long answer;
int main(void) {
FILE *input_file;
FILE *output_file;
int i;
/* Open the input and output files. */
input_file = fopen("tennisin.txt", "r");
output_file = fopen("tennisout.txt", "w");
/* Read the values of N, M, X, A, and B from input file. */
fscanf(input_file, "%d%d", &N, &M);
/* Values in X are indexed from 1 to N (not 0 to N-1) */
for (i = 1; i <= N; i++) {
fscanf(input_file, "%lld", &X[i]);
}
for (i = 0; i < M; i++) {
fscanf(input_file, "%d", &A[i]);
fscanf(input_file, "%d", &B[i]);
}
/*
* TODO: This is where you should compute your solution. Store the number
* of instructions that the robot will successfully complete (or -1 if it
* will run forever) into the variable answer.
*/
/*
* Please note that the answer may exceed the maximum value
* that can be stored in an "int" integer type.
* Because of this, you should use the "long long" integer type
* instead of "int" when computing your solution.
*/
/* Write the answer to the output file. */
if (answer == -1) {
fprintf(output_file, "FOREVER\n");
} else {
fprintf(output_file, "%lld\n", answer);
}
/* Finally, close the input/output files. */
fclose(input_file);
fclose(output_file);
return 0;
}