stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <assert.h>

#define IOS ios::sync_with_stdio(false)

using namespace std;

#define inf (0x3f3f3f3f)

typedef long long int LL;





#include <iostream>

#include <sstream>

#include <vector>

#include <set>

#include <map>

#include <queue>

#include <string>

#include <bitset>

const int maxn = 1e5 + 20;

LL perfixSum[maxn], absSum[maxn];

vector<int>pos;

void cut(LL &val, int pos1, int pos2) {

    if (pos1 > pos2) return;

    val -= absSum[pos2] - absSum[pos1 - 1];

}

void work() {

    int n;

    scanf("%d", &n);

    int val;

    scanf("%d", &val);

    perfixSum[1] = val;

    absSum[1] = val;

    for (int i = 2; i <= n; ++i) {

        int val;

        char op;

        cin >> op;

        scanf("%d", &val);

        if (op == '+') {

            perfixSum[i] = perfixSum[i - 1] + val;

        } else {

            perfixSum[i] = perfixSum[i - 1] - val;

            pos.push_back(i);

        }

        absSum[i] = absSum[i - 1] + val;

    }

    LL ans = perfixSum[n];

    for (int i = 0; i <= (int)pos.size() - 2; ++i) {

        int p1 = pos[i], p2 = pos[i + 1];

        LL tans = absSum[n] - absSum[p2 - 1];

        tans += perfixSum[p1];

        cut(tans, p1 + 1, p2 - 1);

        ans = max(ans, tans);

    }

    cout << ans << endl;

}



int main() {

#ifdef local

    freopen("data.txt", "r", stdin);

//    freopen("data.txt", "w", stdout);

#endif

//    int val;

//    scanf("%d", &val);

//    cout << val << endl;

    work();

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.221848096077787x):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;



const int MAXN=100005;

const int INF=0x3f3f3f3f;



ll sum[MAXN];

ll tot,ans=-INF;

int n,a[MAXN];

bool flag[MAXN];

char s[5];



int main(){

    scanf("%d%d",&n,&a[1]);

    for(int i=2;i<=n;i++){

        scanf("%s%d",s,&a[i]);

        if(s[0]=='-') flag[i]=1;

    }

    for(int i=n;i>=1;i--)

        sum[i]=sum[i+1]+a[i];

    int last=1;

    while(flag[last]==0&&last<=n){

        tot+=a[last];

        last++;

    }

    while(last<=n){

        int last1=last+1;

        ll tot1=0;

        while(flag[last1]==0&&last1<=n){

            tot1+=a[last1];

            last1++;

        }

        if(last1<=n)

            ans=max(ans,tot-tot1-a[last]+sum[last1]);

        tot-=a[last];

        last=last1;

        tot+=tot1;

    }

    ans=max(ans,tot);

    printf("%lld",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



static const int Maxn = 100005;



int n, st, ed;

char op[Maxn];

long long a[Maxn];

long long dp[Maxn][3];



int main() {

  scanf("%d\n", &n);

  scanf("%lld ", a + 1), op[1] = '+';

  for (int i = 2; i <= n; ++i)

    scanf("%c %lld ", op + i, a + i);

  memset(dp, -63, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 1; i <= n; ++i) {

    dp[i][2] = dp[i - 1][2] + a[i];

    if (op[i] == '-') {

      dp[i][1] = max(dp[i - 1][0] - a[i], dp[i - 1][1] + a[i]);

      dp[i][2] = max(dp[i][2], dp[i - 1][1] + a[i]);

    }

    else {

      dp[i][0] = max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]);

      dp[i][1] = max(dp[i - 1][2] + a[i], dp[i - 1][1] - a[i]);

    }

  }

  printf("%lld\n", max(dp[n][0], max(dp[n][1], dp[n][2])));

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.443105869685171x):
//I love Nanami Chiaki

#include<bits/stdc++.h>

using namespace std;

#define ll long long

inline int read(){

	int re=0;char c=getchar();

	while (c<'0' || c>'9') c=getchar();

	while (c>='0' && c<='9') {re=(re<<1)+(re<<3)+c-'0';c=getchar();}

	return re;

}

