PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author:
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Simple
PREREQUISITES:
None
PROBLEM:
There are N slimes in a row, the i-th of which has power A_i. You must do the following N-1 times:
- Choose two adjacent slimes and make one of them eat the other.
If a slime with power x eats a slime with power y, its new power is \max(0, x-y).
There will be a single slime remaining in the end. Find the maximum possible power of this slime.
EXPLANATION:
Suppose slime i is the one remaining in the end.
Note that it’s impossible to increase the power of slime i: if it ever eats another slime, its strength can only either decrease or remain the same.
Now, since slime i is remaining, it must eat at least one slime to its left (i.e. with index \lt i) and one slime to its right (with index \gt i).
This is because slimes can only eat adjacent slimes, so it’s impossible for any slime that’s \lt i to eat something that’s \gt i by “crossing” i.
Ideally, slime i shouldn’t eat another slime with a high power, so that its own power doesn’t decrease much.
The best we can do is, of course, to try and make slime i eat another slime with power 0.
In fact, this is almost always possible!
In particular, it can be seen that:
- If i \geq 3, we can always ensure that slime i eats exactly one slime with power 0 to its left.
- If i \leq N-2, we can always ensure that slime i eats exactly one slime with power 0 to its right.
Proof
This is quite simple to prove.
Suppose i \geq 3 (the i \leq N-2 case is analogous).
Let m be the index of the smallest value among [A_1, A_2, \ldots, A_{i-1}].First, make slime m eat one of its neighbors (other than i, of course).
Note that after this, slime m will have a power of 0.
Now, simply make slime m eat all the other slimes, and its power will remain 0. At the end of this, slime i can eat slime m and A_i won’t decrease, as desired.
This leaves only i = 1, 2 and i = N-1, N as edgecases (the left side for the former two, the right side for the latter).
However, it’s obvious what to do in these cases:
- If i = 1, there’s nothing to eat to the left anyway. i = N with the right is similar.
- If i = 2, the slime is forced to eat slime 1 (with power A_1). The same applies to i = N-1 being forced to eat slime N.
With these observations, we can now easily check for each slime i what its maximum power can be if it’s the last slime remaining.
- If i = 2, set A_i to \max(0, A_i - A_1), otherwise nothing needs to be done for the left side.
- If i = N-1, set A_i to \max(0, A_i - A_N), otherwise nothing needs to be done for the right side.
Print the maximum of all of these.
TIME COMPLEXITY:
\mathcal{O}(N) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
l, r = 0, 0
if i == 1: l = a[0]
if i == n-2: r = a[-1]
ans = max(ans, max(0, a[i] - l - r))
print(ans)