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 (Python):
n = int(input())
a = input().split()
if(n==1):
    print(a[0])
    exit()

a_num = list( map(int, a[::2]))
a_op = a[1::2]

cumsum = a_num[::]
cumsum_op = [0] * n
cumsum_op[0] = a_num[0]
minus_ind = []
for i in range(1,n):
    cumsum[i] += cumsum[i-1]
    if(a_op[i-1] == '+'):
        cumsum_op[i] = cumsum_op[i-1] + a_num[i]
    else:
        cumsum_op[i] = cumsum_op[i-1] - a_num[i]
        minus_ind.append(i)

ans = cumsum_op[-1]
if(len(minus_ind)<2):
    print(ans)
    exit()

for l,r in zip(minus_ind[:-1],minus_ind[1:]):
    
    tmp = cumsum_op[l-1] - (cumsum[r-1] - cumsum[l-1]) + (cumsum[-1] - cumsum[r-1])
    ans = max(ans,tmp)

print(ans)

Solution (Python):

"""

https://atcoder.jp/contests/arc066/tasks/arc066_c

最終的な式における寄与を考える
-はどういう効果を持っている？

愚直な方法としてdpがある
dp[i][mnum] = i番目まで見て、-がmnum個重なってる状態での最大値
→ O(N^2)
じつは簡単にできる・保持しておくべき(答えになる)状態はもっと少ないのでは？
→これ、割と濃厚…か？

-の効果は、それ以降の寄与を反転させる
なるべく必要のない(無駄な)ところでは戻さない方が最適に近づく
現状 - で、足すやつを寄与+にしたい! → 戻す
現状 + で、足すやつを寄与-にしたい！→そんな場合はない
－直後のやつを寄与+にしたい！→ 戻せる+なら直前で-に戻せばいい

お？？？
A-B+C-D+E+F-…とやっていく
一度マイナスのカッコに入ると決めたとする
A-(B+C-(D+E+F)-G-(H+I))
すると、次の-以降はすべて+の寄与にできる

あとは耳dp！
dp[i][state] = i番目まで見て,stateの時の最大値
state = 0 
state = 1 
state = 2 

"""

N = int(input())

A = input().split()

dp = [int(A[0]),float("-inf"),float("-inf")]

for i in range(N-1):

    op = A[i*2+1]
    a =  int(A[i*2+2])

    ndp = [float("inf")] * 3

    if op == "+":

        ndp[0] = dp[0] + a
        ndp[1] = dp[1] - a
        ndp[2] = dp[2] + a

    else:

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

    dp = ndp

print (max(dp))

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
terms = [[]] 
op = '+'
for x in input().rstrip().split():
    if x in '+-':
        op = x
        continue
    x = int(x)
    if op == '+':
        terms[-1].append(x)
    else:
        terms.append([x])



min_minus = 10 ** 18
first_sum = 0
for t in terms[1:]:
    x = first_sum + sum(t)
    if x < min_minus:
        min_minus = x
    first_sum += t[0]

answer = sum(sum(t) for t in terms)
if len(terms) > 1:
    answer -= 2 * min_minus

print(answer)

Solution (Python):
def solve(n, inp):
    if n == 1:
        return int(inp[0])

    INF = -(10 ** 18)

    dp1, dp2, dp3 = int(inp[0]), INF, INF
    for op, a in zip(inp[1::2], inp[2::2]):
        a = int(a)
        if op == '+':
            dp1, dp2, dp3 = dp1 + a, max(dp2 - a, dp3 + a), dp3 + a
        else:
            dp1, dp2, dp3 = max(dp1 - a, dp2 + a), max(dp1 - a, dp2 + a), max(dp2 + a, dp3 - a)

    return dp1


n = int(input())
inp = input().split()
print(solve(n, inp))

Solution (Python):



