You are using the straightforward approach, which has complexity \mathcal{O}(n^2) because you are calculating the answer for all n rotations going over n elements for each. The problem can be solved in \mathcal{O}(n) with some clever thinking that will fetch you 100 points. However it requires a different approach from your current way of tackling it. Give it some thought. If you are still unable to solve it I will be happy to explain the solution

**EDIT**:

Assume the first row is 72314 (no particular significance). Let’s divide a rotation of the sequence 1234…n into 2 pieces, [start value to n] and [1 to end value]. When n=5, all rotations are shown below with `|`

symbolizing the split between the pieces.

` . [7 2 3 1 4] 1 2 3 4 5 8 4 6 5 9 2 3 4 5| 1 9 5 7 6| 5 3 4 5| 1 2 10 6 8| 2 6 4 5| 1 2 3 11 7| 4 3 7 5| 1 2 3 4 12| 3 5 4 8`

Now you can observe observe that each value before the split in the r^{th} rotation is just **(r+the value at that index in the 0^{th} rotation)**. And each value after the split in any rotation is **(r-N+the value at that index in the 0^{th} rotation)**. Let {max}_r(i, j) denote the maximum element from indices i to j in the r^{th} rotation. Then it is follows that **{max}_r(0, i)** equals **r+{max}_0(0, i)** for **i<N-r**. This condition refers to all values of i before the split. And **{max}_r(j, N-1)** equals **r-N+{max}_0(j, N-1)** for all **j≥N-r**. As you can guess, this is for all values after the split. You can verify these properties using the above example.

For each rotation r, we need to find the maximum value before the split, and the maximum value after the split, which are **{max}_r(0, N-r-1)** and **{max}_r(N-r, N-1)** respectively. The greater of these two is the overall answer for the rotation. From what we deduced earlier, **{max}_r(0, N-r-1) = r+{max}_0(0, N-r-1)** and **{max}_r(N-r, N-1) = r-N+{max}_0(N-r, N-1)**. So can we get values like **{max}_0(0, i)** and **{max}_0(j, N-1)** in constant time? Yes we can, using prefix and suffix maximum arrays. Each of these can be calculated beforehand in \mathcal{O}(n).

Complexity is \mathcal{O}(n)

Pseudocode here.

AC submission here.