inline int readc(){

	char c=getchar();

	while (c!='+' && c!='-') c=getchar();

	if (c=='+') return 1;

	return -1;

}

int n;

ll ans=0;

int main(){

	int n=read();

	ans+=read();

	int op,a;

	ll hve=0,add=-1,pre=-1;

	for (int i=1;i<n;i++){

		op=readc();a=read();

		ans+=a;

		if (op==-1){

			if (pre==-1){

				pre=a;

				hve=0;

				continue;

			}

			if (add==-1) add=pre+hve;

			else add=min(add,pre+hve);

			pre+=a;

			hve=0;

		}

		else{

			hve+=a;

		}

	}

	if (pre==-1){

		printf("%lld\n",ans);

		return 0;

	}

	else if (add==-1) ans-=2*pre;

	else ans-=2*add;

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



static const int Maxn = 100005;



int n, st, ed;

char op[Maxn];

long long a[Maxn];

long long dp[Maxn][3];



int main() {

  scanf("%d\n", &n);

  scanf("%lld ", a + 1), op[1] = '+';

  for (int i = 2; i <= n; ++i)

    scanf("%c %lld ", op + i, a + i);

  memset(dp, -63, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 1; i <= n; ++i) {

    dp[i][2] = dp[i - 1][2] + a[i];

    if (op[i] == '-') {

      dp[i][1] = max(dp[i - 1][0] - a[i], dp[i - 1][1] + a[i]);

      dp[i][2] = max(dp[i][2], dp[i - 1][1] + a[i]);

    }

    else {

      dp[i][0] = max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]);

      dp[i][1] = max(dp[i - 1][2] + a[i], dp[i - 1][1] - a[i]);

    }

  }

  printf("%lld\n", max(dp[n][0], max(dp[n][1], dp[n][2])));

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.427475478896376x):
#include<bits/stdc++.h>

using namespace std;

 

typedef long long ll;

typedef pair<int,int> pii;

 

const int maxn=100003;

 

int n,a[maxn],o[maxn];

ll f[maxn][3];

char s[3];

 

int main(){

	scanf("%d",&n);

	scanf("%d",&a[1]);

	for(int i=2;i<=n;++i){

		scanf("%s%d",s,&a[i]);

		if(s[0]=='-')o[i]=1;

	}

	for(int i=0;i<=n;++i)

		for(int j=0;j<3;++j)

			f[i][j]=-1e18;

	f[0][0]=0;

	for(int i=1;i<=n;++i){

		if(!o[i]){

			f[i][0]=max(f[i-1][0]+a[i],f[i-1][1]+a[i]);

			f[i][1]=max(f[i-1][1]-a[i],f[i-1][2]+a[i]);

			f[i][2]=f[i-1][2]+a[i];

		}else{

			f[i][1]=max(f[i-1][0]-a[i],f[i-1][1]+a[i]);

			f[i][2]=max(f[i-1][2]+a[i],f[i-1][1]+a[i]);

		}

		

	}

	printf("%lld\n",max(f[n][0],f[n][1]));

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



static const int Maxn = 100005;



int n, st, ed;

char op[Maxn];

long long a[Maxn];

long long dp[Maxn][3];



int main() {

  scanf("%d\n", &n);

  scanf("%lld ", a + 1), op[1] = '+';

  for (int i = 2; i <= n; ++i)

    scanf("%c %lld ", op + i, a + i);

  memset(dp, -63, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 1; i <= n; ++i) {

    dp[i][2] = dp[i - 1][2] + a[i];

    if (op[i] == '-') {

      dp[i][1] = max(dp[i - 1][0] - a[i], dp[i - 1][1] + a[i]);

      dp[i][2] = max(dp[i][2], dp[i - 1][1] + a[i]);

    }

    else {

      dp[i][0] = max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]);

      dp[i][1] = max(dp[i - 1][2] + a[i], dp[i - 1][1] - a[i]);

    }

  }

  printf("%lld\n", max(dp[n][0], max(dp[n][1], dp[n][2])));

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.4012627402053193x):
#include <bits/stdc++.h>

