Compute the optimal score you can guarantee in a two-player sorted-container game.
Given an integer N, an array F of size N and Q queries, each with integers G and R. G sets the game type: G = 0 means you must move if a valid move exists on your turn; G = 1 means you may skip your turn any number of times. R sets the range: only F[0] to F[R-1] participate.
You and a friend alternate turns and you play first. On your turn you select any element from the current range, remove it from F and add it to a container C, allowed only if C remains sorted in non-decreasing order. On your friend's turn they remove any one element from the range; your friend plays optimally to minimize your final score. If G = 0 and you have no legal element to add on your turn, you lose and your score is 0. The game continues until no elements remain in F[0..R-1]; if you get through to the end, your score is the sum of the elements in C. You maximize your score, the friend minimizes it.
For every query (G, R), print the optimal score you can guarantee on its own line.
Input: N Q, then F[0..N-1], then Q lines of G R. Example: input '6 1 / 10 20 30 40 50 60 / 0 3' gives output 30.
Format · Online assessment.
Community answers
Related courses


