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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

struct UnionFind {
    parent: Vec<usize>,
    rank: Vec<usize>,
    size: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let mut p = vec![0; n];
        for i in 0..n {
            p[i] = i;
        }
        return UnionFind {
            parent: p,
            rank: vec![0; n],
            size: vec![1; n],
        };
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.parent[x] {
            x
        } else {
            let p = self.parent[x];
            let pr = self.find(p);
            self.parent[x] = pr;
            pr
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    fn unite(&mut self, a: usize, b: usize) {
        let a_root = self.find(a);
        let b_root = self.find(b);
        if self.rank[a_root] > self.rank[b_root] {
            self.parent[b_root] = a_root;
            self.size[a_root] += self.size[b_root];
        } else {
            self.parent[a_root] = b_root;
            self.size[b_root] += self.size[a_root];
            if self.rank[a_root] == self.rank[b_root] {
                self.rank[b_root] += 1;
            }
        }
    }

    fn get_size(&mut self, x: usize) -> usize {
        let root = self.find(x);
        self.size[root]
    }
}

fn binsearch(v: &Vec<usize>, n: usize) -> (Option<usize>, Option<usize>) {
    let mut lower = 0;
    let mut upper = v.len() - 1;
    if n <= v[0] {
        return (None, Some(v[0]));
    } else if v[v.len() - 1] <= n {
        return (Some(v[v.len() - 1]), None);
    }

    while lower + 1 != upper {
        let mid = (lower + upper) / 2;
        if n <= v[mid] {
            upper = mid;
        } else {
            lower = mid;
        }
    }
    return (Some(v[lower]), Some(v[upper]));
}

fn gcd(x: usize, y: usize) -> usize {
    if x > y {
        gcd(y, x)
    } else if x == 0 {
        y
    } else if y % x == 0 {
        x
    } else {
        gcd(x, y % x)
    }
}

pub struct RevHeap {
    pub h: BinaryHeap<isize>,
}

impl RevHeap {
    fn new() -> RevHeap {
        RevHeap {
            h: BinaryHeap::new(),
        }
    }
    fn push(&mut self, n: isize) {
        self.h.push(-1 * n);
    }

    fn pop(&mut self) -> Option<isize> {
        if let Some(n) = self.h.pop() {
            Some(-1 * n)
        } else {
            None
        }
    }

    fn print(&self) {
        println!("{:?}", self.h);
    }
}

use std::collections::BinaryHeap;

fn main() {
    input! {
        n: usize,
        mut a: [isize; n],
    }

    let neg_count = a.iter().fold(0, |acc, &x| acc + if x < 0 { 1 } else { 0 });
    if let Some(_) = a.iter().find(|&&x| x == 0) {
        println!("{}", a.iter().fold(0, |acc, &x| acc + x.abs()));
        return;
    }

    if neg_count % 2 == 0 {
        println!("{}", a.iter().fold(0, |acc, &x| acc + x.abs()));
        return;
    }

    let mut a: Vec<isize> = a.iter().map(|x| x.abs()).collect();
    a.sort();
    println!("{}", a.iter().fold(0, |acc, &x| acc + x) - a[0] * 2);
}