Business Magic

면접 대비

시간 제한2초메모리 제한1024 MB

요약
하나의 구간을 골라 값을 두 배로 만들고 그 구간 밖의 매장은 원하면 부호를 바꿔, 만들 수 있는 최대 총합을 구한다.
난이도

보통10점 중 5점

유형
동적 계획법, 배열, 누적 합, 그리디
정답자
아직 제출이 없습니다

문제

There are nn stores located along a street, numbered from 11 to nn from nearest to farthest. Last month, the store kk had a net profit of r_kr\_k. If r_kr\_k is positive, it represents a proft of r_kr\_k dollars; if r_kr\_k is negative, it represents a loss of −r_k-r\_k dollars.

As a master of business magic, you have two types of spells at your disposal that you can use to alter the net profts of these stores for the next month:

  1. Blue Magic: You can choose a single continuous interval \[L,R]\[L, R]. The effect of this spell will be to double the net profit of every store from store LL to store RR (inclusive) for the next month. That is, if k∈\[L,R]k \in \[L, R], then store kk will have net profit 2r_k2r\_k next month.
  2. Green Magic: You can choose any store and cast the green magic on it. The effect of the green magic is to change the next month’s net profit of that store to the negative of its last month’s net profit.

Any store that has not been affected by either spell will have the same net profit next month as it did last month.

However, there are some restrictions when casting spells. You can only cast the blue magic once and it must be used before the green magic. Additionally, the green magic cannot be cast on any store that has already been affected by the blue magic. Your task is to determine the maximum possible sum of the net profits for all stores for the next month after casting your spells optimally.

입력

The first line contains an integer nn, the number of stores. The second line contains nn space-separated integers r_1,r_2,…,r_nr\_1, r\_2, \dots ,r\_n, where r_kr\_k is the net profit of store kk last month.

출력

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

제한

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

예제3

  1. 예제 1

    입력
    5
    -2 5 -3 4 -1
    
    예상 출력
    20
    
  2. 예제 2

    입력
    7
    -1 -1 -1 -1 -1 -1 -1
    
    예상 출력
    7
    
  3. 예제 3

    입력
    4
    998244353 864197532 -7 1000000000
    
    예상 출력
    5724883756