n = int(eval(input()))

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

T = [0 for _ in range(n)]

c = 0

while True:

  f = False

  S = {0:1}

  for j in range(n):

    if H[j] > 0:

      S[1] = 1

      H[j] -= 1

      f = True

    elif len(S) > 1 and H[j] == 0:

      break

  if f: c += 1

  if H == T: break

print(c)

n = int(eval(input()))

H = [0] + list(map(int, input().split())) + [0]

T = [0 for _ in range(n+2)]

c = 0

while True:

	s0 = 0; t1 = 0

	for i in range(0, n+2):

		if H[i] > 0 and s0 == 1:

			H[i] -= 1

			t1 = 1

		elif H[i] == 0 and t1 == 1:

			c += 1

			t1 = 0

		elif H[i] == 0:

			s0 = 1

	if H == T: break

print(c)
n = int(eval(input()))
H = list(map(int, input().split()))
T = [0 for _ in range(n)]
c = 0
while True:
    f = False
    S = {0: 1}
    for j in range(n):
        if H[j] > 0:
            S[1] = 1
            H[j] -= 1
            f = True
        elif len(S) > 1 and H[j] == 0:
            break
    if f:
        c += 1
    if H == T:
        break
print(c)

n = int(eval(input()))
H = [0] + list(map(int, input().split())) + [0]
T = [0 for _ in range(n + 2)]
c = 0
while True:
    s0 = 0
    t1 = 0
    for i in range(0, n + 2):
        if H[i] > 0 and s0 == 1:
            H[i] -= 1
            t1 = 1
        elif H[i] == 0 and t1 == 1:
            c += 1
            t1 = 0
        elif H[i] == 0:
            s0 = 1
    if H == T:
        break
print(c)

-H = list(map(int, input().split())) -T = [0 for _ in range(n)] +H = [0] + list(map(int, input().split())) + [0] +T = [0 for _ in range(n + 2)] -    f = False -    S = {0: 1} -    for j in range(n): -        if H[j] > 0: -            S[1] = 1 -            H[j] -= 1 -            f = True -        elif len(S) > 1 and H[j] == 0: -            break -    if f: -        c += 1 +    s0 = 0 +    t1 = 0 +    for i in range(0, n + 2): +        if H[i] > 0 and s0 == 1: +            H[i] -= 1 +            t1 = 1 +        elif H[i] == 0 and t1 == 1: +            c += 1 +            t1 = 0 +        elif H[i] == 0: +            s0 = 1