Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author: r3mark
Observe that the final marks should be a non-decreasing sequence. (The observation was not true for the original problem, which was incorrect as a result.) This is because it never hurts Bob to swap marks
and
if
and
, since the
array is non-decreasing.
With this observation, a dynamic programming solution can be found for the first subtask. Let
be the most amount of money given that only the first
tests are being considered,
, and
. Then the transition can be done in
by considering the length of marks equal to exactly
. The rest of the marks will definitely be less than
and will contribute to the money earned.
Time Complexity: 
For the second subtask, a similar approach is used. Instead of considering the tests from
to
, this dynamic programming solution considers the tests from
to
. The advantage here is the dimension
in the first subtask is no longer necessary, as
should always be equal to
. So let
be the most amount of money given that only the last
tests are being considered and
. If
, that is equivalent to adding
to all the marks in the transition. So
![\displaystyle dp[i][e] = \min_{j>i} dp[j+1][e-(N-j)] + (j-i+1) \cdot (v_j + v_{j+1} + \dots + v_N)](//static.dmoj.ca/mathoid/5e4b59651aac115279c7b3f15078178530c1e8b7/svg)
Then the transition can be done in
.
Time Complexity: 
For the final subtask, note that the convex hull optimization can be applied to the dynamic programming solution from the second subtask. This changes the transition into amortized
.
Time Complexity: 
Comments