Problem p03147 (python)

Fixed submission:
import sys
L = int(sys.stdin.readline().rstrip())
 
h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = h[0]
elif L == 2:
  ans = max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
  ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
L = int(sys.stdin.readline().rstrip())
 
if L == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = h
elif L == 2:
  ans = max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
L = int(sys.stdin.readline().rstrip())
 
if L == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = h
elif L == 2:
  ans = max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
L = int(sys.stdin.readline().rstrip())
 
if L == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = N - h
elif L == 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
L = int(sys.stdin.readline().rstrip())

if L == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))

L = N
if L == 0:
  ans = 0
elif L == 1:
  ans = N - h
elif L == 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
N = int(sys.stdin.readline().rstrip())

if N == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))

L = N
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
N = int(sys.stdin.readline().rstrip())
h = list(map(int, sys.stdin.readline().rstrip().split()))
 
L = N
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(sys.stdin.readline().rstrip())
h = map(int, sys.stdin.readline().rstrip().split())

L = len(h)
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  if grad1:
    p = [h[0]]
  else:
    p = []
    
  v = []
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p.append(h[n-1])
    else:
      v.append(h[n-1])
    grad1 = grad2
               
  if not grad1:
    p.append(h[n])
 
ans = sum(p) - sum(v)
print(ans)