Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value a_i. 

First, Alice chooses a non-empty consecutive segment of cards [l; r] (l ≤ r). After that Bob removes a single card j from that segment (l ≤ j ≤ r). The score of the game is the total value of the remaining cards on the segment (a_l + a_{l + 1} + ... + a_{j - 1} + a_{j + 1} + ... + a_{r - 1} + a_r). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 0.

Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.

What segment should Alice choose so that the score is maximum possible? Output the maximum score.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of cards.

The second line contains n integers a_1, a_2, ..., a_n (-30 ≤ a_i ≤ 30) — the values on the cards.

Output

Print a single integer — the final score of the game.

Examples

Input


5
5 -2 10 -1 4


Output


6


Input


8
5 2 5 3 -30 -30 6 9


Output


10


Input


3
-10 6 -15


Output


0

Note

In the first example Alice chooses a segment [1;5] — the entire row of cards. Bob removes card 3 with the value 10 from the segment. Thus, the final score is 5 + (-2) + (-1) + 4 = 6.

In the second example Alice chooses a segment [1;4], so that Bob removes either card 1 or 3 with the value 5, making the answer 5 + 2 + 3 = 10.

In the third example Alice can choose any of the segments of length 1: [1;1], [2;2] or [3;3]. Bob removes the only card, so the score is 0. If Alice chooses some other segment then the answer will be less than 0.
import sys
input=sys.stdin.readline
def build(a,index,beg,end,tree):
    if (beg == end):
        tree[index][0] = a[beg]
        tree[index][1] = a[beg]
    else:
        mid = (beg + end) // 2
        build(a, 2 * index + 1, beg, mid,tree)
        build(a, 2 * index + 2, mid + 1, end,tree)

        tree[index][0] = tree[2 * index + 1][0] +tree[2 * index + 2][0]

        tree[index][1] = max(tree[2 * index + 1][1],tree[2 * index + 1][0]+tree[2 * index + 2][1])

def query(index,beg, end, l,r):
    result=[-1,-1]
    if (beg > r or end < l):
        return result

    if (beg >= l and end <= r):
        return tree[index]


    mid = (beg + end)//2

    if (l > mid):
        return query(2 * index + 2, mid + 1, end,l, r)
    if (r <= mid):
        return query(2 * index + 1, beg, mid,l, r)

    left = query(2 * index + 1, beg, mid,l, r)
    right = query(2 * index + 2, mid + 1,end, l, r)

    result[0] = left[0]+ right[0]
    result[1] = max(left[1], left[0] +
                    right[1])

    return result


n=int(input())
arr=list(map(int,input().split()))

tree=[[0,0] for j in range(3*n)]
build(arr,0,0,n-1,tree)

fw = [0 for k in range(n)]
bw = [0 for k in range(n)]



cur_max = bw[n - 1] = arr[n - 1]
i = n - 2
p = [n - 1]
bw[n - 1] = 0
d = dict()
q=0
d[arr[n - 1]] = n - 1
while i >= 0:
    if arr[i] > 0:
        r = 1000000
        for j in range(arr[i] + 1, 31):
            if j in d.keys():
                r = min(r, d[j])
        if r > p[0]:
            bw[i] = max(0, cur_max)
        else:
            bw[i] = max(0, query(0,0,n-1,i+1,r-1)[1])

    cur_max = max(arr[i], cur_max + arr[i])
    if arr[i] == cur_max:
        p = [i]
        q = 1
    else:
        p.append(i)
        q += 1
    d[arr[i]] = i
    i += -1
tree=[[0,0] for j in range(3*n)]
build(arr[::-1],0,0,n-1,tree)
cur_max, max_so_far = arr[0], arr[0]
p = [0]
q = 0
d = dict()
d[arr[0]] = 0
for i in range(1, n):
    if arr[i] > 0:
        r = -1

        for j in range(arr[i] + 1, 31):
            if j in d.keys():
                r = max(r, d[j])

        if r < p[0]:
            fw[i] = max(0, cur_max)
        else:
            fw[i] = max(0,query(0,0,n-1,n-1-i+1,n-1-r-1)[1])

    cur_max = max(arr[i], cur_max + arr[i])
    if arr[i] == cur_max:
        p = [i]
        q = 1
    else:
        p.append(i)
        q += 1
    d[arr[i]] = i

f = 0
f = max(f, fw[n - 1], bw[0])
j = 1
while (j < (n - 1)):
    f = max(f, fw[j] + bw[j])
    j += 1
print(f)