아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Maxdifficent Group

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

요약
배열을 두 개 이상의 연속한 그룹으로 나눌 때, 인접한 두 그룹 합의 차이의 최댓값을 가장 크게 만드는 값을 구한다.
난이도

보통10점 중 7점

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

문제

Given an array of integers A_1..NA\_{1..N} where N≥2N ≥ 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 i≤k≤ji ≤ 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_i≠G_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 i≤k≤ji ≤ 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 (2≤N≤100,0002 ≤ N ≤ 100\\,000) representing the number of elements in array AA. The next line contains NN integers A_iA\_i (−106≤A_i≤106-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.

예제3

  1. 예제 1

    입력
    4
    100 -30 -20 50
    
    예상 출력
    150
    
  2. 예제 2

    입력
    5
    12 7 4 32 9
    
    예상 출력
    46
    
  3. 예제 3

    입력
    6
    -5 10 -5 45 -20 15
    
    예상 출력
    70