Maxdifficent Group

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

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

  • Each element belongs to exactly one group.
  • If A_iA\_i and A_jA\_j where i<ji < j belongs to the same group, then A_kA\_k where ikji ≤ k ≤ j also belongs to the same group as A_iA\_i and A_jA\_j.
  • There is at least one pair of elements that belong to a different group.

Let G_iG\_i denotes the group ID of element A_iA\_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. G_i=xA_i\text{cost}(x) = \sum\_{\text{i s.t. }G\_i = x}{A\_i}

Two different group IDs, G_iG\_i and G_jG\_j (where G_iG_jG\_i \ne G\_j), are adjacent if and only if G_kG\_k is either G_iG\_i or G_jG\_j for every ikji ≤ k ≤ 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).

diff(x,y)=cost(x)cost(y)\text{diff}(x, y) = |\text{cost}(x) − \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,30,20,70A\_{1..4} = \\{100, −30, −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.

  • G_1..4=1,2,3,4G\_{1..4} = \\{1, 2, 3, 4\\}. There are 33 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:

    • diff(1,2)=cost(1)cost(2)=(100)(30)=130\text{diff}(1, 2) = |\text{cost}(1) − \text{cost}(2)| = |(100) − (−30)| = 130,
    • diff(2,3)=cost(2)cost(3)=(30)(20)=10\text{diff}(2, 3) = |\text{cost}(2) − \text{cost}(3)| = |(−30) − (−20)| = 10, and
    • diff(3,4)=cost(3)cost(4)=(20)(70)=90\text{diff}(3, 4) = |\text{cost}(3) − \text{cost}(4)| = |(−20) − (70)| = 90.
    • The largest diff()\text{diff}() value in this group assignment is 130130.
  • G_1..4=1,2,2,3G\_{1..4} = \\{1, 2, 2, 3\\}. There are 22 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:

    • diff(1,2)=cost(1)cost(2)=(100)(30+(20))=150\text{diff}(1, 2) = |\text{cost}(1) − \text{cost}(2)| = |(100) − (−30 + (−20))| = 150, and
    • diff(2,3)=cost(2)cost(3)=(30+(20))(20)=70\text{diff}(2, 3) = |\text{cost}(2) − \text{cost}(3)| = |(−30 + (−20)) − (−20)| = 70.
    • The largest diff()\text{diff}() value in this group assignment is 150150.

The other 66 group assignments are: G_1..4=1,1,1,2G\_{1..4} = \\{1, 1, 1, 2\\}, G_1..4=1,1,2,2G\_{1..4} = \\{1, 1, 2, 2\\}, G_1..4=1,2,2,2G\_{1..4} = \\{1, 2, 2, 2\\}, G_1..4=1,1,2,2G\_{1..4} = \\{1, 1, 2, 2\\}, G_1..4=1,1,2,3G\_{1..4} = \\{1, 1, 2, 3\\}, and G_1..4=1,2,3,3G\_{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 G_1..4=1,2,2,3G\_{1..4} = \\{1, 2, 2, 3\\}.

입력

Input begins with a line containing an integer NN (2N100,0002 ≤ N ≤ 100\\,000) representing the number of elements in array AA. The next line contains NN integers A_iA\_i (106A_i106-10^6 ≤ A\_i ≤ 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.