using namespace std;

int a[100002];

long long dp[100002][3];

char s[2];

int main(){

    int n;

    scanf("%d", &n);

    scanf("%d", &a[1]);



    for(int i=1;i<=n;i++) dp[i][0] = dp[i][1] = dp[i][2] = -1e18;

    dp[1][0] = a[1];



    for(int i=2;i<=n;i++){

        scanf("%s", s);

        scanf("%d", &a[i]);

        if(s[0] == '-'){

            dp[i][0] = max(dp[i-1][0] - a[i], dp[i-1][1] + a[i]);

            dp[i][1] = max(dp[i-1][0] - a[i], max(dp[i-1][1] + a[i], dp[i-1][2] - a[i]));

            dp[i][2] = max(dp[i-1][2] - a[i], dp[i-1][1] + a[i]);

        }

        else{

            dp[i][0] = max(dp[i-1][0] + a[i], dp[i-1][1] + a[i]);

            dp[i][1] = max(dp[i-1][1] - a[i], dp[i-1][2] + a[i]);

            dp[i][2] = dp[i-1][2] + a[i];

        }

        dp[i][0] = max(dp[i][0], -(long long)1e18);

        dp[i][1] = max(dp[i][1], -(long long)1e18);

        dp[i][2] = max(dp[i][2], -(long long)1e18);

    }   

    printf("%lld\n", max(max(dp[n][0], dp[n][1]), dp[n][2]));

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



static const int Maxn = 100005;



int n, st, ed;

char op[Maxn];

long long a[Maxn];

long long dp[Maxn][3];



int main() {

  scanf("%d\n", &n);

  scanf("%lld ", a + 1), op[1] = '+';

  for (int i = 2; i <= n; ++i)

    scanf("%c %lld ", op + i, a + i);

  memset(dp, -63, sizeof(dp));

  dp[0][0] = 0;

  for (int i = 1; i <= n; ++i) {

    dp[i][2] = dp[i - 1][2] + a[i];

    if (op[i] == '-') {

      dp[i][1] = max(dp[i - 1][0] - a[i], dp[i - 1][1] + a[i]);

      dp[i][2] = max(dp[i][2], dp[i - 1][1] + a[i]);

    }

    else {

      dp[i][0] = max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]);

      dp[i][1] = max(dp[i - 1][2] + a[i], dp[i - 1][1] - a[i]);

    }

  }

  printf("%lld\n", max(dp[n][0], max(dp[n][1], dp[n][2])));

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3084434923288457x):
#include<bits/stdc++.h>

using namespace std;



int read(){

	int a = 0; char c = getchar(); while(!isdigit(c)) c = getchar();

	while(isdigit(c)){a = a * 10 + c - 48; c = getchar();} return a;

}



const int _ = 1e5 + 7;

long long N , dp[_][2] , val[_][2] , cnt;