def solve(n):

    s = input()
    sum_a = 0
    sum_n = 0
    sum_np = 0
    min_sum_np = 10 ** 15

    j = s.find('-')
    if j < 0:
        return sum(list(map(int, s.split(' + '))))

    sum_a = sum(list(map(int, s[:j - 1].split(' + '))))
    j += 2
    s += ' '
    len_s = len(s)
    sign = -1
    w = ''
    while j < len_s:
        ch = s[j]
        j += 1
        if ch == ' ':
            if len(w):
                a = int(w)
                sum_a += a
                w = ''
                if sign < 0:
                    sum_n += a
                    sum_np = 0
                else:
                    sum_np += a
        elif ch == '+':
            sign = 1
        elif ch == '-':
            min_sum_np = min(min_sum_np, sum_n + sum_np)
            sign = -1
        else:
            w += ch

    min_sum_np = min(min_sum_np, sum_n + sum_np)
    return sum_a - 2 * min_sum_np


def main():
    n = input()
    n = int(n)

    print(solve(n))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(str,input().split()))
dp = [[-float("inf")] *4 for i in range(n+1)]
dp[0][0] = 0
for i in range(len(a)):
    b = i//2+1
    if a[i] == "-":
        tmp = [-float("inf"),0,0,0]
        for j in range(3):
            tmp[j+1] = dp[b][j]
        dp[b] = tmp
        continue
    elif a[i] == "+":
        continue
    for j in range(4):
        for k in range(3):
            if j >= k:
                dp[b][k] = max(dp[b][k],dp[b-1][j]+((-1)**j)*int(a[i]))
print(dp[n][0])

Solution (Python):
input()
sm = de = 0
ans = 10 ** 15
for s in (input() + "-0").split('-'):
    a = list(map(int, s.split('+')))
    if sm:
        ans = min(ans, sum(a) + de)
        de += a[0]
    sm += sum(a)
print(sm - 2 * ans)

Solution (Python):
n = input()
s = map(int, ('+ ' + raw_input()).replace('+ ', '+').replace('- ', '-').split())
s0 = [abs(x) for x in s]
t = filter(lambda x: s[x + 1] < 0, xrange(n - 1)) + [n - 1] * 2
for i in xrange(1, n):
	s[i] += s[i - 1]
	s0[i] += s0[i - 1]
print max([s0[n - 1] - 2 * s0[t[i + 1]] + s0[t[i]] + s[t[i]] for i in xrange(len(t) - 1)])

Solution (Python):
n = input()
code = raw_input().split()
a = int(code[0]); b = c = -10**18
for op, v in zip(code[1::2], map(int,code[2::2])):
    if op is "+":
        b = max(b-v, c+v)
        c += v
        a = max(a+v, b)
    else:
        c = max(b+v, c-v)
        a = b = max(a-v, c)
print max(a, b, c)

Solution (Python):
n = input()
code = raw_input().split()

a = int(code[0]); b = c = -10**18
for op, v in zip(code[1::2], map(int,code[2::2])):
    if op is "+":
        d = max(b-v, c+v)
        a,b,c = max(a+v, d), d, c+v
    else:
        d = max(b+v, c-v)
        a,b,c = max(a-v, d), max(a-v, d), d
print max(a, b, c)

Solution (Python):
n = input()
code = raw_input().split()

dp = [[0]*3 for i in xrange(n)]
dp[0] = [int(code[0]), -10**18, -10**18]
for i in xrange(1,n):
    op, a = code[2*i-1: 2*i+1]
    v = int(a)
    if op is "+":
        dp[i][0] = max(dp[i-1][0]+v, dp[i-1][1]-v, dp[i-1][2]+v)
        dp[i][1] = max(              dp[i-1][1]-v, dp[i-1][2]+v)
        dp[i][2] =                                 dp[i-1][2]+v
    else:
        dp[i][0] = max(dp[i-1][0]-v, dp[i-1][1]+v, dp[i-1][2]-v)
        dp[i][1] = max(dp[i-1][0]-v, dp[i-1][1]+v, dp[i-1][2]-v)
        dp[i][2] = max(              dp[i-1][1]+v, dp[i-1][2]-v)
print max(dp[-1])