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)

#include "bits/stdc++.h"
using namespace std;

#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

#define MAX_N 200000
int N;
int A[MAX_N] = {};
char op[MAX_N] = {};

pair<int, int> ma(int l);
pair<int, int> mi(int l);

pair<int, int> mi(int l) {
	dump("mi");
	int ret = A[l];
	int tmp = A[l];
	int min_index = l;
	rep(i, l + 1, N) {
		if (op[i] == '-') {
			pair<int, int> res = ma(i);
			tmp -= res.first;
			i = res.second;
		}
		else {
			tmp += A[i];
		}
		if (tmp <= ret) {
			ret = tmp;
			min_index = i;
		}
		dump(i);
		dump(tmp);
		dump(ret);
	}
	return make_pair(ret, min_index);
}

pair<int, int> ma(int l) {
	dump("ma");
	int ret = A[l];
	int tmp = A[l];
	int max_index = l;
	rep(i, l + 1, N) {
		if (op[i] == '-') {
			pair<int, int> res = mi(i);
			tmp -= res.first;
			i = res.second;
		}
		else {
			tmp += A[i];
		}
		if (tmp >= ret) {
			ret = tmp;
			max_index = i;
		}
		dump(i);
		dump(tmp);
		dump(ret);
	}
	return make_pair(ret, max_index);
}

int solve() {
	pair<int, int> res = ma(0);
	int ret = res.first;
	while (res.second < N) {
		res = ma(res.second + 1);
		ret += res.first;
	}
	return ret;
}

signed main() {
	cin >> N;
	cin >> A[0];
	rep(i, 1, N)cin >> op[i] >> A[i];
	cout << solve() << endl;
	return 0;
}