Harryxun/stf_run huggingface dataset:
# D Patisserie
#入力を取得
N,M=map(int,input().split())
check=M+10 if M+10<=N else N
xyz_list,x_list,y_list,z_list=[],[],[],[]
for i in range(N):
    xyz_list.append(input().split())
    
var_list=[x for x in itl.product([-1,1],repeat=3)]
sum_list=[]
for i in var_list:
    print(i,xyz_list)
    sort_list=[0,]*N
    for j in range(N):
        sort_list[j]=int(xyz_list[j][0])*i[0]+ int(xyz_list[j][1])*i[1]+ int(xyz_list[j][2])*i[2]
    sort_list.sort(reverse=True)
    sum_=0
    print(sort_list)
    for j in range(M):
        sum_+=sort_list[j]
    sum_list.append(sum_)
    
sum_list.sort(reverse=True)
print(sum_list[0])

### FIXED CODE ###

# D Patisserie
import itertools as itl
#入力を取得
N,M=map(int,input().split())
check=M+10 if M+10<=N else N
xyz_list,x_list,y_list,z_list=[],[],[],[]
for i in range(N):
    xyz_list.append(input().split())
    
var_list=[x for x in itl.product([-1,1],repeat=3)]
sum_list=[]
for i in var_list:
    sort_list=[0,]*N
    for j in range(N):
        sort_list[j]=int(xyz_list[j][0])*i[0]+ int(xyz_list[j][1])*i[1]+ int(xyz_list[j][2])*i[2]
    sort_list.sort(reverse=True)
    sum_=0
    for j in range(M):
        sum_+=sort_list[j]
    sum_list.append(sum_)
    
sum_list.sort(reverse=True)
print(sum_list[0])
def eratosthenes_2017like(x):
    prime = []
    if x < 3:
        return prime
    search = [i for i in range(2,x+1)]
    while search[0] <= math.sqrt(x):
        add = search.pop(0)
        prime.append(add)
        multiple = 1
        while add*multiple <= x:
            multiple += 1
            try:
                search.remove(add*multiple)
            except ValueError:
                pass
    prime.extend(search)

    for p in prime[::-1]:
        if not((p+1)/2 in prime):
            prime.remove(p)
    return prime

for i in range(q):
    l, r = map(int,input().split())
    left_len = len(eratosthenes_2017like(l-1))
    right_len = len(eratosthenes_2017like(r))
    print(right_len - left_len)

### FIXED CODE ###

sieve = [True] * 100001
#one-based-index
def mark(sieve,x):
    for i in range(x+x,len(sieve),x):
        sieve[i] = False

for x in range(2,int(len(sieve) ** 0.5) + 1):
    if sieve[x]: mark(sieve,x)

sieve[0] = False
sieve[1] = False

for i in range(len(sieve)-2,2,-2):
    if sieve[i]:
        if not(sieve[(i+1)//2]):
            sieve[i] = False #2017likeでない

sieve[2] = False

cnt = [0] * 100002
for i,s in enumerate(sieve):
    if s:
        cnt[i] = cnt[i-1] + 1
    else:
        cnt[i] = cnt[i-1]

q = int(input())

for i in range(q):
    l, r = map(int,input().split())
    print(cnt[r] - cnt[l-1])
s = input()
print(s[0]+len(s)+s[-1])

### FIXED CODE ###

n,m = map(int,input().split())
print((n-1)*(m-1))
X, Y, Z, K = map(int, input().split())

A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a + 1) + (b + 1) > K:
            break
        for c in range(Z):
            if (a + 1) + (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])


### FIXED CODE ###

X, Y, Z, K = map(int, input().split())

A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i] + B[j])

AB.sort(reverse=True)

ABC = []
for i in range(min(K, len(AB))):
    for j in range(Z):
        ABC.append(AB[i] + C[j])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    abc = []
    for i in range(M):
        a, b = map(int, input().split())
        c = np.array(list(map(int, input().split()))) - 1
        abc.append((a, b, c))
    abc = sorted(abc)
    INF = int(1e10)
    dp = np.full(N, INF, dtype=int)

    ans = 0
    for i in range(M):
        print(dp)
        a, b, c = abc[i]
        if any(dp[c] == INF):
            dp[c] = np.minimum(dp[c], a)
            ans += a

    ok = True
    for i in range(N):
        if dp[i] == INF:
            ok = False
            break
    
    if ok:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
