Problem:
Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(final String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final int number = scanner.nextInt();
        final long[] longs = new long[number];

        for (int i = 0; i < longs.length; i++) {
            longs[i] = scanner.nextLong();
        }

        final long[][] dpTable = new long[4][number];

        /*
        0 直前，今回ともに操作なし
        1 直前に操作なし，今回は操作あり
        2 直前も今回も操作あり
        3 直前に操作あり，今回は操作なし
         */

        dpTable[0][0] = longs[0];
        dpTable[1][0] = -longs[0];
        dpTable[2][0] = -longs[0];
        dpTable[3][0] = longs[0];

        for (int i = 0; i < number - 2; i++) {
            dpTable[0][i + 1] = Math.max(dpTable[0][i] + longs[i + 1], dpTable[3][i] + longs[i + 1]);
            dpTable[1][i + 1] = Math.max(dpTable[0][i] - longs[i + 1], dpTable[3][i] - longs[i + 1]);
            dpTable[2][i + 1] = Math.max(dpTable[1][i] + longs[i + 1], dpTable[2][i] + longs[i + 1]);
            dpTable[3][i + 1] = Math.max(dpTable[1][i] - longs[i + 1], dpTable[2][i] - longs[i + 1]);
        }

        dpTable[0][number - 1] = dpTable[0][number - 2] + longs[number - 1];
        dpTable[1][number - 1] = dpTable[1][number - 2] - longs[number - 1];
        dpTable[2][number - 1] = dpTable[2][number - 2] - longs[number - 1];
        dpTable[3][number - 1] = dpTable[3][number - 2] + longs[number - 1];

        System.out.println(Math.max(Math.max(dpTable[0][number - 1], dpTable[1][number - 1]), Math.max(dpTable[2][number - 1], dpTable[3][number - 1])));
    }
}