#include #include #include #include #include using namespace std; int program[5]; int programIndex = 0; bool rising; bool solutionFound = false; bool forceMaximum = false; bool forceMaximumInt = 0; int history(int now, int back) { int a = now - back; if (a < 0) { a += 5; } return program[a]; } void addToProgram(int novy) { program[programIndex] = novy; programIndex++; if (programIndex == 5) { programIndex = 0; } } int main() { int prgLength; while (scanf("%d", &prgLength) == 1) { solutionFound = false; rising = false; programIndex = 0; if (prgLength < 4) { int throwAway; printf("OK\n"); for (int i = 0; i = program[1]) { printf("3\n"); int throwAway; for (int i = 4; i < prgLength; i++) { scanf("%d", &throwAway); } continue; } } else { rising = true; if (program[3] < program[1]) { rising = false; } else if (program[3] == program[1]) { rising = false; forceMaximum = true; forceMaximumInt = program[2] - program[0]; } } // main progress programIndex = 4; for (int fullIndex = 4; fullIndex < prgLength; fullIndex++) { int novy; scanf("%d", &novy); if (forceMaximum) { forceMaximum = false; if (novy >= forceMaximumInt) { printf("%d\n", fullIndex); solutionFound = true; continue; } } if (solutionFound) { continue; } if (rising) { if (novy > history(programIndex, 2)) { // printf("WAS HERE %d %d %d\n", fullIndex, novy, history(programIndex, 2)); addToProgram(novy); } else { rising = false; // printf("DEBUG: FALL AT %d\n", fullIndex); int difference = history(programIndex, 2) - history(programIndex, 4); if (novy < difference) { rising = false; } else { int nahled; bool hasNext = (fullIndex + 1 < prgLength); if (!hasNext) { // printf("DEBUG: HAS NO NEXT: fullindex %d\n", fullIndex); solutionFound = true; printf("OK\n"); break; } scanf("%d", &nahled); int barrier = history(programIndex, 1) - history(programIndex, 3); // int barrier = history(programIndex, 4); addToProgram(novy); addToProgram(nahled); fullIndex++; if (nahled < barrier) { rising = false; } else { printf("%d\n", fullIndex); solutionFound = true; continue; } } } } else { if (novy >= history(programIndex, 2)) { printf("%d\n", fullIndex); solutionFound = true; continue; } else { addToProgram(novy); } } } if (!solutionFound) { printf("OK\n"); } } }