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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

// Math = ♥
// Sometimes it is people that no one imagines anything of who do things that no one can imagine
// After all this time? Always

#include <bits/stdc++.h>                        // uncomment before submission
//#include <ext/pb_ds/assoc_container.hpp>		// uncomment before submission
//#include <ext/pb_ds/tree_policy.hpp>			// uncomment before submission
//using namespace __gnu_pbds;					// uncomment before submission
using namespace std;
//<---------------------------------------------------Template----------------------------------------------------------->
#define int long long
#define ll long long
#define ld long double
int INF = 1e9 + 7;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;                  // Vector of long long
typedef vector<vi> vvi;                 // Vector of vi
typedef vector<ii> vii;                 // Vector of pairs
typedef vector<vii> vvii;               // Vector of Vector of pairs
typedef vector<bool> vb;                // Vector of bool
#define pq priority_queue               // Max heap (To convert to min heap, use negative sign before every value)
#define ff first                        // For pairs
#define ss second                       // For pairs
#define pb push_back                    // Pushback to vector
#define mp make_pair                    // Makes pairs to be stored as pair
#define all(c) (c).begin(), (c).end()   // Mainly used by me in sorting
// ordered_set adds two new functions to set - (set).find_by_order([kth element based on zero indexing]) and order_of_key()
// order_of_key returns number of elements less that parameter. If element exists, that order is its index
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//<----------------------------------------------------------------------------------------------------------------------->

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;

    vector<char> op;
    vi num;

    int x;
    cin >> x, num.pb(x);
    for (int i = 0; i < n - 1; i++)
    {
        char ch;
        cin >> ch >> x;
        op.pb(ch), num.pb(x);
    }

    vvi dp(n, vector<int>(3));
    dp[0][0] = num[0];
    dp[0][1] = -INF;
    dp[0][2] = -INF;

    for (int i = 1; i < n; i++)
    {
        if (op[i - 1] == '+')
        {
            dp[i][1] = dp[i - 1][1] - num[i];
            dp[i][2] = dp[i - 1][2] + num[i];
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + num[i];
        } else
        {
            dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) - num[i];
            dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + num[i];
            dp[i][0] = -INF;
        }
        dp[i][0] = max(dp[i][0], max(dp[i][1], dp[i][2]));
        dp[i][1] = max(dp[i][1], dp[i][2]);
    }

    int a[] = {dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]};
    sort(a, a + 3);

    cout << a[2] << endl;
    return 0;
}