CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
lsA = list(map(int,input().split()))
lsA += lsA
lsA.sort(reverse=True)

lsA.pop(0)
ans = 0
for i in range(N-1):
    ans += lsA.pop(0)
print(ans)

# Optimized solution
N = int(eval(input()))
lsA = list(map(int,input().split()))
lsA += lsA
lsA.sort(reverse=True)

lsA.pop(0)
ans = sum(lsA[:N-1])
print(ans)
# Original solution
N = int(eval(input()))
A_list = list(map(int,input().split()))
A_list.sort(reverse = True)
cmfrt = 0
mx_cmfrt = 0
counter = 0
reached_list = [A_list[0],A_list[0]]
for j in range(1, len(A_list)):
    for i in range(1, len(reached_list)):
        cmfrt = min(reached_list[i-1], reached_list[i])
        if cmfrt > mx_cmfrt:
            mx_cmfrt = cmfrt
            insrt_pst = i
    reached_list.insert(insrt_pst,A_list[j])
    counter += mx_cmfrt
    mx_cmfrt = 0
print(counter)

# Optimized solution
import math
N = int(eval(input()))
A = list(map(int,input().split()))
A.sort(reverse = True)
score = A[0]
for i in range(1, math.ceil(N*0.5)):
    if N - i * 2 != 1:
        score += 2 * A[i]
    else:
        score += A[i]
print(score)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
for i in range(2, N):
    x, y = max_edge_list[0]
    ans += edge[(x, y)]
    del edge[(x, y)]
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    max_edge_list.remove((x, y))
print(ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
idx = 0
for i in range(2, N):
    x, y = max_edge_list[idx]
    ans += edge[(x, y)]
    #del edge[(x, y)]
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    #max_edge_list.pop(0)
    idx += 1
print(ans)
# Original solution
import heapq

N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
h = [-A[0]]
ans = 0
for i in range(1,N):
    ans -= heapq.heappop(h)
    heapq.heappush(h, -A[i])
    heapq.heappush(h, -A[i])
print(ans)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
AA = []
for i in range(N):
    if i == 0:
        AA.append(A[i])
    else:
        AA.append(A[i])
        AA.append(A[i])
AA.sort(reverse=True)
ans = 0
for i in range(N-1):
    ans += AA[i]
print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
a.sort(reverse=True)
l=[a[0]]
ans=0
for aa in a[1:]:
  ans+=max(l)
  l.remove(max(l))
  l.append(aa)
  l.append(aa)
print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
w=n//2
for aa in a[1:w]:
  ans+=aa*2
if n%2!=0:
  ans+=a[w]
print(ans)
# Original solution
n,*a=list(map(int,open(0).read().split()))
a=a+a
a.sort()
print((sum(a[-n:-1])))

# Optimized solution
n,*a=list(map(int,open(0).read().split()));print((sum(sorted(a*2)[-n:-1])))
# Original solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)

			pgt.put(val)
			pgt.put(val)
	print((sum(ansans)))


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)
			pgt.put(val)
			pgt.put(val)

	print((sum(ansans)))


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()
# Original solution
def d_chat_in_a_circle():
    N = int(eval(input()))
    A = sorted([int(i) for i in input().split()], reverse=True)

    ans = [A[0]]
    j = 1
    while True:
        ans.extend([A[j]] * 2)
        j += 1
        if len(ans) > N - 1:
            ans.pop()
            break
    if N % 2 == 0:
        ans.pop()
    return sum(ans)

print((d_chat_in_a_circle()))

