Problem:
Score : 900 points

Problem Statement

Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N.
Here, A_i is an integer, and op_i is an binary operator either + or -.
Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula.
Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer.
It is allowed to insert any number of parentheses at a position.
Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.

Constraints

1≦N≦10^5

1≦A_i≦10^9

op_i is either + or -.

Input

The input is given from Standard Input in the following format:

N
A_1 op_1 A_2 ... op_{N-1} A_N

Output

Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.

Sample Input 1

3
5 - 1 - 3

Sample Output 1

7

The maximum possible value is: 5 - (1 - 3) = 7.

Sample Input 2

5
1 - 2 + 3 - 4 + 5

Sample Output 2

5

The maximum possible value is: 1 - (2 + 3 - 4) + 5 = 5.

Sample Input 3

5
1 - 20 - 13 + 14 - 5

Sample Output 3

13

The maximum possible value is: 1 - (20 - (13 + 14) - 5) = 13.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(dead_code)]
fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

#[allow(unused_macros)]
macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

#[allow(unused_macros)]
macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) } }
// macro_rules! debug { ($x: expr) => () }

#[allow(dead_code)]
fn show<T>(iter: T) -> String
where
    T: Iterator,
    T::Item: std::fmt::Display
{
    let mut res = iter.fold(String::new(), |sum, e| sum + &format!("{} ", e));
    res.pop();
    res
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BinaryHeap, HashMap, HashSet};
#[allow(unused_imports)]
use std::collections::btree_map::Entry::{Occupied, Vacant};

fn main() {
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    macro_rules! printf { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    macro_rules! printfln { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); }

    let n = readl!(usize);
    let s = readlvec!(String);
    let mut a = vec![0; n];
    a[0] = s[0].parse().unwrap();
    let mut sign = vec![false; n-1];
    for i in 0..n-1 {
        sign[i] = s[i*2+1] == "-";
        if sign[i] {
            a[i+1] = -s[i*2+2].parse::<i64>().unwrap();
        } else {
            a[i+1] = s[i*2+2].parse().unwrap();
        }
    }
    let a = a;
    
    // debug!(sign);
    // let mut dp = vec![[0;4]; n+1];
    // dp[0][1] = -1_000_000_000_000_000;
    let inf = 1_000_000_000_000_000;
    let mut dp = [-inf;3];
    dp[0] = 0;
    for i in 0..n {
        let mut nex = [-inf; 3];
        {
            let nv = dp[0] + a[i];
            nex[0] = max(nex[0], nv);
            if i >= 1 && sign[i-1] {
                nex[1] = max(nex[1], nv);
            }
        }
        {
            let nv = dp[1] - a[i];
            nex[1] = max(nex[1], nv);
            nex[0] = max(nex[0], nv);
            if i >= 1 && sign[i-1] {
                nex[2] = max(nex[2], nv);
            }
        }
        {
            let nv = dp[2] + a[i].abs();
            nex[2] = max(nex[2], nv);
            nex[0] = max(nex[0], nv);
        }
        dp = nex;
        // dp[i+1] = dp[i];
        // dp[i+1][0] += a[i];
        // dp[i+1][1] -= a[i];
        // dp[i+1][0] = max(dp[i+1][0], dp[i][1]+a[i]);
        // if i >= 2 && sign[i-2] {
        //     debug!(i);
        //     dp[i+1][1] = max(dp[i+1][1], dp[i][0]-a[i]);
        // }
    }
    printfln!("{}", max(dp[0], max(dp[1], dp[2])));
    // debug!(dp);
    // println!("{}", max(dp[n][0], dp[n][1]));


}