TL;DR: Here’s my solution: https://www.codechef.com/viewsolution/27595469 to https://www.codechef.com/problems/BOXGAM97

Since the Editorial is not out as I write this here’s how it works,

`def compare(a, b, P): if P: return a if a < b else b return a if a > b else b`

The `compare`

function takes three arguments `a,`

`b`

and `P`

. It returns what `P`

would pick out of `a`

and `b`

.

After the input taking part comes the three cases *tagged with comments in the solution*.

**Case 1:**

`target = min(A) if P else max(A) if A[1] == target or A[-2] == target: return target`

If a player gets the first chance he can always force the minimum or maximum if it is second or second last irrespective of whether he gets the last turn. Whether the number of turns, `K`

is even or odd i.e. whether the starting player gets the last turn, the second and second last values can always be forced, keeping the ball in the desired box if odd or the first or the last box respectively if even.

**Case 2:**

`if K % 2 == 1: return target`

If the number of turns, `K`

is odd the starting player gets the last chance and thus get what he wants, as he can always redo what the other player did.

**Case 3:**

`val = compare(A[1], A[-2], P) for i in range(1, len(A)-1): comp = compare(A[i-1], A[i+1], not P) val = compare(val, comp, P) return val`

If the number of turns, `K`

is even the other person gets the last chance thus the ball will land one box away from where the first player starts. This box can be predicted as the actions of the other player can be predicted. Since the second and the second last box can be forced we check if any of the possible landing boxes is better than what can be forced.

I know there are very few people who do competitive in python, I too am planning the move to C++ before graduating to Div_1 but are there any improvements I can make to my code? (Coding style or just simple logic changes?)

I messed it up during the competition case on line 19, I put `N`

instead of `K`

.