# Optimized solution
def d_chat_in_a_circle():
    N = int(eval(input()))
    A = sorted([int(i) for i in input().split()])
    return sum(A[(N - 1) - (k // 2)] for k in range(1, N))

print((d_chat_in_a_circle()))
# Original solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]

# val next
l=[(-1,-1)]*(n+1)
l[0]=(a[0],0)
nx_idx=1

ans = 0

for i in range(1,n):
  aa = a[i]
  now = 0
  mx_idx=0
  mx=-1

  cnt=0
  while True:
    cnt+=1

    val = l[now][0]
    nxt = l[now][1]

    nxtv=0
    nxtv=l[nxt][0]
    
    n_val = min(val,nxtv)

    if mx < n_val:
      mx = n_val
      mx_idx = now

    if nxt == a[i]:
      break
    
    if cnt==nx_idx:
      break

    now = nxt

  ans+=mx

  val = l[mx_idx][0]
  nxt = l[mx_idx][1]

 
  l[nx_idx]=(a[i],l[mx_idx][1])
  l[mx_idx]=(mx,nx_idx)

  nx_idx+=1
print(ans)


# Optimized solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]
 
ans=0

cur=0
now=1
for i in range(n-1):
  ans+=a[cur]
  if now==1:
    now=0
    cur+=1
  elif now==0:
    now=1


print(ans)
# Original solution
N = int(eval(input()))
As = sorted(list(map(int, input().split())), reverse = True)

points = []
for i in range(N):
  points = points + [As[i]] * (i+1)
print((sum(points[:N-1])))

# Optimized solution
N = int(eval(input()))
As = sorted(list(map(int, input().split())), reverse = True)
ans = As[0]
N -= 2

p = N // 2
ans += sum(As[1:1+p]) * 2

if N % 2 == 1:
  ans += As[1+p]

print(ans)
# Original solution
N = int(eval(input()))

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
for i in range(N-1):
  if i == 0 or i%2 == 1:
    m = max(A)
    result += m
    A.remove(m)
  else:
    result += m
    
print(result)

# Optimized solution
N = int(eval(input()))

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
A.sort(reverse=True)
result += A[0]
A.pop(0)
if N % 2 == 0:
  result += sum(A[:(int(N/2)-1)])*2
else:
  result += (sum(A[:(int((N-1)/2)-1)])*2 + A[int((N-1)/2)-1])
    
print(result)
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))

current_h = [0] * N
c = 0

l = 0
while True:
    escape_loop = False
    while current_h[l] == h[l]:
        if l + 1 < N:
            l += 1
        else:
            escape_loop = True
            break
    if escape_loop:
        break

    r = l
    while True:
        if r + 1 < N and current_h[r + 1] < h[r + 1]:
            r += 1
        else:
            break

    for i in range(l, r + 1):
        current_h[i] += 1
    c += 1

print(c)


# Optimized solution
N = int(eval(input()))
h = list(map(int, input().split()))

s = h[0]
for i in range(1, N):
    s += max(0, h[i] - h[i - 1])

print(s)
# Original solution
N = int(eval(input()))
L = list(map(int,input().split()))
ans = 0
def zero_spliter(L):
    cnt = 1
    for x in L:
        if x == 0:
            cnt += 1
    M = [[] for _ in range(cnt)]
    i = 0
    for x in L:
        if x != 0:
            M[i].append(x)
        else:
            i += 1
    for x in M:
        if x != []:
            yield x
ans = 0
def counter(L):
    global ans
    if L == []:
        pass
    if 0 in L:
        l = list(zero_spliter(L))
        for x in l:
            counter(x)
    else:
        M = [x-1 for x in L]
        ans += 1
        counter(M)
counter(L)
print(ans)


# Optimized solution
N = int(eval(input()))
L = list(map(int,input().split()))

def count(L):
    if len(L) == 1:
        return L[0]
    if L == []:
        return 0
    if 0 in L:
        i = L.index(0)
        return count(L[:i]) + count(L[i+1:])
    else:
        m = min(L)
        L = list([x-m for x in L])
        return m + count(L) 
print((count(L)))
# Original solution
def main():
    N = int(eval(input()))
    h = list(map(int, input().split()))
    
    ans = 0
    while sum(h) > 0:
      ans += 1
      flag = False
      for i in range(N):
        if h[i] > 0:
          h[i] -= 1
          flag = True
        elif flag is True:
          break

    print(ans)
        
    
if __name__ == '__main__':
    main()

# Optimized solution
N, *h = list(map(int, open(0).read().split()))
ans = sum([max(h[i + 1] - h[i], 0) for i in range(N - 1)])
print((ans + h[0]))
# Original solution
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    h=list(map(int,readline().split()))

    count=0
    while 1:
        first=True
        for i in range(N):
            if first and h[i]>=1:
                first=False
                h[i]-=1
            elif first and h[i]==0:
                pass
            elif first is False and h[i]>=1:
                h[i]-=1
            elif first is False and h[i]==0:
                break
        if first:
            break
        else:
            count+=1
    print(count)

if __name__=="__main__":
    main()

# Optimized solution
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    h=list(map(int,readline().split()))
    ans=0
    while True:
        flag=True
        flag2=True
        for i in range(n):
            if h[i]>0:
                h[i]-=1
                flag=False
                flag2=False
                if i==n-1:
                    ans+=1
            else:
                if flag2==False:
                    flag2=True
                    ans+=1
        if flag: break

    print(ans)

if __name__=="__main__":
    main()
# Original solution
# import sys
# input = sys.stdin.readline
# import re
import collections
import bisect
import math
import fractions
import collections
import itertools
from functools import reduce

def main():
    n = int(eval(input()))
    h = list(input_list())
    count = 0
    while True:
        if list(set(h)) == [0]:
            break
        flag = False
        count += 1
        for i, v in enumerate(h):
            if v != 0:
                flag = True
                h[i] -= 1
            else:
                if flag:
                    break
    print(count)

def minus(numbers):
    for i, n in enumerate(numbers):
        if n == 0:
            break
        numbers[i] -= 1
    return numbers


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == '__main__':
    main()



# Optimized solution
# import sys
# input = sys.stdin.readline
import collections

def main():
    n = int(eval(input()))
    h = input_list()
    count = 0
    while True:
        if list(set(h)) == [0]:
            break
        count += 1
        f = False
        for i, v in enumerate(h):
            if v == 0:
                if f:
                    break
            if v > 0:
                h[i] -= 1
                f = True
    print(count)


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()
# Original solution
n=int(eval(input()))
h=[0]+list(map(int,input().split()))+[0]

cnt=0
while any([i!=0 for i in h]):
    start,end=100,100
    for i in range(n+2):
        if h[i]!=0:
            start=min(start,i)
        elif h[i]==0 and start<i:
            end=min(end,i)
    l=[i-1 for i in h[start:end]]
    h=h[:start]+l+h[end:]
    cnt+=1
print(cnt)

# Optimized solution
n=int(eval(input()))
h=[0]+list(map(int,input().split()))+[0]

cnt=0
while any([i!=0 for i in h]):
    start,end=100,100
    for i in range(n+2):
        if h[i]!=0:
            start=min(start,i)
        elif h[i]==0 and start<i:
#            end=min(end,i)
            end=i
            break
    l=[i-1 for i in h[start:end]]
    h=h[:start]+l+h[end:]
    cnt+=1
print(cnt)
# Original solution
n = int(eval(input()))
li = list(map(int,input().split()))
all_li = [li]

def conv(target_list):
    count = 0
    flag = True
    while flag:
        all_tmp = []
        for each_list in target_list:
            tmp = []
            m = min(each_list)
            count += min(each_list)
            #print(each_list)
            for e in each_list:
                if e-m != 0:
                    tmp.append(e-m)
                else:
                    if tmp:
                        all_tmp.append(tmp)
                        tmp = []
            if tmp:
                all_tmp.append(tmp)
        if len(all_tmp) == 0:
            flag = False
        target_list = all_tmp
    return count

print((conv(all_li)))

# Optimized solution
n = int(eval(input()))
li = list(map(int,input().split()))
last = li[0]
count = li[0]

for i,l in enumerate(li):
    if i == 0:
        continue
    if l > last:
        count += l - last
    last = l
print(count)
# Original solution
N = int(eval(input()))
H = list(map(int,input().split()))
H = [0]+H+[0]
def trm(li):
  for i in range(len(li)-2,0,-1):
    if li[i-1]<=li[i]<=li[i+1]:
      li.pop(i)
    elif li[i-1]>=li[i]>=li[i+1]:
      li.pop(i)
  return H
H = trm(H)
cnt = 0
for i in range(max(H),0,-1):
  flg = False
  for j in range(len(H)):
    if H[j]==i:
      if not flg:
        cnt +=1
        flg = True
      H[j]=H[j]-1
    else:
      flg = False
print(cnt)

# Optimized solution
#!/usr/bin python3
# -*- coding: utf-8 -*-

def cnt(A, x):
    N = len(A)
    i = 0
    ret = 0
    while i<N:
        if A[i]<x:
            i += 1
        else:
            ret += 1
            while i<N and A[i]>=x:
                i += 1
    return ret

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))

    ret = 0
    for i in range(1,101):
        ret += cnt(A,i)

    print(ret)

