Problem p03401 (python)

Fixed submission:
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
m = []
for i in range(n):
    if a[i] <= a[i + 1] <= a[i + 2] or a[i + 2] <= a[i + 1] <= a[i]:
        m.append(0)
    elif a[i] <= a[i + 2] <= a[i + 1] or a[i + 1] <= a[i + 2] <= a[i]:
        m.append(1)
    elif a[i + 1] <= a[i] <= a[i + 2] or a[i + 2] <= a[i] <= a[i + 1]:
        m.append(-1)

s = sum(abs(a[i] - a[i + 1]) for i in range(n + 1))

for i, m0 in enumerate(m):
    if m0 == 0:
        print(s)
    elif m0 == 1:
        print(s - 2 * abs(a[i + 1] - a[i + 2]))
    elif m0 == -1:
        print(s - 2 * abs(a[i] - a[i + 1]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 1]:
        m.append(-1)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i, m0 in enumerate(m):
    if m0 == 0:
        print(s)
    elif m0 > 0:
        print(s - 2 * abs(a[i + 1] - a[i + 2]))
    elif m0 < 0:
        print(s - 2 * abs(a[i + 2] - a[i + 3]))
print(s - abs(a[n - 1] - a[n]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 1]:
        m.append(-1)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i in m:
    if i == 0:
        print(s)
    elif i > 0:
        print(s - 2 * abs(a[i - 1] - a[i]))
    elif i < 0:
        print(s - 2 * abs(a[i] - a[i + 1]))
print(s - abs(a[n - 1] - a[n]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 3]:
        m.append(-1)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i in m:
    if i == 0:
        print(s)
    elif i > 0:
        print(s - 2 * abs(a[i - 1] - a[i]))
    elif i < 0:
        print(s - 2 * abs(a[i] - a[i + 1]))
print(s - abs(a[n - 1] - a[n]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 3]:
        m.append(3)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i in m:
    if i == 0:
        print(s)
    elif i > 0:
        print(s - 2 * abs(a[i - 1] - a[i]))
    elif i < 0:
        print(s - 2 * abs(a[i] - a[i + 1]))
print(s - abs(a[n - 1] - a[n]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = map(int, input().split())
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 3]:
        m.append(3)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i in m:
    if i == 0:
        print(s)
    elif i > 0:
        print(s - 2 * abs(a[i - 1] - a[i]))
    elif i < 0:
        print(s - 2 * abs(a[i] - a[i + 1]))
print(s - abs(a[n - 1] - a[n]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = map(int, input().split())
m = []
for i in range(n - 2):
    if a[i] < a[i + 1] < a[i + 2] or a[i + 2] < a[i + 1] < a[i]:
        m.append(0)
    if a[i + 1] < a[i] < a[i + 2] or a[i + 1] < a[i + 2] < a[i]:
        m.append(1)
    if a[i] < a[i + 2] < a[i + 1] or a[i + 2] < a[i] < a[i + 3]:
        m.append(3)

s = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))

print(s - abs(a[0] - a[1]))
for i in m:
    if i == 0:
        print(s)
    elif i > 0:
        print(s - 2 * abs(a[i - 1] - a[i]))
    elif i > 0:
        print(s - 2 * abs(a[i] - a[i + 1]))
print(s - abs(a[n - 1] - a[n]))