from itertools import*

from numpy import*

n,*a=list(map(int,open(0).read().split()))

if set(a)=={0}:

    print((0))

    exit()

a=array(a)

count=0

ans=0

while 1:

    count=min(a[a!=0])

    ans+=count*sum(k for k,v in groupby(a>0)if k==True)

    a=a-min(a[a!=0])

    a[a<0]=0

    if sum(a)==0:

        break

print(ans)
n,*a=list(map(int,open(0).read().split()))

ans=0

pre=0

for i in a:

    if i>pre:

        ans+=i-pre

    pre=i

print(ans)
from itertools import *
from numpy import *

n, *a = list(map(int, open(0).read().split()))
if set(a) == {0}:
    print((0))
    exit()
a = array(a)
count = 0
ans = 0
while 1:
    count = min(a[a != 0])
    ans += count * sum(k for k, v in groupby(a > 0) if k == True)
    a = a - min(a[a != 0])
    a[a < 0] = 0
    if sum(a) == 0:
        break
print(ans)

n, *a = list(map(int, open(0).read().split()))
ans = 0
pre = 0
for i in a:
    if i > pre:
        ans += i - pre
    pre = i
print(ans)

-from itertools import * -from numpy import * - -if set(a) == {0}: -    print((0)) -    exit() -a = array(a) -count = 0 -while 1: -    count = min(a[a != 0]) -    ans += count * sum(k for k, v in groupby(a > 0) if k == True) -    a = a - min(a[a != 0]) -    a[a < 0] = 0 -    if sum(a) == 0: -        break +pre = 0 +for i in a: +    if i > pre: +        ans += i - pre +    pre = i