import numpy as np
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    INF = int(1e10)
    dp = [INF] * (1 << N)
    dp[0] = 0
    bits = set()
    bits.add(0)
    for i in range(M):
        a, b = map(int, input().split())
        c = list(map(int, input().split()))
        key = 0
        for t in c:
            key += 1 << (t - 1)
        add_bits = set()
        for bit in bits:
            tmp = bit | key
            if dp[bit] + a < dp[tmp]:
                dp[tmp] = dp[bit] + a
                add_bits.add(tmp)
        bits |= add_bits
    
    if dp[(1 << N) - 1] == INF:
        print(-1)
    else:
        print(dp[(1 << N) - 1])

if __name__ == "__main__":
    main()
import sys

n, y = input().split()
n = int(n)
y = int(y)

for a in range(n+1):
	for b in range(n+1-a):
		for c in range(n+1-a-b):
            if a + b + c < n:
              continue
            if a + b + c > n:
              break
			if (a + b + c == n) and (a * 10000 + b * 5000 + c * 1000 == y):
				print(str(a) + ' ' + str(b) + ' ' + str(c))
				sys.exit()

print('-1 -1 -1')

### FIXED CODE ###

import sys

n,y = map(int, input().split())

for a in range(n+1):
	for b in range(n+1-a):
		c = n - a - b
		if (a * 10000 + b * 5000 + c * 1000 == y):
				print(str(a) + ' ' + str(b) + ' ' + str(c))
				sys.exit()
	
print('-1 -1 -1')
sys.exit()
n=int(input())
name=['a','b','c','d','e','f','g','h','i','j','k','l','n','m','o','p','q','r','s','t','u','w','x','y','z']
result=''
if n<27:
    print(name[n-1])
else:
    while n>26:
        a=n%26
        result+=name[a-1]
        n//=26
    result+=name[n-1]
    print(result[::-1])


### FIXED CODE ###

n=int(input())
result=''
while n>0:
    n-=1
    result+=chr(ord('a')+n%26)
    n//=26
print(result[::-1])
input()
data=list(map(int,input().split()))
count=0
for i in range(len(data)):
    if data[i]==i+1:
        data[i],data[i+1]=data[i+1],data[i]
        count+=1
print(count)

### FIXED CODE ###

input()
data=list(map(int,input().split()))
count=0
for i in range(len(data)-1):
    if data[i]==i+1:
        data[i],data[i+1]=data[i+1],data[i]
        count+=1
if data[-1]==len(data):
    count+=1
print(count)
date = input()
dic ={"SUN":0, "SAT":6, "FRI":5, "TUE":4, "WED":3, "THU":2, "FRI":1}

print(7 - dic[date])

### FIXED CODE ###

s = input()
week = ["SAT","FRI","THU","WED","TUE","MON","SUN"]
days = week.index(s) + 1
print(days)
a =listmap(int, input().split()))
print(a[0]*a[1], 2(a[0]+a[1]))


### FIXED CODE ###

a,b = [int(x) for x in input().split()]
print(a*b,2*(a+b))
def solve():
    global S, N
    N = int(input())

    S = []
    for n in range(N):
        S.append(list(input()))


    res = 0
    for n in range(N):
        if check(n):
            res += N

    print(res)

def check(move):
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-move)%Nj] != S[(j-move)%N][i]:
                return False
    return True


if __name__ == "__main__":
    solve()

### FIXED CODE ###

def solve():
    global S, N
    N = int(input())

    S = []
    for n in range(N):
        S.append(list(input()))


    res = 0
    for n in range(N):
        if check(n):
            res += N

    print(res)

def check(move):
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-move)%N][j] != S[(j-move)%N][i]:
                return False
    return True


if __name__ == "__main__":
    solve()
a1=input()
a2=[i for i in a1.split()]
a3,a4=[res[i] for i in (0,1)]
A,B=int(a3),int(a4)
if A>2*B:
    print(A-(2*B))
elif A<2*B:
    print('0')

### FIXED CODE ###

a1=input()
a2=[i for i in a1.split()]
a3,a4=[a2[i] for i in (0,1)]
A,B=int(a3),int(a4)
print(max(0,A-2*B))
L, R= map(int,input().split())
m = 2019
lis = list(range(L,R+1))
lis2 = [i%m for i in lis]
lis2.sort()
print(lis2[0]*lis2[1])