int main(){

	N = read(); val[0][1] = read();

	for(int i = 1 ; i < N ; ++i) if(getchar() == '-') val[++cnt][0] = read(); else val[cnt][1] += read();

	memset(dp , -0x3f , sizeof(dp)); dp[0][0] = 0;

	for(int i = 1 ; i <= cnt ; ++i){

		dp[i][0] = max(dp[i - 1][0] , dp[i - 1][1]) + val[i][1] - val[i][0];

		dp[i][1] = max(dp[i - 1][0] - val[i][0] - val[i][1] , dp[i - 1][1] + val[i][0] + val[i][1]);

	}

	cout << max(dp[cnt][0] , dp[cnt][1]) + val[0][1];

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef pair<int, int> PII;



const int MM = 1e9 + 7;

const double eps = 1e-8;

const int MAXN = 2e6 + 10;

const ll INF = 1ll << 60;



int n, m;



ll f[MAXN][2];



int a[MAXN];



void prework(){



}



int b[MAXN];



void read(){

	scanf("%d", &n);

	for(int i = 1; i <= n; i++){

		scanf("%d", &a[i]);

		char s[10];

		if (i < n){

			scanf("%s", s);

			if (s[0] == '+')

				b[i + 1] = 1;

			else

				b[i + 1] = -1;

		}

	}

	b[0] = b[1] = 1;

	f[0][0] = 0;

	f[0][2] = f[0][1] = -INF;

	for(int i = 1; i <= n; i++){

		if (b[i] == 1){

			f[i][1] = max(f[i-1][1] - a[i], f[i-1][2] + a[i]);

			f[i][2] = f[i-1][2] + a[i];

			f[i][0] = max(f[i-1][0] + a[i], f[i-1][1]);

		}

		else{

			f[i][2] = max(f[i-1][1] + a[i], f[i-1][2] - a[i]);

			f[i][0] = max(f[i-1][0] - a[i], f[i][2]);

			f[i][1] = f[i][0];

		}

		//cout<<f[i][0]<<' '<<f[i][1]<<endl;

	}

	cout<<max(f[n][1],max(f[n][0], f[n][2]))<<endl;

}



void solve(int casi){



}



void printans(){



}





int main(){

	prework();

	int T = 1;

//	cin>>T;

	for(int i = 1; i <= T; i++){

		read();

		solve(i);

		printans();

	}

	return 0;

}






// Faster C++ solution for the same problem (measured speedup: 5.152952316271076x):
#include<stdio.h>

#define N (100010)

typedef long long LL;

int a[N];

char b[N][2];

int n;

int main(){

	scanf("%d",&n);

	for (int i=1;i<n;i++){

		scanf("%d%s",a+i,b[i]);

	}

	scanf("%d",a+n);

	LL ans1=a[1];

	for (int i=1;i<n;i++) ans1+=b[i][0]=='+'?a[i+1]:-a[i+1];

	LL temp=ans1;

	LL cur=a[n],tot=0;

	for (int i=n-1;i;i--){

		if (b[i][0]=='-'){

			temp=temp+a[i+1];

			if (temp-cur+tot>ans1) ans1=temp-cur+tot;

			tot+=cur;

			cur=0;

		}

		else {

			temp=temp-a[i+1];

		}

		cur+=a[i];

	}

	printf("%lld\n",ans1);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



typedef long long LL;



const int N = 1e5 + 10;

int a[N], ri[N];

char b[N];

LL sum[N];



int main() {

    int n;

    scanf("%d", &n);

    scanf("%d", &a[1]);

    for (int i = 2; i <= n; i++) scanf(" %c %d", b + i, a + i);

    for (int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i];

    ri[n + 1] = n + 1;

    for (int i = n; i >= 2; i--) {

        ri[i] = ri[i+1];

        if (b[i+1] == '-') ri[i] = i + 1;

    }

    LL ans = -1e18;

    LL tmp = a[1];

    for (int i = 2; i <= n; i++) {

        if (b[i] == '-') {

            LL ans1 = tmp - a[i] - (sum[ri[i]-1]-sum[i]) + (sum[n] - sum[ri[i]-1]);

            ans = max(ans1, ans);

        }

        if (b[i] == '-') tmp -= a[i];

        else tmp += a[i];

    }

    ans = max(ans, tmp);

    printf("%lld\n", ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.133337106409482x):
#include<stdio.h>

#define N (100010)

typedef long long LL;

int a[N];

char b[N][2];

int n;

int main(){

	scanf("%d",&n);

	for (int i=1;i<n;i++){

		scanf("%d%s",a+i,b[i]);

	}

	scanf("%d",a+n);

	LL ans1=a[1];

	for (int i=1;i<n;i++) ans1+=b[i][0]=='+'?a[i+1]:-a[i+1];

	LL temp=ans1;

	LL cur=a[n],tot=0;

	for (int i=n-1;i;i--){

		if (b[i][0]=='-'){

			temp=temp+a[i+1];

			if (temp-cur+tot>ans1) ans1=temp-cur+tot;

			tot+=cur;

			cur=0;

		}

		else {

			temp=temp-a[i+1];

		}

		cur+=a[i];

	}

	printf("%lld\n",ans1);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

long long dp[maxn][3];

int n;

int val[maxn],pos[maxn];

char op[3];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&val[i]);

		if(i!=n)

		{

			scanf("%s",op+1);

			if(op[1]=='+')pos[i+1]=0;

			else pos[i+1]=1;		

		}

	}

	memset(dp,128,sizeof(dp));

	dp[0][0]=0;

	for(int i=1;i<=n;i++)

	{

		if(pos[i])

		{

			dp[i][0]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][1]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][2]=max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]);

		}

		else 

		{

			dp[i][0]=max(max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]),dp[i-1][0]+val[i]);

			dp[i][1]=max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]);

			dp[i][2]=dp[i-1][2]+val[i];

		}

	}

	printf("%lld\n",dp[n][0]);

}

