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 \le r$). After that Bob removes a single card $j$ from that segment $(l \le j \le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + 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 \le n \le 10^5$) — the number of cards.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-30 \le a_i \le 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$.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
l = list(map(int,input().split()))

curr = 0
best = 0
prevs = [0] * 31
for v in l:
    curr += v
    if v >= 0:
        for i in range(0, v):
            prevs[i] = curr
        for i in range(v, 31):
            best = max(curr - prevs[i] - i, best)
    else:
        for i in range(31):
            prevs[i] = min(prevs[i], curr)
print(best)


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout
from collections import defaultdict

N = 10**5 + 7

minn = [0 for _ in range(2*N)]
maxx = [0 for _ in range(2*N)]

def build(p, n):
    for i in range(n): 
        maxx[n+i] = p[i]
        minn[n+i] = p[i]

    for i in range(n-1, 0, -1):
        maxx[i] = max(maxx[i<<1], maxx[i<<1|1])
        minn[i] = min(minn[i<<1], minn[i<<1|1])

def query(l, r, n):
    l += n
    r += n

    retminn, retmaxx = float('inf'), -float('inf')

    while l < r:
        if l&1:
            retminn = min(retminn, minn[l])
            retmaxx = max(retmaxx, maxx[l])
            l += 1

        if r&1:
            r -= 1
            retminn = min(retminn, minn[r])
            retmaxx = max(retmaxx, maxx[r])

        l >>= 1
        r >>= 1

    return retminn, retmaxx


def main():
    n = int(input())
    a = list(map(int, input().strip().split()))

    left = [-1]*n
    right = [n]*n

    p = [a[i] for i in range(n)]
    for i in range(1, n): p[i] += p[i-1]

    build(p, n)

    st = [0]
    for i in range(1, n):
        while st and a[st[-1]] <= a[i]: st.pop()

        if st: left[i] = st[-1]
        st.append(i)

    st = [n-1]
    for i in range(n-2, -1, -1):
        while st and a[st[-1]] < a[i]: st.pop()

        if st: right[i] = st[-1]
        st.append(i)

    #print(left, right, p)
    #
    
    if max(a) <= 0:
        print(0)
        return None

    ret = -float('inf')

    for i in range(n):
        l, r = left[i], right[i]

        _, rmaxx = query(i, r, n)
        lminn, _ = query(max(0, l), i, n)

        if l < 0: lminn = min(lminn, 0)

        s = rmaxx - lminn
        #print(i, s, a[i], l, r)

        ret = max(ret, s-a[i])

    print(ret)



def __starting_point(): main()

__starting_point()