| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 57 | 49 | 43 | 84.314% |
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.
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.
$$\text{cost}(x) = \sum_{\text{i s.t. }G_i = x}{A_i}$$
Two different group IDs, $G_i$ and $G_j$ (where $G_i \ne G_j$), are adjacent if and only if $G_k$ is either $G_i$ or $G_j$ for every $i ≤ k ≤ j$. Finally, the $\text{diff}()$ value of two group IDs $x$ and $y$ is defined as the absolute difference between $\text{cost}(x)$ and $\text{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 $\text{diff}()$ value between any pair of adjacent group IDs is maximized; you only need to output the largest $\text{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.
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 $\text{diff}()$ that can be obtained is 150ドル$ from the group assignment $G_{1..4} = \{1, 2, 2, 3\}$.
Input begins with a line containing an integer $N$ (2ドル ≤ N ≤ 100,000円$) representing the number of elements in array $A$. The next line contains $N$ integers $A_i$ ($-10^6 ≤ A_i ≤ 10^6$) representing the array $A$.
Output contains an integer in a line representing the maximum possible largest $\text{diff}()$ that can be obtained from a group assignment.
4 100 -30 -20 50
150
This is the example from the problem statement.
5 12 7 4 32 9
46
The maximum possible largest $\text{diff}()$ of 45ドル$ can be obtained from the group assignment $G_{1..5} = \{1, 1, 1, 1, 2\}$. The $\text{diff}()$ value of the only adjacent group IDs is: $\text{diff}(1, 2) = 45$.
6 -5 10 -5 45 -20 15
70
The maximum possible largest $\text{diff}()$ of 70ドル$ can be obtained from the group assignment $G_{1..6} = \{1, 2, 2, 2, 3, 4\}$. The $\text{diff}()$ values of any two adjacent group IDs are: $\text{diff}(1, 2) = 55,ドル $\text{diff}(2, 3) = 70,ドル and $\text{diff}(3, 4) = 35$.