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.
# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 2020/6/4

"""

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

INF = 10**7

class SegmentTree:
    def __init__(self, size, useMax):
        n = size * 4 + 1
        v = -INF if useMax else INF
        self.data = [v for _ in range(n)]
        self.size = size
        self.n = n
        self.useMax = useMax
        self.lazy = [False for _ in range(n)]
    
    def push(self, index):
        pass
    
    def build(self):
        pass
    
    def update(self, index, val):
        self.update_impl(1, 0, self.size-1, index, index, val)
    
    def query(self, start, end):
        return self.query_impl(1, 0, self.size-1, start, end)
    
    def update_impl(self, index, nodel, noder, targetl, targetr, val):
        if nodel > noder or targetl > targetr:
            return -INF if self.useMax else INF
        
        self.push(index)
        if nodel == targetl and noder == targetr:
            self.data[index] = max(self.data[index], val) if self.useMax else min(self.data[index], val)
            return self.data[index]
        
        m = (nodel + noder) // 2
        a =  self.update_impl(index * 2, nodel, min(m, noder), targetl, min(m, targetr), val)
        b =  self.update_impl(index * 2 + 1, max(nodel, m+1), noder, max(targetl, m+1), targetr, val)
        c = max(a, b) if self.useMax else min(a, b)
        self.data[index] = max(self.data[index], c) if self.useMax else min(self.data[index], c)
        
        return c
        
    def query_impl(self, index, nodel, noder, targetl, targetr):
        if nodel > noder or targetl > targetr:
            return -INF if self.useMax else INF
    
        self.push(index)
        if nodel == targetl and noder == targetr:
            return self.data[index]
        
        m = (nodel + noder) // 2
        a = self.query_impl(index * 2, nodel, min(m, noder), targetl, min(m, targetr))
        b = self.query_impl(index * 2 + 1, max(m+1, nodel), noder, max(m+1, targetl), targetr)
        c = max(a, b) if self.useMax else min(a, b)
        
        return c
    
    
def solve(N, A):
    presum = [0]
    for v in A:
        presum.append(presum[-1] + v)
    
    left = []
    q = []
    # print(presum)
    st = SegmentTree(N, useMax=False)
    for i, v in enumerate(A):
        while q and q[-1][0] <= v:
            q.pop()
        j = q[-1][1] + 1 if q else 0
        left.append(max(presum[i] - st.query(j, i-1), 0))
        st.update(i, presum[i])
        q.append((v, i))
        # print(st.data)
        # print([st.query(i, i) for i in range(N+1)])
        
    right = []
    q = []
    st = SegmentTree(N, useMax=True)
    for i in range(N-1, -1, -1):
        v = A[i]
        while q and q[-1][0] <= v:
            q.pop()
        j = q[-1][1] - 1 if q else N-1
        right.append(max(0, st.query(i+1, j) - presum[i+1]))
        st.update(i, presum[i+1])
        q.append((v, i))
    right = right[::-1]
    
    # print(left)
    # print(right)
    
    return max([a+b for a, b in zip(left, right)])

N = int(input())
A = [int(x) for x in input().split()]
# N = 10
# A = [-1 for _ in range(N)]
print(solve(N, A))
