COLLAPSEE - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: nikhil_010309
Tester: raysh07
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

You are given an array A of length N.
You can choose two adjacent elements A_i and A_{i+1}, delete both, and insert A_{i+1} - A_i in their place.

Find the maximum possible final value.

EXPLANATION:

Each operation ‘merges’ two adjacent elements.
So, after performing several operations, each remaining element will represent some subarray of the original array; with its elements merged together in some way.
These subarrays also partition the original array.

Now, suppose we have two subarrays [l, r] and [a, b] that have both been merged into a single element, and we then perform an operation that merges these two elements (where a = r+1 so the elements are adjacent).
Then, note that the ‘contribution’ of every element in [l, r] simply gets negated, while the contribution of every element in [a, b] remains the same.

So, when merging a subarray into a single element, it can be seen that the contribution of each element of the subarray is pretty much its sign, i.e. A_i contributes either +A_i or -A_i.

The question now is, which combinations of signs are valid?


To answer that, let’s look at a subarray [l, r] which has been merged into one value.

Every element initially starts with a positive sign (before any merges.)
Further, the element A_r will always be on the right side of a merge; and so will never have its sign negated. Thus, it will always contribute A_r (and never -A_r).

Next, look at element A_{r-1}.
In any merge involving it, it will almost always be on the right side. The only exception is when it is merged into A_r, in which case it will be on the left.
Since it’ll be on the left exactly once, its sign will be flipped; and thus it always contributes -A_{r-1}.

What about the remaining elements, as in A_i for l \le i \le r-2?
Well, for those it turns out we have complete freedom!
That is, we can choose either +A_i or -A_i for each one, and no matter what configuration we choose is attainable!

Proof

The proof is fairly straightforward.

First, consider always merging the last two elements.
This will give everything a -1 sign, except for A_r which has +1; because every element will be on the left side of a merge exactly once.

Now, for any l \le i \le r-2 that we want to end up with a positive sign, simply merge it with the element to its right once at the very beginning.
Once that’s done, do the reverse-pass merge mentioned above, and note that:

  • Every element that was initially merged to its right, will be on the left side of a merge twice in total; and hence have sign +1.
  • Everything else will have sign -1, except A_r which always has +1.

This is what we wanted, so we’re done.


With that in mind, let’s look at the final element, which comes from merging the range [1, N] into a single element.

A_N will always contribute +A_N, and A_{N-1} will always contribute -A_{N-1}.
For every other element, we are able to choose its sign freely.
Since the objective is to maximize the final value, clearly we just choose \max(A_i, -A_i) for each one.
Note that this expression reduces to just |A_i|.

So, the final answer is simply

A_N - A_{N-1} + \sum_{i=1}^{N-2} |A_i|

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()))
    
    print(a[n-1] - a[n-2] + sum(abs(a[i]) for i in range(n-2)))

1 Like