if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
h = list(map(int,input().split()))
h = [0] + h
ans = 0

while(sum(h)>0):
    for i in range(N):     
        if h[i] == 0 and h[i+1] > 0:
            ans += 1
    for i in range(N+1):
        if h[i] > 0:
            h[i] -= 1
print(ans)  

# Optimized solution
N = int(eval(input()))
h = list(map(int,input().split()))
h = [0] + h
ans = 0

for i in range(N):
    if h[i]<h[i+1]:
        ans += h[i+1]-h[i]
print(ans)
# Original solution
def main():
    n, *h = list(map(int, open(0).read().split()))
    ans = 0
    f = True
    base = 0
    for i, j in zip(h, h[1:]):
        g = i <= j
        if f and not g:
            ans += i - base
        if not f and g:
            base = i
        f = g
    else:
        if f:
            ans += h[-1] - base
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n, *h = list(map(int, open(0).read().split()))
    ans = 0
    for i, j in zip([0] + h, h):
        if i < j:
            ans += (j - i)
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
def main():
    n = int(eval(input()))
    h_li = list(map(int, input().split()))
    i = 0
    count = 0
    f = 0
    discount = max(h_li)
    while True:
        if h_li[i] >= discount:
            count += 1
            h_li[i] -= 1
            while i < len(h_li) - 1:
                if h_li[i + 1] >= discount:
                    i += 1
                    h_li[i] -= 1
                else:
                    i += 1
                    break
        if i == len(h_li) - 1:
            i = 0
            discount -= 1
            if max(h_li) == 0:
                f = 1
        else:
            i += 1
        if f == 1:
            break
    print(count)