// Faster C++ solution for the same problem (measured speedup: 3.4193850795901604x):
/*

1. 只有 "-" 号后面才有 "("

2. 最多只会存在两层嵌套。假设有三层：-(A-(B-(C-D))), 显然 -(C-D) 还是 -(C-D)，可以变化成 -(A-B)-(C-D)，层数变少.



由此得出 DP 解法：f[i, j] 表示结束第 i 个数时，有 j 个左括号尚未匹配的答案，j = 0/1/2

*/

#include <bits/stdc++.h>

#define rep(i, x, y) for (int i = x; i <= y; i++)

using namespace std;



const int N = 1e5 + 10;

typedef long long ll;

ll n, x, f[3];



int read() {

    int x = 0, op = 1; char ch = getchar();

    for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1;

    for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch - '0');

    return x * op;

}



int main() {

    cin >> n;

    f[1] = f[2] = -1e18;

    rep(i, 1, n) {

        x = read();

        f[0] += x, f[1] -= x, f[2] += x;

        if (x < 0)

            f[2] = max(f[2], f[1]), f[1] = max(f[1], f[0]);  // 在 x 前加一个 '('

        f[0] = max(f[0], f[1]), f[1] = max(f[1], f[2]);  // 在 x 后加一个 ')'

    }

    printf("%lld\n", f[0]);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

long long dp[maxn][3];

int n;

int val[maxn],pos[maxn];

char op[3];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&val[i]);

		if(i!=n)

		{

			scanf("%s",op+1);

			if(op[1]=='+')pos[i+1]=0;

			else pos[i+1]=1;		

		}

	}

	memset(dp,128,sizeof(dp));

	dp[0][0]=0;

	for(int i=1;i<=n;i++)

	{

		if(pos[i])

		{

			dp[i][0]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][1]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][2]=max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]);

		}

		else 

		{

			dp[i][0]=max(max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]),dp[i-1][0]+val[i]);

			dp[i][1]=max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]);

			dp[i][2]=dp[i-1][2]+val[i];

		}

	}

	printf("%lld\n",dp[n][0]);

}

// Faster C++ solution for the same problem (measured speedup: 3.4377738055946905x):
#include<bits/stdc++.h>

#define maxn 100005

#define LL long long

using namespace std;



int n;

LL sm[maxn],lsm[maxn];

bool isn[maxn];



int main(){

	scanf("%d",&n);

	LL ans = 0;

	for(int i=1;i<=n;i++){

		scanf("%lld",&sm[i]);

		if(isn[i-1]) ans -= sm[i];

		else ans += sm[i];

		lsm[i] = ans;

		sm[i] += sm[i-1];

		char ch[2];

		if(i<n){

			scanf("%s",ch);

			if(ch[0] == '-')isn[i] = 1;

		}

	}

	int nx = n;

	for(int i=n-1;i>=0;i--) if(isn[i]){

		ans = max(ans , sm[n] - sm[nx] - (sm[nx] - sm[i]) + lsm[i]);

		nx = i;

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

long long dp[maxn][3];

int n;

int val[maxn],pos[maxn];

char op[3];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&val[i]);

		if(i!=n)

		{

			scanf("%s",op+1);

			if(op[1]=='+')pos[i+1]=0;

			else pos[i+1]=1;		

		}

	}

	memset(dp,128,sizeof(dp));

	dp[0][0]=0;

	for(int i=1;i<=n;i++)

	{

		if(pos[i])

		{

			dp[i][0]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][1]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][2]=max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]);

		}

		else 

		{

			dp[i][0]=max(max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]),dp[i-1][0]+val[i]);

			dp[i][1]=max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]);

			dp[i][2]=dp[i-1][2]+val[i];

		}

	}

	printf("%lld\n",dp[n][0]);

}

