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: Wrong Answer)


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.IntStream;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().solve();
	}

	private void solve() throws IOException {
		try {
			//			solveA();
			//			solveB();
			//			solveC();
			solveD();
			//			 solveE();
			//			 solveF();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.flush();
				out.close();
			}
		}

	}

	private void solveA() {
		int a = nextInt();
		int b = nextInt();
		int t = nextInt();

		int res = 0;

		double time = 0;
		while (time <= t + 0.5) {
			time += a;
			if (time <= t + 0.5) {
				res += b;
			}
		}

		out.println(res);
	}

	private void solveB() {
		int numN = nextInt();
		int[] v = IntStream.range(0, numN).map(i -> nextInt()).toArray();
		int[] x = IntStream.range(0, numN).map(i -> nextInt()).toArray();

		long res = 0;
		for (int i = 0; i < numN; i++) {
			if (v[i] > x[i]) {
				res += (v[i] - x[i]);
			}
		}

		out.println(res);
	}

	/*
	 * 想定漏れパターン
	 * 4
	 * 1 3 6 9
	 * -> 3(1->3)
	 *
	 * 3
	 * 7 6 8
	 * -> 2(7->6 or 8)
	 */
	private void solveC() {
		int numN = nextInt();
		List<Integer> oddL = new ArrayList<Integer>();
		List<Integer> eveL = new ArrayList<Integer>();
		int[] wk = new int[numN];
		for (int i = 0; i < numN; i++) {
			int val = nextInt();
			if (val % 2 == 0) {
				eveL.add(val);
			} else {
				oddL.add(val);
			}
			wk[i] = val;
		}
		Collections.sort(eveL);
		Collections.sort(oddL);
		Arrays.sort(wk);

		//		/*
		//		 * 1種類しかないのであれば、最小を最大にしたほうが良い
		//		 */
		//		if (eveL.size() == 0 || oddL.size() == 0) {
		//			wk[0] = wk[wk.length - 1];
		//			long res = wk[wk.length - 1];
		//			for (int i = 0; i < wk.length; i++) {
		//				res = maximumKouyaku2Args(res, wk[i]);
		//			}
		//			out.println(res);
		//			return;
		//		}
		//
		//		/*
		//		 * 偶奇が混じっている（両方とも2つ以上）のであれば、
		//		 * 公約数は1になる
		//		 */
		//		if (eveL.size() > 1 && oddL.size() > 1) {
		//			out.println(1);
		//			return;
		//		}
		//
		//		/*
		//		 * 偶奇どちらが多い？
		//		 */
		//		boolean isEq = oddL.size() < eveL.size();
		//		List<Integer> tmp = isEq ? eveL : oddL;
		//
		//		/*
		//		 * 公約数を求めていく
		//		 * 初期値は何でもよい
		//		 */
		//		long res = tmp.get(tmp.size() - 1);
		//		for (int i = 1; i < tmp.size(); i++) {
		//			res = maximumKouyaku2Args(res, tmp.get(i));
		//		}
		//
		//		long res1 = tmp.get(tmp.size() - 1);
		//		for (int i = 0; i < tmp.size(); i++) {
		//			res1 = maximumKouyaku2Args(res1, tmp.get(i));
		//		}
		//		out.println(Long.max(res, res1));
	}

	private long maximumKouyaku2Args(long num1, long num2) {
		long max = Long.max(num1, num2);
		long min = Long.min(num1, num2);
		long amari = max % min;

		while (amari != 0) {
			max = min;
			min = amari;
			amari = max % min;
		}
		return min;

	}

	private void solveD() {
		int numN = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();

		long[] dp = new long[numN + 2];
		for (int i = 0; i < numN - 1; i++) {
			long val1 = wk[i] + wk[i + 1];
			long val2 = (wk[i] * -1) + (wk[i + 1] * -1);
			if (val2 + dp[i + 2] > val1 + dp[i + 2]) {
				wk[i] *= -1;
				wk[i + 1] *= -1;
			}
			dp[i + 2] = Long.max(val2 + dp[i + 2], val1 + dp[i + 2]);
		}

		long res = 0;
		for (int i = 0; i < dp.length; i++) {
			res += dp[i];
		}
		out.println(res);

	}

	private void solveE() {
		int numN = nextInt();

		out.println("");
	}

	private void solveF() {
		int numN = nextInt();

		out.println("");
	}

	private final PrintWriter out = new PrintWriter(System.out);
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
}