if __name__ == '__main__':
    main()

# Optimized solution
n = int(eval(input()))
h_li = list(map(int, input().split()))
count = 0
for i in range(100):
    f = 0
    for j in range(n):
        if h_li[j] >= i + 1:
            if f == 0:
                count += 1
                f = 1
        else:
            f = 0
print(count)
# Original solution
N = int(eval(input()))
h = list(map(int,input().split()))
count = 0
i = 0
while sum(h) > 0:
    if h[i] > 0:
        j = i
        count += 1
        while j < N and h[j] > 0:
            h[j] = h[j] - 1
            j += 1
    else:
        i += 1
print(count)

# Optimized solution
N = int(eval(input()))
h = list(map(int, input().split()))
cnt = 0
i = 0
for _ in range(100 * 100):
    if h[i] > 0:
        j = i
        cnt += 1
        while j < N and h[j] > 0:
            h[j] = h[j] - 1
            j += 1
    else:
        i += 1
        if i == N:
            break
print(cnt)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

c = 0
while any(h):
    ans = 0
    for l in range(n):
        tmp = 0
        r = l
        while h[r]:
            tmp += 1
            r += 1
            if r == n:
                break
        if ans < tmp:
            x = l
            y = r
            ans = tmp
    for i in range(x, y):
        h[i] -= 1
    c += 1
print(c)


# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))
ans = h[0]
for i in range(n-1):
    ans += max(h[i+1]-h[i], 0)

print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int,input().split()))
ans = h[0]
for i in range(1,n):
    if h[i-1]<h[i]: ans += h[i]-h[i-1]
