Given an array of integers A_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 A_i and A_j where i<j belongs to the same group, then A_k where i≤k≤j also belongs to the same group as A_i and A_j.
- There is at least one pair of elements that belong to a different group.
Let G_i denotes the group ID of element A_i. The cost of a group is equal to the sum of all elements in A that belong to that group.
cost(x)=∑_i s.t. G_i=xA_i
Two different group IDs, G_i and G_j (where G_i=G_j), are adjacent if and only if G_k is either G_i or G_j for every i≤k≤j. Finally, the diff() value of two group IDs x and y is defined as the absolute difference between cost(x) and cost(y).
diff(x,y)=∣cost(x)−cost(y)∣
Your task in this problem is to find a group assignment such that the largest diff() value between any pair of adjacent group IDs is maximized; you only need to output the largest diff() value.
For example, let A_1..4=100,−30,−20,70. There are 8 ways to assign each element in A into a group in this example; some of them are shown as follows.
-
G_1..4=1,2,3,4. There are 3 pairs of group IDs that are adjacent and their diff() values are:
- diff(1,2)=∣cost(1)−cost(2)∣=∣(100)−(−30)∣=130,
- diff(2,3)=∣cost(2)−cost(3)∣=∣(−30)−(−20)∣=10, and
- diff(3,4)=∣cost(3)−cost(4)∣=∣(−20)−(70)∣=90.
- The largest diff() value in this group assignment is 130.
-
G_1..4=1,2,2,3. There are 2 pairs of group IDs that are adjacent and their diff() values are:
- diff(1,2)=∣cost(1)−cost(2)∣=∣(100)−(−30+(−20))∣=150, and
- diff(2,3)=∣cost(2)−cost(3)∣=∣(−30+(−20))−(−20)∣=70.
- The largest diff() value in this group assignment is 150.
The other 6 group assignments are: G_1..4=1,1,1,2, G_1..4=1,1,2,2, G_1..4=1,2,2,2, G_1..4=1,1,2,2, G_1..4=1,1,2,3, and G_1..4=1,2,3,3. Among all possible group assignments in this example, the maximum largest diff() that can be obtained is 150 from the group assignment G_1..4=1,2,2,3.