// Faster C++ solution for the same problem (measured speedup: 3.4536513590462334x):
#include<bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)

#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)

#define mp(x,y) make_pair((x),(y))

#define sz(x) (int)(x).size()

#define pb push_back

typedef long long ll;

typedef pair<int,int> pii;

inline int read(){

    register int x=0,f=1;register char ch=getchar();

    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}

    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}

    return f?x:-x;

}



int n;

ll f[100005][3];

inline void chkmax(ll& x,ll y){x=x>y?x:y;}



int main(){

	// freopen("in.in","r",stdin);

	f[0][0]=0,f[0][1]=f[0][2]=-1e16;

	n=read();

	REP(i,1,n){

		int x=read();

		f[i][0]=max(f[i-1][0],f[i-1][1]);

		f[i][1]=max(f[i-1][1],f[i-1][2]);

		f[i][2]=f[i-1][2];

		f[i][0]+=x,f[i][1]-=x,f[i][2]+=x;

		if(x<0)chkmax(f[i][2],f[i][1]),chkmax(f[i][1],f[i][0]);

	}

	printf("%lld\n",max(f[n][0],max(f[n][1],f[n][2])));

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

#define int long long

const int N = 1e5 + 5, M = 5, inf = 1e15;

int dp[N][M], a[N];

char op[N];



int Sign(int x) {

    if (x % 2) return -1;

    return 1;

}



int32_t main() {

    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) dp[i][j] = -inf;

    int n; cin >> n >> a[0];

    for (int i = 1; i < n; i++) cin >> op[i] >> a[i];

    dp[0][0] = a[0];

    for (int i = 1; i < n; i++) for (int j = M - 1; j >= 0; j--) {

        if (op[i] == '+') dp[i][j] = dp[i - 1][j] + a[i] * Sign(j);

        else if (j) dp[i][j] = dp[i - 1][j - 1] + a[i] * Sign(j);

        if (j + 1 < M) dp[i][j] = max(dp[i][j], dp[i][j + 1]);

    }

    cout << dp[n-1][0] << "\n";

}


// Faster C++ solution for the same problem (measured speedup: 1.4885095276385938x):
#include <bits/stdc++.h>



using namespace std;

#define int long long

const int N = 1e5 + 5, M = 3, inf = 1e15;

int dp[N][M], a[N];

char op[N];



int Sign(int x) {

    if (x % 2) return -1;

    return 1;

}



int32_t main() {

    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.tie(0);

    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) dp[i][j] = -inf;

    int n; cin >> n >> a[0];

    for (int i = 1; i < n; i++) cin >> op[i] >> a[i];

    dp[0][0] = a[0];

    for (int i = 1; i < n; i++) for (int j = M - 1; j >= 0; j--) {

        if (op[i] == '+') dp[i][j] = dp[i - 1][j] + a[i] * Sign(j);

        else if (j) dp[i][j] = dp[i - 1][j - 1] + a[i] * Sign(j);

        if (j + 1 < M) dp[i][j] = max(dp[i][j], dp[i][j + 1]);

    }

    cout << dp[n-1][0] << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

long long dp[maxn][3];

int n;

int val[maxn],pos[maxn];

char op[3];

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&val[i]);

		if(i!=n)

		{

			scanf("%s",op+1);

			if(op[1]=='+')pos[i+1]=0;

			else pos[i+1]=1;		

		}

	}

	memset(dp,128,sizeof(dp));

	dp[0][0]=0;

	for(int i=1;i<=n;i++)

	{

		if(pos[i])

		{

			dp[i][0]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][1]=max(dp[i-1][0]-val[i],max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]));

			dp[i][2]=max(dp[i-1][1]+val[i],dp[i-1][2]-val[i]);

		}

		else 

		{

			dp[i][0]=max(max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]),dp[i-1][0]+val[i]);

			dp[i][1]=max(dp[i-1][1]-val[i],dp[i-1][2]+val[i]);

			dp[i][2]=dp[i-1][2]+val[i];

		}

	}

	printf("%lld\n",dp[n][0]);

}

