Under construction

Still being built. Things may change

Business Magic

Interview

Time limit2sMemory limit1024 MB

Summary
Pick one interval to double, and negate each negative store outside it, to maximize the total profit for next month.
Level

Medium5 of 10

Topics
Array, Dynamic programming
Solved
No attempts yet

Problem

There are nn stores along a street, numbered from 11 to nn from nearest to farthest. Last month, store kk had a net profit of rkr_k. If rkr_k is positive, it is a profit of rkr_k dollars. If rkr_k is negative, it is a loss of −rk-r_k dollars.

You have two kinds of spells that change the net profits for next month.

  1. Blue Magic: Choose one continuous interval [L,R][L, R]. The net profit of every store from LL to RR (inclusive) is doubled. That is, if k∈[L,R]k \in [L, R], store kk has net profit 2rk2r_k next month.
  2. Green Magic: Choose any store and cast the spell on it. The store's net profit for next month becomes the negative of its net profit last month.

A store that is not affected by either spell has the same net profit next month as last month.

There are restrictions. You can cast Blue Magic only once, and it must come before Green Magic. Green Magic cannot be cast on any store already affected by Blue Magic. Find the maximum possible total net profit of all stores for next month after casting the spells optimally.

Input

The first line contains nn, the number of stores. The second line contains nn space-separated integers r1,r2,…,rnr_1, r_2, \dots, r_n, where rkr_k is the net profit of store kk last month.

Output

Output a single integer, the maximum possible total net profit of all stores for next month.

Constraints

  • 1≤n≤3×1051 \le n \le 3 \times 10^5
  • −109≤rk≤109-10^9 \le r_k \le 10^9 for k∈{1,2,…,n}k \in \{1, 2, \dots, n\}

Examples3

  1. Example 1

    Input
    5
    -2 5 -3 4 -1
    
    Expected output
    20
    
  2. Example 2

    Input
    7
    -1 -1 -1 -1 -1 -1 -1
    
    Expected output
    7
    
  3. Example 3

    Input
    4
    998244353 864197532 -7 1000000000
    
    Expected output
    5724883756