luogu#P7917. [Kubic] Addition

[Kubic] Addition

Background

It is recommended to read the background of Problem B first.

Problem Description

There is an initial sequence aa of length nn. You need to perform n1n - 1 operations. In each operation, you first choose two adjacent numbers x,yx, y in the current sequence and delete them (xx is to the left of yy in the original sequence), then insert either x+yx + y or xyx - y at the original position. After n1n - 1 operations, exactly one number will remain. Find the maximum possible value of this remaining number.

Input Format

The first line contains an integer nn.

The second line contains nn integers, where the ii-th integer represents aia_i.

Output Format

Output a single line containing one integer, which is the answer.

5
-1 1 1 -1 1
3

Hint

For 100%100\% of the testdata, 1n105,ai1091 \le n \le 10^5, |a_i| \le 10^9.

| |Score|nn|ai|a_i||Special Property| |:-:|:-:|:-:|:-:|:-:| |Subtask1\operatorname{Subtask}1|1010|2\le 2|No special restrictions|None| |Subtask2\operatorname{Subtask}2|2020|100\le 100|No special restrictions|None| |Subtask3\operatorname{Subtask}3|55|No special restrictions|No special restrictions|ai0a_i \ge 0| |Subtask4\operatorname{Subtask}4|3030|No special restrictions|1\le 1|None| |Subtask5\operatorname{Subtask}5|3535|No special restrictions|No special restrictions|None|

Sample Explanation

One possible sequence of operations is as follows:

-1 1 1 -1 1

-1 1 1 -2

-1 1 3

-1 4

3

It can be proven that there is no better plan.

Input Format

Output Format

Translated by ChatGPT 5