PivotOJ

Maxdifficent Group

시간 제한: 1000ms메모리 제한: 1024MB출처: ICPC Asia Jakarta Regional 2021BOJ 26862

문제

Given an array of integers A1..NA_{1..N} where N ≥ 2. Each element in A should be assigned into a group while satisfying the following rules.

  • Each element belongs to exactly one group.
  • If AiA_i and AjA_j where i<ji < j belongs to the same group, then AkA_k where i &le; k &le; j also belongs to the same group as AiA_i and AjA_j.
  • There is at least one pair of elements that belong to a different group.

Let GiG_i denotes the group ID of element AiA_i. The cost of a group is equal to the sum of all elements in AA that belong to that group.

cost(x)=i s.t. Gi=xAi\text{cost}(x) = \sum_{\text{i s.t. }G_i = x}{A_i}

Two different group IDs, GiG_i and GjG_j (where GiGjG_i \ne G_j), are adjacent if and only if GkG_k is either GiG_i or GjG_j for every i &le; k &le; j. Finally, the diff()\text{diff}() value of two group IDs xx and yy is defined as the absolute difference between cost(x)\text{cost}(x) and cost(y)\text{cost}(y).

\text{diff}(x, y) = |\text{cost}(x) &minus; \text{cost}(y)|

Your task in this problem is to find a group assignment such that the largest diff()\text{diff}() value between any pair of adjacent group IDs is maximized; you only need to output the largest diff()\text{diff}() value.

For example, let A_{1..4} = \{100, &minus;30, &minus;20, 70\}. There are 88 ways to assign each element in AA into a group in this example; some of them are shown as follows.

  • G1..4={1,2,3,4}G_{1..4} = \{1, 2, 3, 4\}. There are 33 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:
    • \text{diff}(1, 2) = |\text{cost}(1) &minus; \text{cost}(2)| = |(100) &minus; (&minus;30)| = 130,
    • \text{diff}(2, 3) = |\text{cost}(2) &minus; \text{cost}(3)| = |(&minus;30) &minus; (&minus;20)| = 10, and
    • \text{diff}(3, 4) = |\text{cost}(3) &minus; \text{cost}(4)| = |(&minus;20) &minus; (70)| = 90.
    • The largest diff()\text{diff}() value in this group assignment is 130130.
  • G1..4={1,2,2,3}G_{1..4} = \{1, 2, 2, 3\}. There are 22 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:
    • \text{diff}(1, 2) = |\text{cost}(1) &minus; \text{cost}(2)| = |(100) &minus; (&minus;30 + (&minus;20))| = 150, and
    • \text{diff}(2, 3) = |\text{cost}(2) &minus; \text{cost}(3)| = |(&minus;30 + (&minus;20)) &minus; (&minus;20)| = 70.
    • The largest diff()\text{diff}() value in this group assignment is 150150.

The other 66 group assignments are: G1..4={1,1,1,2}G_{1..4} = \{1, 1, 1, 2\}, G1..4={1,1,2,2}G_{1..4} = \{1, 1, 2, 2\}, G1..4={1,2,2,2}G_{1..4} = \{1, 2, 2, 2\}, G1..4={1,1,2,2}G_{1..4} = \{1, 1, 2, 2\}, G1..4={1,1,2,3}G_{1..4} = \{1, 1, 2, 3\}, and G1..4={1,2,3,3}G_{1..4} = \{1, 2, 3, 3\}. Among all possible group assignments in this example, the maximum largest diff()\text{diff}() that can be obtained is 150150 from the group assignment G1..4={1,2,2,3}G_{1..4} = \{1, 2, 2, 3\}.

입력

Input begins with a line containing an integer NN (2 &le; N &le; 100\,000) representing the number of elements in array AA. The next line contains NN integers AiA_i (-10^6 &le; A_i &le; 10^6) representing the array AA.

출력

Output contains an integer in a line representing the maximum possible largest diff()\text{diff}() that can be obtained from a group assignment.

예제

예제 1

입력
4
100 -30 -20 50
출력
150

예제 2

입력
5
12 7 4 32 9
출력
46

예제 3

입력
6
-5 10 -5 45 -20 15
출력
70
코드를 제출하려면 로그인하세요.