There is an array A of length N, where N is even. In one operation, you can partition the array into two (non-empty) parts and reverse those two parts. More formally, we choose an index i (1≤i<N) then we change the array into [Ai,Ai−1,…,A1,AN,AN−1,…,Ai+1].
Define the cost of the array as N∑i=1(−1)i+1Ai=A1−A2+A3−A4+…−AN.
Find the maximum cost of the array after some (possibly zero) operations.
N A1 A2 … AN
An integer representing the maximum cost.
2
123 456
-333
4
-1 1 -1 1
4
In the first sample, A=[123,456]. Since N=2, the only valid operation is using i=1, but after performing this operation, the array is unchanged. So the maximum cost of the array is 123−456=−333.
In the second sample, A=[−1,1,−1,1]. After performing an operation with i=2, the array becomes [1,−1,1,−1]. The final cost of the array is 1−(−1)+1−(−1)=4 and we can show that this is the maximum possible cost.
Subtask | Score |
---|---|
1 | 100 |