// Faster C++ solution for the same problem (measured speedup: 3.453653074750459x):
#include <bits/stdc++.h>

#define inf 100000000000000ll

using namespace std;



int read() {

  int x = 0, f = 1;

  char c = getchar();

  while (c < '0' || c > '9')

    f = (c == '-') ? -1 : f, c = getchar();

  while (c >= '0' && c <= '9')

    x = x * 10 + c - '0', c = getchar();

  return x * f;

}



int n;

long long f[100005][3];

// void chmax(long long &x, long long y) { x = x > y ? x : y; }



int main() {

  n = read();

  f[0][0] = 0, f[0][1] = f[0][2] = -inf;

  for (int i = 1, x; i <= n; ++i) {

    x = read();

    {

      f[i][0] = max(f[i - 1][0], f[i - 1][1]),

      f[i][1] = max(f[i - 1][1], f[i - 1][2]);

      f[i][2] = f[i - 1][2];

    }

    f[i][0] += x, f[i][1] -= x, f[i][2] += x;

    if (x < 0)

      f[i][2] = max(f[i][2], f[i][1]), f[i][1] = max(f[i][1], f[i][0]);

    // chmax(f[i][2], f[i][1]), chmax(f[i][1], f[i][0]);

  }

  printf("%lld\n", max(f[n][0], max(f[n][1], f[n][2])));

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

typedef long long LL;

int op[N], num[N]; LL dp[N][3]; char s[5];

int main() {

	int n; scanf("%d%d", &n, &num[1]), op[1] = 1;

	for (int i = 2; i <= n; i++) {

		scanf("%s%d", s + 1, &num[i]);

		if (s[1] == '-') op[i] = -1; else op[i] = 1;

	}

	memset(dp, -0x3f, sizeof(dp)), dp[1][0] = num[1];

	for (int i = 2; i <= n; i++) {

		dp[i][1] = dp[i - 1][1] - op[i] * num[i];

		dp[i][0] = dp[i - 1][0] + op[i] * num[i];

		dp[i][2] = dp[i - 1][2] + op[i] * num[i];

		if (op[i - 1] == -1) {

			dp[i][1] = max(dp[i][1], dp[i - 1][0] - op[i] * num[i]);

			dp[i][2] = max(dp[i][2], dp[i - 1][1] + op[i] * num[i]);

		}

		dp[i][1] = max(dp[i][1], dp[i][2]);

		dp[i][0] = max(dp[i][0], dp[i][1]);

	}

	printf("%lld\n", dp[n][0]);

}

// Faster C++ solution for the same problem (measured speedup: 3.445987787028961x):
#include <bits/stdc++.h>

#define inf 100000000000000ll

using namespace std;



int read() {

  int x = 0, f = 1;

  char c = getchar();

  while (c < '0' || c > '9')

    f = (c == '-') ? -1 : f, c = getchar();

  while (c >= '0' && c <= '9')

    x = x * 10 + c - '0', c = getchar();

  return x * f;

}



int n;

long long f[100005][3];

// void chmax(long long &x, long long y) { x = x > y ? x : y; }



int main() {

  n = read();

  f[0][0] = 0, f[0][1] = f[0][2] = -inf;

  for (int i = 1, x; i <= n; ++i) {

    x = read();

    {

      f[i][0] = max(f[i - 1][0], f[i - 1][1]),

      f[i][1] = max(f[i - 1][1], f[i - 1][2]);

      f[i][2] = f[i - 1][2];

    }

    f[i][0] += x, f[i][1] -= x, f[i][2] += x;

    if (x < 0)

      f[i][2] = max(f[i][2], f[i][1]), f[i][1] = max(f[i][1], f[i][0]);

    // chmax(f[i][2], f[i][1]), chmax(f[i][1], f[i][0]);

  }

  printf("%lld\n", max(f[n][0], max(f[n][1], f[n][2])));

  return 0;

}