Problem p03373 (python)

Fixed submission:
input_str = input()

A, B, C, X, Y = input_str.split(" ")

A = int(A)
B = int(B)
C = int(C)
X = int(X)
Y = int(Y)

answer = 0
ave_AB = (A + B) / 2
if ave_AB <= C:
    answer = X*A + Y*B
else:
    xy_min = min(X,Y)
    answer = xy_min * C * 2
    tmp = X - Y
    if tmp > 0:
        if A < C*2:
            answer += tmp*A
        else:
            answer += tmp*C*2
    elif tmp < 0:
        if B < C*2:
            answer += -tmp*B
        else:
            answer += -tmp*C*2
print(answer)

Buggy submission (verdict: Runtime Error):
# input
N, C = map(int, input().split(" "))
x, v = [], []
for i in range(1, N+1):
    xv = list(map(int, input().split(" ")))
    x.append(xv[0])
    v.append(xv[1])

# x_i メートル
# v_i キロカロリー
max_cal = 0
S = [[i for i in range(N)]]  # stack
place = 0  # 位置
cal = 0
S[0].append(cal)
S[0].append(place)
while len(S) > 0:
    s = S.pop()  # n = len
    # index -1 : place
    # index -2 : calorie
    if len(s) == 3:
        # len(s) = 1 になったとき
        idx = s[0]  # first
        next_place = x[idx]
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[s[0]]
        max_cal = max(max_cal, next_cal)
    else:
        #while len(s) > 1:
        # x[s[0]] は s[0] 番目の寿司の相手ある絶対位置（初期値からの距離）
        place = s[-1]
        idx = s[0]  # first
        next_place = x[idx]
        dist = abs(next_place - place)
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[1:-2] + [next_cal, next_place])
        place = s[-1]
        idx = s[-3]  # last
        next_place = x[idx]
        #place_step = abs(next_place - place) % C
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[:-3] + [next_cal, next_place])
print(max_cal)

Buggy submission (verdict: Runtime Error):
# input
N, C = map(int, input().split(" "))
x, v = [], []
for i in range(1, N+1):
    xv = list(map(int, input().split(" ")))
    x.append(xv[0])
    v.append(xv[1])

# x_i メートル
# v_i キロカロリー
max_cal = 0
S = [[i for i in range(N)]]  # stack
place = 0  # 位置
cal = 0
S[0].append(cal)
S[0].append(place)
while len(S) > 0:
    s = S.pop()  # n = len
    # index -1 : place
    # index -2 : calorie
    if len(s) == 3:
        # len(s) = 1 になったとき
        idx = s[0]  # first
        next_place = x[idx]
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[s[0]]
        max_cal = max(max_cal, next_cal)
    else:
        #while len(s) > 1:
        # x[s[0]] は s[0] 番目の寿司の相手ある絶対位置（初期値からの距離）
        place = s[-1]
        idx = s[0]  # first
        next_place = x[idx]
        dist = abs(next_place - place)
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[1:-2] + [next_cal, next_place])
        place = s[-1]
        idx = s[-3]  # last
        next_place = x[idx]
        #place_step = abs(next_place - place) % C
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[:-3] + [next_cal, next_place])
print(max_cal)

Buggy submission (verdict: Runtime Error):
# input
N, C = map(int, input().split(" "))
x, v = [], []
for i in range(1, N+1):
    xv = list(map(int, input().split(" ")))
    x.append(xv[0])
    v.append(xv[1])

# x_i メートル
# v_i キロカロリー
max_cal = 0
S = [[i for i in range(N)]]  # stack
place = 0  # 位置
cal = 0
S[0].append(cal)
S[0].append(place)
while len(S) > 0:
    s = S.pop()  # n = len
    # index -1 : place
    # index -2 : calorie
    print("pop", s, max_cal)
    if len(s) == 3:
        # len(s) = 1 になったとき
        idx = s[0]  # first
        next_place = x[idx]
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[s[0]]
        max_cal = max(max_cal, next_cal)
    else:
        #while len(s) > 1:
        # x[s[0]] は s[0] 番目の寿司の相手ある絶対位置（初期値からの距離）
        place = s[-1]
        idx = s[0]  # first
        next_place = x[idx]
        dist = abs(next_place - place)
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[1:-2] + [next_cal, next_place])
        place = s[-1]
        idx = s[-3]  # last
        next_place = x[idx]
        #place_step = abs(next_place - place) % C
        place_step = min(dist, C - dist)
        next_cal = s[-2] - place_step + v[idx]
        max_cal = max(max_cal, next_cal)
        S.append(s[:-3] + [next_cal, next_place])
print(max_cal)