# COLLAPSEE - Editorial

**URL:** <https://discuss.codechef.com/t/collapsee-editorial/126094>\
**Category:** editorial\
**Tags:** editorial, start262\
**Created:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/collapsee-editorial/126094 "2026-10-28T17:30:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 28, 2026, 5:30pm UTC](https://discuss.codechef.com/t/collapsee-editorial/126094/1 "2026-10-28T17:30:19Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/COLLAPSEE)  
[Contest: Division 1](https://www.codechef.com/START262A/problems/COLLAPSEE)  
[Contest: Division 2](https://www.codechef.com/START262B/problems/COLLAPSEE)  
[Contest: Division 3](https://www.codechef.com/START262C/problems/COLLAPSEE)  
[Contest: Division 4](https://www.codechef.com/START262D/problems/COLLAPSEE)

_ **Author:** _ [nikhil\_010309](https://www.codechef.com/users/nikhil_010309)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/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)**
>
> ```python
> 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)))
> 
> ```