print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int,input().split()))
ans = h[0]
for i in range(1,n):
    if h[i]>h[i-1]:ans+=h[i]-h[i-1]
print(ans)
# Original solution
n = int(eval(input()))
l = list(map(int, input().split()))
l = [0] + l + [0]

ans = 0
for i in range(1, max(l)+1):
  g = 0
  for j in range(1, n+1):
    if l[j] >= i and l[j-1] < i:
      g += 1
  ans += g
print(ans)

# Optimized solution
n = int(eval(input()))
H = list(map(int, input().split()))
H = [-1]+H+[-1]
ans = 0
for i in range(1, 101):
    for j in range(1, n+1):
        if H[j] >= i and H[j-1] < i:
            ans += 1
print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
while sum(h) != 0:
  flag = False
  for i in range(n):
    if h[i] != 0: flag = True
    if flag == True and h[i] == 0: break
    elif flag == False and h[i] == 0: continue
    h[i] -= 1
  ans += 1
print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
while max(h) != 0:
  if h[0] > 0: flag = True
  else: flag = False
    
  if flag:
    for i in range(n):
      if h[i] == 0: break
      h[i] -= 1
    ans += 1
  else:
    for i in range(1,n):
      if flag == False and h[i] == 0: continue
      if flag == True and h[i] == 0: break
      flag = True
      h[i] -= 1
    ans += 1
      
print(ans)
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    while True:
        if H[i] <= 0:
            break

        base = H[i]
        for j in range(i+1, N):
            # 花の高さが0なら初期のまま
            if H[j] <= 0:
                break

            # 次の花(j+1)が基準の花(j)以下か
            if base <= H[j]:
                H[j] -= 1
            else:
                break
        H[i] -= 1
        ans += 1

ans += max(0, H[-1])
print(ans)


# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    while True:
        if H[i] == 0:
            break

        for j in range(i+1, N):
            if H[j] == 0:
                break

            H[j] -= 1
        H[i] -= 1
        ans += 1
ans += max(0, H[-1])
print(ans)
# Original solution
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
hl = LI()
wl = []
count = 0

len_h = len(hl)

def m1(n):
    if n > 0:
        n = n - 1
    else:
        n = 0
    return n

max_hana = max(hl)
if max_hana == 0:
    print((0))
    sys.exit()

for i in range(max_hana):
    for j in range(len_h):
        if j == 0:
            if hl[j] > 0:
                count += 1
        else:
            if hl[j] > 0 and hl[j-1] == 0:
                count += 1
            elif hl[j] > 0 and hl[j-1] == 0:
                count += 1
    hl = list(map(m1, hl))

print(count)

# Optimized solution
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
hl = LI()
# count = 0

# len_h = len(hl)

# def m1(n):
#     if n > 0:
#         n = n - 1
#     else:
#         n = 0
#     return n

# max_hana = max(hl)
# if max_hana == 0:
#     print(0)
#     sys.exit()

# for i in range(max_hana):
#     for j in range(len_h):
#         if j == 0:
#             if hl[j] > 0:
#                 count += 1
#         else:
#             if hl[j] > 0 and hl[j-1] == 0:
#                 count += 1
#             elif hl[j] > 0 and hl[j-1] == 0:
#                 count += 1
#     hl = list(map(m1, hl))

# print(count)

######################
# 隣との差分を考えたほうが早い

# 1    2    4    2    1    0      計4
#   1    2    0    0     0

ans = hl[0]
for i in range(1, n):
    ans += max(0, hl[i] - hl[i - 1])
print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int,input().split()))

ans = 0
for i in range(max(h)):
    c = 0
    for j in range(n):
        if h[j] != 0:
            h[j] -= 1
            c += 1
        else:
            if c != 0:
                ans += 1
                c = 0
        if j == n-1 and c != 0:
            ans += 1
print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int,input().split()))

ans = 0
c = 0
while max(h) != 0:
    flag = False
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            flag = True
            if i == n-1:
                ans += 1
                c += 1
        elif flag:
            flag = False
            ans += 1
print(ans)