### FIXED CODE ###

L, R= map(int,input().split())
m = 2019
ans = 0
if R-L < m:
    ans = m
    L = L%m
    R = R%m
    lis = list(range(L,R+1))
    lis2 = [i%m for i in lis]
    for i in range(len(lis2)):
        for j in range(i+1,len(lis2)):
            if ans > (lis2[i]*lis[j])%m:
                ans = (lis2[i]*lis[j])%m
print(ans)
a,b,c,d,e,f = map(int, input().split())
g = max(a, b)
h = min(a, b)
salt = []
water = []
concentration = 0

for i in range(int(e*f/(c*100)) + 1):
    j = 0
    while e*f/100 >= c*i +d*j:
        salt.append(c*i+d*j)
        j += 1
salt.sort()

for i in range(int(f/(100*g))+1):
    j = 0
    while f >= 100*(g*i+h*j):
        water.append(100*(g*i+h*j))
        j += 1
water.sort()
water = water[1:]

for i in range(len(water)):
    addsalt = min(water[i] * e // 100, f - water[i])
    while addsalt not in salt:
        addsalt -=1
    if concentration < addsalt / (water[i] + addsalt):
        concentration = addsalt / (water[i] + addsalt)
        salt1 = addsalt
        water1 = water[i]

print(salt1+water1, salt1)

### FIXED CODE ###

a,b,c,d,e,f = map(int, input().split())
g = max(a, b)
h = min(a, b)
salt = []
water = []
concentration = 0

for i in range(int(e*f/(c*100)) + 1):
    j = 0
    while e*f/100 >= c*i +d*j:
        salt.append(c*i+d*j)
        j += 1
salt.sort()

for i in range(int(f/(100*g)) + 1):
    j = 0
    while f >= 100*(g*i+h*j):
        water.append(100*(g*i+h*j))
        j += 1
water.sort()
water = water[1:]

for i in range(len(water)):
    addsalt = min(water[i] * e // 100, f - water[i])
    while addsalt not in salt:
        addsalt -=1
    if concentration < addsalt / (water[i] + addsalt):
        concentration = addsalt / (water[i] + addsalt)
        salt1 = addsalt
        water1 = water[i]

try:
    print(salt1+water1, salt1)
except NameError:
    print(water[0], 0)
S = input()

find_flag = 0
rest_S = S
while True:
    if len(rest_S) == 0:
        find_flag = 1
        break
    elif rest_S[-5:] in candidates:
        rest_S = rest_S[:-5]
    elif rest_S[-6:] in candidates:
        rest_S = rest_S[:-6]
    else:
        break

if find_flag:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

S = input()

candidates = ["dream", "dreamer", "erase", "eraser"]

find_flag = 0
rest_S = S
while True:
#     print(rest_S[-5:], rest_S[-7:]) 
    if len(rest_S) == 0:
        find_flag = 1
        break
    elif (len(rest_S) >= 7) & (rest_S[-7:] in candidates):
        rest_S = rest_S[:-7]
    elif (len(rest_S) >= 6) & (rest_S[-6:] in candidates):
        rest_S = rest_S[:-6]
    elif (len(rest_S) >= 5) & (rest_S[-5:] in candidates):
        rest_S = rest_S[:-5]
    else:
        break

if find_flag:
    print("YES")
else:
    print("NO")
N = int(input())
H = list(map(int, input().split()))


def move_number(number_list):
    n = len(number_list)
    result = 0
    for i in range(n - 1):
        if number_list[i + 1] <= number_list[i]:
            result += 1
        else:
            return result
    return result


def all_move_numbers(number_list):
    result = []
    n = len(number_list)
    for i in range(n - 1):
        _number_list = number_list[i:]
        result.append(move_number(_number_list))
    return result


def max_number(number_list):
    result = max(all_move_numbers(number_list))
    return result


print(max_number(H))

### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))


def decreasing(number_list):
    n = len(number_list) - 1
    result = ["F"] * n
    for i in range(n):
        if number_list[i + 1] <= number_list[i]:
            result[i] = "T"

    return result


print(max(map(len, "".join(decreasing(H)).split("F"))))
w, h = map(int, raw_input().split())
p = []
q = []
for i in xrange(w):
    p.append(int(raw_input()))
for i in xrange(h):
    q.append(int(raw_input()))

wl = w
hl = h

cost = 0

p.sort()
q.sort()

p.append(10 ** 8)
q.append(10 ** 8)

pc = 0
qc = 0

while(wl + hl - pc - qc> 0):
    if p[pc] < q[qc]:
        cost += (hl - qc + 1) * p[pc]
        pc += 1
    else:
        cost += (wl - pc + 1) * q[qc]
        qc += 1

print cost



### FIXED CODE ###

s = raw_input()
st = "CODEFESTIVAL2016"
count = 0
for (sl, stl) in zip(s, st):
    if sl != stl:
        count += 1
print count
a,b,c =int(input())

if a=<b:
  ans=c
else:
  ans=c-(a-b)
  if ans<0:
    ans=0
    
  
print(ans)

### FIXED CODE ###

a,b,c = map(int,input().split())
ans = 0

if a <= b:
    ans = c
else:
	ans = c-(a-b)
	if ans < 0:
		ans = 0
    
print(ans)
all,rolls=map(int,input().split())
List=list(map(int,input().split()))

sum = 0
sumList =[]

for i in range(all - rolls + 1):
	sum = 0
	for j in range(rolls):
		sum = sum + (List[i + j] + 1) / 2
	sumList.append(sum)

sumList.sort(reverse=True)
print(sumList[0])

### FIXED CODE ###

a,b=input().split()
c,d=map(int,input().split())
e=input()

if a==e:
	c = c - 1
else:
	d = d - 1

print(c,d)
import sys

n = int(sys.argv[1])
total = 0

def gcd(a,b):
	if b == 0:
		return a

	return gcd(b, a % b)

total = total + n * (n+1) / 2

for i in range(n):
	for j in range(i+1,n):
		total = total + 6 * gcd(j+1,i+1)

for i in range(n):
	for j in range(i+1,n):
		for k in range(j+1,n):
			l = gcd(j+1,k+1)
			if i < l:
				total = total + 6 * gcd(l,i+1)
			else:
				total = total + 6 * gcd(i+1,l)

print(int(total))

### FIXED CODE ###

n = int(input())
total = 0

#a > b
def gcd(a,b):
	if b == 0:
		return a

	return gcd(b, a % b)

total = total + n * (n+1) / 2

for i in range(n):
	for j in range(i+1,n):
		total = total + 6 * gcd(j+1,i+1)

for i in range(n):
	for j in range(i+1,n):
		for k in range(j+1,n):
			l = gcd(j+1,k+1)
			if i < l:
				total = total + 6 * gcd(l,i+1)
			else:
				total = total + 6 * gcd(i+1,l)

print(int(total))
import pi
a = float(raw_input())
print"%.6f %.6f" %(pi*a**2,2*pi*a)

### FIXED CODE ###

import math
a = float(raw_input())
print"%.6f %.6f" %(math.pi*a**2,2*math.pi*a)
import math as m

Z = 10 ** 9 + 7

def conb(S, i):
    if (i == 1):
        return 1
    elif (i == 2):
        return S - 1
    elif (i == 3):
        return (S - 1) * (S - 2) // 2
    else:
        n = 0
        for j in range(1, S - i + 2):
            n += conb(S - j, i - 1)
        return n

def calc(N, K):
    M = N - K + 1
    M_fact = m.factorial(M)
    result = ''
    for i in range(1, K + 1):
        val = m.factorial(M) // m.factorial(i) // m.factorial(M - i) * conb(K, i)
        result += "{}\n".format(
            val if val < Z else val % Z
            )
    print(result)

s = input().split(' ')
N = int(s[0])
K = int(s[1])

calc(N, K)

### FIXED CODE ###

import math as m

Z = 10 ** 9 + 7


def calc(N, K):
    M = N - K + 1
    M_fact = m.factorial(M)
    K1_fact = m.factorial(K - 1)
    result = ''
    for i in range(1, K + 1):
        if (M - i < 0):
            val = 0
        else:
            val = M_fact // m.factorial(i) // m.factorial(M - i) * \
                K1_fact // m.factorial(i - 1) // m.factorial(K - i)
        result += "{}\n".format(
            val if val < Z else val % Z
        )
    print(result)


s = input().split(' ')
N = int(s[0])
K = int(s[1])

calc(N, K)
import math
import random


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

l = [0]*(n+2)
r = [0]*(n+2)
m = [0]*(n+2)

for i in range(n+1):
    j = n-i
    if i == 0:  # j = n
        l[i] = 0
        r[j+1] = 0
    else:
        l[i] = math.gcd(l[i-1], a[i-1])
        r[j] = math.gcd(r[j+1], a[j])

m = [math.gcd(l[i], r[i+1]) for i in range(n+1)]

print(max(m))




#
# tmpgcd = list()
#
# for i in range(2):
#     sample = random.choices(a, k=2)
#     tmpgcd.append(computeGCD(sample[0], sample[1]))
# tmpgcd = sorted(tmpgcd)
#
# # print('hey')
#
# mini = tmpgcd[1]
#
#
# def computeGCD(x, y):
#     if x > y:
#         small = y
#     else:
#         small = x
#     for i in range(1, small + 1):
#         if ((x % i == 0) and (y % i == 0)):
#             gcd = i
#
#     return gcd
# # print(mini, tmpgcd)
# result = 0
# for i in range(mini, -1, -1):
#     tmp = [x % i for x in a]
#     tmp = [x for x in tmp if x != 0]
#     if len(tmp) <= 1:
#         result = i
#         break
#
# print(result)
#
#
#
#
#
#


### FIXED CODE ###

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)


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

l = [0]*(n+2)
r = [0]*(n+2)
m = [0]*(n+2)

for i in range(n+1):
    j = n-i
    if i == 0:  # j = n
        l[i] = 0
        r[j+1] = 0
    else:
        l[i] = gcd(l[i-1], a[i-1])
        r[j] = gcd(r[j+1], a[j])

m = [gcd(l[i], r[i+1]) for i in range(n+1)]

print(max(m))
import math
import sys
readline = sys.stdin.readline


def main():
    # input
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    B = [0] * n

    for a in A:
        B[a - 1] += 1

    total = 0
    for b in B:
        if b > 1:
            total += b * (b - 1) // 2

    C = [0] * n
    for i, b in enumerate(B):
        C[i] = total - b + 1

    for a in A:
        print(C[a-1])


if __name__ == '__main__':
    main()


### FIXED CODE ###

n,m = map(int, input().split())
print((n*(n-1)+m*(m-1))//2)
n,k = map(int,input().split())

if k==1 :
  ln=0
elif n>=k and k!=0 :
  if n//k==0:
    ln=0
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)

### FIXED CODE ###

n,k = map(int,input().split())
 
if k==1 :
  ln=0
elif k==0 :
  ln=n
elif n>=k:
  a = n%k
  n=a
  while 1:
    ln = n
    n = abs(a-k)
    if ln<=n:
      break
else:
  while 1:
    ln = n
    n = abs(n-k)
    if ln<=n:
      break
print(ln)
# @author 

import sys


class PIndependentSet:
    def solve(self):
        from collections import defaultdict

        MOD = 10 ** 9 + 7

        def dfs(s):
            done[s] = 1
            dp[s][0] = 1
            dp[s][1] = 1
            for x in adj[s]:
                if done[x]:
                    continue
                _in = True
                dfs(x)
                dp[s][0] = (dp[s][0] * dp[x][1]) % MOD
                dp[s][1] = (dp[s][1] * (dp[x][0] + dp[x][1])) % MOD

        n = int(input())
        adj = defaultdict(list)

        for i in range(n - 1):
            x, y = [int(_) for _ in input().split()]
            x -= 1
            y -= 1
            adj[x].append(y)
            adj[y].append(x)

        dp = [[0, 0] for _ in range(n)]
        done = [0] * n
        dfs(0)

        print(dp[0][0] + dp[0][1])

solver = PIndependentSet()
input = sys.stdin.readline

solver.solve()


### FIXED CODE ###

# @author 

import sys


class PIndependentSet:
    def solve(self):
        from collections import defaultdict
        import sys
        sys.setrecursionlimit(10 ** 5 + 5)
        MOD = 10 ** 9 + 7

        def dfs(s):
            done[s] = 1
            dp[s][0] = 1
            dp[s][1] = 1
            for x in adj[s]:
                if done[x]:
                    continue
                dfs(x)
                dp[s][0] = (dp[s][0] * dp[x][1]) % MOD
                dp[s][1] = (dp[s][1] * (dp[x][0] + dp[x][1])) % MOD

        n = int(input())
        adj = defaultdict(list)

        for i in range(n - 1):
            x, y = [int(_) for _ in input().split()]
            x -= 1
            y -= 1
            adj[x].append(y)
            adj[y].append(x)

        dp = [[0, 0] for _ in range(n)]
        done = [0] * n
        dfs(0)

        print((dp[0][0] + dp[0][1]) % MOD)

solver = PIndependentSet()
input = sys.stdin.readline

solver.solve()
a,b = map(int,input())
ans = a*b
if a>9 or b>9:
  ans = -1
print(ans)

### FIXED CODE ###

a,b = map(int,input().split())
ans = a*b
if a>9 or b>9:
  ans = -1
print(ans)
N = int(input())
a = []
for x in range(N):
	i = x + 1
	a.append(list(input().split()).extend([i]))
print(a)
sorted(a, key=lambda x:(x[0],x[1]), reverse=True)
for review in a:
	print(review[2])


### FIXED CODE ###

N = int(input())
a = []
for x in range(N):
	i = x + 1
	S_i, P_i = input().split()
	a.append([S_i, int(P_i), i])
# print(a)
a = sorted(a, key=lambda x:(x[0],-x[1]))
for review in a:
	print(review[2])
import sys
import numpy as np
import collections

def parse(inp):
    N=int(next(inp))
    H=np.array(next(inp).split(),dtype=int)
    return H
  
def split(H):
    Splited=np.array([],dtype=int)
    SplitPoint=[]
    for i in range(len(H)):
        if(H[i]==0):
            SplitPoint.append(i)
    
    if(SplitPoint==[]):
        Splited=np.array([H])
        return Splited
            
    Splited=np.append(Splited,np.split(H,SplitPoint))
    
            
    
    for i in range(len(Splited)):
        if(Splited[i].size==0):
             continue
        elif(Splited[i][0]==0):
            Splited[i]=np.delete(Splited[i],[0])
    
    return Splited
                
def main(H):
    H=collections.deque(H)
#    print(H)

    answer=0
    
    while H:
        Hnow=H.popleft()
        if(Hnow.size==0):
            continue
#        print("now: ",Hnow)
        Hnowmin=Hnow.min()
        answer+=Hnowmin
        for i in range(len(Hnow)):
            Hnow[i]=Hnow[i]-Hnowmin
#        print("decreased: ",Hnow)
        for i in range(len(split(Hnow))):
            if(split(Hnow)[i].size!=0):
#                print("append: ",split(Hnow)[i])
                H.append(split(Hnow)[i])
#        print("H: ",H)
    return answer

print(main(split(parse(sys.stdin))))

### FIXED CODE ###

import sys
import numpy as np
import collections

def parse(inp):
    N=int(next(inp))
    H=np.array(next(inp).split(),dtype=int)
    return H
  
def split(H):
    Splited=np.array([],dtype=int)
    SplitPoint=[]
    for i in range(len(H)):
        if(H[i]==0):
            SplitPoint.append(i)
    
    if(SplitPoint==[]):
        Splited=np.array([H])
        return Splited
            
    Splited=np.split(H,SplitPoint)
    
            
    
    for i in range(len(Splited)):
        if(Splited[i].size==0):
             continue
        elif(Splited[i][0]==0):
            Splited[i]=np.delete(Splited[i],[0])
    
    return Splited
                
def main(H):
    H=collections.deque(H)
#    print(H)

    answer=0
    
    while H:
        Hnow=H.popleft()
        if(Hnow.size==0):
            continue
#        print("now: ",Hnow)
        Hnowmin=Hnow.min()
        answer+=Hnowmin
        for i in range(len(Hnow)):
            Hnow[i]=Hnow[i]-Hnowmin
#        print("decreased: ",Hnow)
        for i in range(len(split(Hnow))):
            if(split(Hnow)[i].size!=0):
#                print("append: ",split(Hnow)[i])
                H.append(split(Hnow)[i])
#        print("H: ",H)
    return answer

print(main(split(parse(sys.stdin))))