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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;

public class CSharp{
    public static void Main(){
        Kyopuro chokudai = new Kyopuro();
	    chokudai.AtCoder();
	}
}

public class Kyopuro{
    
    int n;
    int[] a;
    long[] sum;
    List<long> b;
    long ans;
    
    public void AtCoder(){
        n = int.Parse(Console.ReadLine());
        a = new int[n];
        string[] s = Console.ReadLine().Split(' ');
        ans = long.Parse(s[0]);
        int sign;
        for(int i=1; i<n; i++){
            sign = 1;
            if(s[i*2-1] == "-") sign = -1;
            a[i] = int.Parse(s[i*2]) * sign;
        }
        
        long tmp;
        b = new List<long>();
        int c = 1;
        while(c<n && a[c]>0) ans += a[c++];
        if(c<n) ans += a[c++];
        while(c<n){
            tmp = 0;
            if(a[c]<0){
                //while(c<n && a[c]<0) tmp -= a[c++];
                //while(c<n && a[c]>0) tmp += a[c++];
                while(c<n) tmp += Math.Abs(a[c++]);
                ans += tmp;
                b.Add(0);
            } else {
                while(c<n && a[c]>0) tmp += a[c++];
                b.Add(tmp);
            }
            if(c<n) b.Add(a[c++]);
            else b.Add(0);
        }
        
        c = 0;
        while(c + 1 < b.Count){
            tmp = 0;
            tmp += b[c++];
            tmp += b[c++];
            ans += Math.Abs(tmp);
        }
        c = 1;
        tmp = 0;
        if(b.Count > 0){
            sum = new long[b.Count];
            sum[0] = b[0];
            tmp = b[0];
        }
        for(int i=1; i<b.Count; i++){
            sum[i] = sum[i-1] + Math.Abs(b[i]);
        }
        while(c + 1 < b.Count){
            if(b[c] + b[c+1] < 0){
                ans = Math.Max(ans, tmp + b[c] + b[c+1] + sum[b.Count - 1] - sum[c+1]);
            }
            tmp = tmp - b[c-1] + Math.Abs(b[c-1] + b[c]) + b[c+1];
            c = c + 2;
        }
        c = 3;
        if(b.Count > 1) tmp = b[0];
        while(c < b.Count){
            if(b[c] + b[c-1] < 0){
                ans = Math.Max(ans, tmp - b[c] - b[c-1] + b[c-2] + sum[b.Count - 1] - sum[c]);
            }
            tmp = tmp - b[c-3] + Math.Abs(b[c-3] + b[c-2]) + b[c-1];
            c = c + 2;
        }
        
        Console.WriteLine(ans);
    }
    
    public static void DebugWL(object x){ System.Diagnostics.Debug.WriteLine(x); }
}