Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03142 (python)

Fixed submission:
import sys
# from heapq import heappop, heappush
from collections import deque
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [0] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] += 1

n = check_lst.index(0)
ans = [0] * N
que = deque()
que.append(n)
while que:
    v = que.popleft()
    for v2 in lst[v]:
        check_lst[v2] -= 1
        if check_lst[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1
print (*ans, sep = '\n')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [False] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] = True
# print (lst)

ans_lst = [[-1, 0] for _ in range(N)] #[親, 距離]を要素とするリスト
#根を求める = どこからも辺が伸びていないところ --> check_lst = Falseの場所
n = check_lst.index(False)
ans_lst[n][0] = -1
# print (n)

def dfs(i): #iからの経路を探す
    distance = ans_lst[i][1]
    for j in lst[i]:
        # print (j)
        if distance + 1 > ans_lst[j][1]:#距離が最長となるとき
            ans_lst[j][0] = i
            ans_lst[j][1] = distance + 1
            dfs(j)
    return 

dfs(n)
# print (ans_lst)

# print ('以下答え')
for i, d in ans_lst:
    print (i + 1)
Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 9)

N, M = map(int, input().split())

G = [[] for _ in range(N + 1)]

for _ in range(M):
    x, y = map(int, input().split())
    G[x].append(y)

lst = [-1] * (N + 1)


def calc(x):
    if lst[x] != -1:
        return lst[x]
    tmp = 0
    for next_ in G[x]:
        tmp = max(tmp, calc(next_))
    
    lst[x] = tmp + 1
    return tmp + 1

for i in range(1, N + 1):
    if lst[i] == -1:
        calc(i)

print (max(lst) - 1)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

G = [[] for _ in range(N + 1)]

for _ in range(M):
    x, y = map(int, input().split())
    G[x].append(y)

lst = [-1] * (N + 1)


def calc(x):
    if lst[x] != -1:
        return lst[x]
    tmp = 0
    for next_ in G[x]:
        tmp = max(tmp, calc(next_))
    
    lst[x] = tmp + 1
    return tmp + 1

for i in range(1, N + 1):
    if lst[i] == -1:
        calc(i)

print (max(lst) - 1)
Problem p03181 (python)

Fixed submission:
import sys
input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)

N, MOD = map(int, input().split())


G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    G[x].append(y)
    G[y].append(x)
G[0].append(1)
G[1].append(0)

DP1 = [0] * (N + 1)

parent = [-1] * (N + 1)
parent[1] = 0

order = []

stack = [1]
while stack:
    x = stack.pop()
    order.append(x)
    for next_ in G[x]:
        if next_ == parent[x]:
            continue
        parent[next_] = x
        stack.append(next_)

for v in order[::-1]:
    tmp = 1
    for next_ in G[v]:
        tmp *= DP1[next_] + 1
        tmp %= MOD
    DP1[v] = tmp
# print (DP1)

DP2 = [1] * (N + 1)

for x in order:
    # 累積積のパート
    #ここが同じ値を返すかチェックする
    n = len(G[x])
    lst1 = [1] * (n + 1)
    lst2 = [1] * (n + 1)
    for i in range(n):
        #親のときだけは違うルールで対処
        if G[x][i] == parent[x]:
            lst1[i + 1] = lst1[i] * DP2[x] % MOD
        else:
            lst1[i + 1] = lst1[i] * (DP1[G[x][i]] + 1) % MOD

        if G[x][n - 1 - i] == parent[x]:
            lst2[n - i - 1] = lst2[n - i] * DP2[x] % MOD
        else:
            lst2[n - i - 1] = lst2[n - i] * (DP1[G[x][n - 1 - i]] + 1) % MOD
    
    for i in range(n):
        if G[x][i] == parent[x]:
            continue
        tmp = lst1[i] * lst2[i + 1] % MOD
        tmp += 1 #白色の分
        DP2[G[x][i]] = tmp % MOD
        stack.append(G[x][i])

# print (DP2)

for i in range(1, N + 1):
    print ((DP1[i] * DP2[i]) % MOD)

# print (DP1)
# print (DP2)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, MOD = map(int, input().split())

#計算量 log(m)くらい
def modinv(a, m): #mod. m でのaの逆元 a^{-1}を計算する
    b = m
    u = 1
    v = 0
    while b:
        t = a//b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    
    u %= m
    if u < 0:
        u += m
    return u

G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    G[x].append(y)
    G[y].append(x)

DP1 = [-1] * (N + 1)
DP1[0] = 1
parent = [-1] * (N + 1)
parent[1] = 0

def dfs(x, p):
    tmp = 1
    for next_ in G[x]:
        if next_ == p:
            continue
        parent[next_] = x
        tmp *= (dfs(next_, x) + 1)
        # tmp %= MOD
    DP1[x] = tmp
    return tmp

dfs(1, 0)
# print (DP1)

DP2 = [-1] * (N + 1)
DP2[0] = 1
DP2[1] = 1
stack = [1]

while stack:
    x = stack.pop()
    for next_ in G[x]:
        if DP2[next_] != -1:
            continue
        tmp = DP1[x]
        tmp //= (DP1[next_] + 1)
        tmp *= DP2[parent[next_]]
        tmp += 1 #白色の分
        DP2[next_] = tmp
        stack.append(next_)

# print (DP2)

for i in range(1, N + 1):
    print (DP1[i] * DP2[i] % MOD)
Problem p03206 (python)

Fixed submission:
D = int(input())
print ('Christmas', end = '')
for i in range(25 - D):
    print (' Eve', end = '')
print ()

Buggy submission (verdict: Runtime Error):
N, X = map(int, input().split())

a = [1]
p = [1]
for i in range(1, N+1):
    a.append(2 * a[i-1] + 3)
    p.append(2 * a[i-1] + 1)

def calc(N, X): #レベルNバーガーの下からX層に含まれるパティの枚数
    if N == 0: #全てパティ
        if X == 0:
            return 0
        else:
            return 1
    if X == 1:
        return 0
    # print (N - 1)
    if 1 <= X <= a[N - 1]:
        # print ('A')
        return calc(N - 1, X - 1)
    if X == 2 + a[N - 1]:
        # print ('B')
        return p[N - 1] + 1
    if 2 +a[N - 1] < X <= 2 + 2 * a[N - 1]:
        # print ('C')
        return p[N - 1] + 1 + calc(N - 1, X - 2 - a[N - 1])
    if X == 3 + 2 * 2 * a[N - 1]:
        # print ('D')
        return 2 * p[N - 1] + 1

print (calc(N, X))
Problem p03251 (python)

Fixed submission:
import sys
N, M, X, Y = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

z1 = max(x) + 1
z2 = min(y)

if X < z1 and z1 < Y:
    if X < z2 and z2 < Y:
        if z1 <= z2:
            print ('No War')
            sys.exit()
print ('War')

Buggy submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

z1 = max(x) + 1
z2 = min(y)

if X < z1 and z1 < Y:
    if X < z2 and z2 < Y:
        if z1 <= z2:
            print ('No War')
            sys.exit()
print ('War')
Problem p03268 (python)

Fixed submission:
N, K = map(int, input().split())

if K%2 == 0: #Kが偶数の時
    a = N//K
    b = (N+K//2)//K
    ans = a ** 3 + b ** 3
else: #Kが奇数の時3
    a = N//K
    ans = a ** 3

print (ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

if K % 2 == 1:
    tmp = N // K
    ans = tmp ** 3
    print (ans)0

else:
    tmp = N // K
    ans = tmp ** 3

    tmp = N // (K // 2) - tmp
    ans += tmp ** 3

    print (ans)
Problem p03274 (python)

Fixed submission:
import sys

N, K = map(int, input().split())
lst = list(map(int, input().split()))

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
#print (lastx, lasty)

def check(k):
    ans = 10 ** 9
    for j in range(0,k+1): #j: 正側の使う個数
#        print ('j, k-j', j, k-j)
        if j > lastx or k-j > lasty:
            pass
        else:
            if j == 0:
                p = 0
            else:
                p = lst_p[j-1]
            if k-j == 0:
                m = 0
            else:
                m = lst_m[lasty-(k-j)]
            ans = min(ans, 2 * p + abs(m), p + 2 * abs(m))
    return ans

print (check(K))

Buggy submission (verdict: Runtime Error):
import sys

N, K = map(int, input().split())
lst = list(map(int, input().split()))

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 9
all_m = 10 ** 9
if K <= lastx:
    all_p = lst_p[K-1]
if K <= lasty:
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, all_m))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1
Problem p03280 (python)

Fixed submission:
A, B = map(int, input().split())
print ((A-1)*(B-1))

Buggy submission (verdict: Runtime Error):
A, B = map(int, input().split())
print ((A-1)*(B-1)
Problem p03290 (python)

Fixed submission:
#人のプログラムの全コピ　TKR18さん

#入力
a,b=map(int,input().split())
c=[0]+[list(map(int,input().split()))for i in range(a)]

#print(c)

def f(t,s): #t:t*100点までリストが存在する s: 目標値
    if t==0: #が0の時、とりあえず大きな数を返す
        return 10**9
    l=min(s//(t*100),c[t][0]) #s//(t*100): 目標を最高基本点で割った時の商、c[t][0]:最高基本点の使える数　これらの小さい方を返す
    k=100*t*l #最高基本点を使って得点できる最高点
    if l==c[t][0]: #最高基本点を使った数が、使える数と同じならボーナス点を加える。
        k+=c[t][1] 
    if k<s: #最高基本点を全て使って得点できる点が、目標点以下なら...
        l+=f(t-1,s-k) #最高気本点を除いたもので、残りの点数を計算する。
    return min(l,f(t-1,s)) #最高気本点を使うもの(l)と最高基本点を使わないもの(f(t-1,s)のうち最小のものを見つける
print(f(a,b))

Buggy submission (verdict: Runtime Error):
#人のプログラムの全コピ　TKR18さん

#入力
a,b=map(int,input().split())
c=[0]+[list(map(int,input().split()))for i in range(a)]

#print(c)

def f(t,s): #t:t*100点までリストが存在する s: 目標値
    if t==0: #が0の時、とりあえず大きな数を返す
        return 10**9
    l=min(s//(t*100),c[t][0]) #s//(t*100): 目標を最高基本点で割った時の商、c[t][0]:最高基本点の使える数　これらの小さい方を返す
    k=100*t*l #最高基本点を使って得点できる最高点
    if l==c[t][0]: #最高基本点を使った数が、使える数と同じならボーナス点を加える。
        k+=c[t][1] 
    if k<s:　#最高基本点を全て使って得点できる点が、目標点以下なら...
        l+=f(t-1,s-k) #最高気本点を除いたもので、残りの点数を計算する。
    return min(l,f(t-1,s)) #最高気本点を使うもの(l)と最高基本点を使わないもの(f(t-1,s)のうち最小のものを見つける
print(f(a,b))
Problem p03310 (python)

Fixed submission:
#PypyでもTLE A[:half]のような箇所が遅い？

N = int(input())
A = list(map(int, input().split()))
for i in range(1, N):
    A[i] += A[i - 1]

def calc(lb, ub):
    max_num = A[ub]
    lb -= 1
    if lb == -1:
        min_num = 0
    else:
        min_num = A[lb]
    total = A[ub] - min_num
    # print (total)
    while (ub - lb) > 1:
        mid = (lb + ub)//2
        if A[mid] - min_num > total/2:
            ub = mid
        else:
            lb = mid
    # print ('ub', ub)
    a1, b1 = A[ub] - min_num, max_num - A[ub]
    diff1 = abs(b1 - a1)
    # print ('diff1', diff1)
    a2, b2 = A[ub - 1] - min_num, max_num - A[ub - 1]
    diff2 = abs(b2 - a2)
    # print ('diff2', diff2)
    if diff2 < diff1:
        a1, b1 = a2, b2
    return a1, b1

# print (calc(A, 0))
# half = 2
# P, Q = calc(0, half - 1)
# print (P, Q)

ans = 10 ** 9
for half in range(2, N - 1):
    # print (A[:half], end = ' ')
    # print (A[half:])
    P, Q = calc(0, half - 1)
    R, S = calc(half, N - 1)
    # print (P, Q, R, S)
    tmp = max(P, Q, R, S) - min(P, Q, R, S)
    # print (tmp)
    ans = min(tmp, ans)

# print ('ans', ans)
print (ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [0] + list(map(int, input().split()))
for i in range(1, N + 1):
    A[i] += A[i - 1]


# print (A)

ans = 10 ** 9

#基準を求める
stan = A[N]//4

for i in range(2 ** 3):
    PQRS = [0, 0, 0, 0]
    index = 1
    prev_index = 0
    for j in range(3):
        if (i >> j) & 1: #基準以上にするとき
            while A[index] - A[prev_index] < stan and index < N:
                index += 1
            PQRS[j] = A[index] - A[prev_index]
            prev_index = index
            # print (1, end = '')
        else: #基準以下にするとき
            while A[index] - A[prev_index] < stan and index < N:
                index += 1
            PQRS[j] = A[index - 1] - A[prev_index]
            prev_index = index - 1
            # print (0, end = '')
    PQRS[3] = A[N] - A[prev_index]
    # print (PQRS, end = ' ')
    # print (max(PQRS) - min(PQRS))
    ans = min((max(PQRS) - min(PQRS)), ans)

stan = (A[N] + 3)//4

for i in range(2 ** 3):
    PQRS = [0, 0, 0, 0]
    index = 1
    prev_index = 0
    for j in range(3):
        if (i >> j) & 1: #基準以上にするとき
            while A[index] - A[prev_index] < stan and index < N:
                index += 1
            PQRS[j] = A[index] - A[prev_index]
            prev_index = index
            # print (1, end = '')
        else: #基準以下にするとき
            while A[index] - A[prev_index] < stan and index < N + 1:
                index += 1
                if index == N + 1:
                    break
            PQRS[j] = A[index - 1] - A[prev_index]
            prev_index = index - 1
            # print (0, end = '')
    PQRS[3] = A[N] - A[prev_index]
    # print (PQRS, end = ' ')
    # print (max(PQRS) - min(PQRS))
    ans = min((max(PQRS) - min(PQRS)), ans)


print (ans)
Problem p03311 (python)

Fixed submission:
#平均値に近づけるという考え方が間違い-->中央値に近づける

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

A = [A[i]-(i+1) for i in range(N)]

A.sort()

center1 = A[N//2]
if N == 1:
    center2 = 10 ** 9
else:
    center2 = A[(N+2)//2]

ans1 = 0
for i in range(N):
    ans1 +=abs(A[i]-center1)
    
ans2 = 0
for j in range(N):
    ans2 += abs(A[j]-center2)
    
print (min(ans1, ans2))

Buggy submission (verdict: Runtime Error):
#平均値に近づけるという考え方が間違い

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

A = [A[i]-(i+1) for i in range(N)]

A.sort()

center1 = A[N//2]
center2 = A[(N+2)//2]

ans1 = 0
for i in range(N):
    ans1 +=abs(A[i]-center1)

ans2 = 0
for j in range(N):
    ans2 += abs(A[j]-center2)
    
print (min(ans1, ans2))
Problem p03393 (python)

Fixed submission:
S = list(input())

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

for i in range(25, -1, -1):
    if i == -1:
        break
    if ord(S[i]) < ord(S[i - 1]):
        continue
    else:
        break
# print (i)
if i == 0:
    print (-1)
    exit()

tmp_n = ord('z')
for j in S[i:]:
    if ord(j) > ord(S[i - 1]):
        tmp_n = min(tmp_n, ord(j))

print (*S[:i - 1], sep = '', end = '')
print (chr(tmp_n))

Buggy submission (verdict: Runtime Error):
S = list(input())

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

for i in range(25, 0, -1):
    if ord(S[i]) < ord(S[i - 1]):
        continue
    else:
        break
# print (i)
if i == 1:
    print (-1)
    print (A)
    exit()

tmp_n = ord('z')
for j in S[i:]:
    if ord(j) > ord(S[i - 1]):
        tmp_n = min(tmp_n, ord(j))

print (*S[:i - 1], sep = '', end = '')
print (chr(tmp_n))

Buggy submission (verdict: Runtime Error):
S = list(input())

#最後かどうかのチェック
final = 'zyxwvutsrqponmlkjihgfedcba'
flag = True
for i in range(len(S)):
    if S[i] == final[i]:
        pass
    else:
        flag = False
        break

if flag and len(S) == 26:
    print (-1)
    exit()

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, a[i], sep = '')
            exit()
Problem p03423 (python)

Fixed submission:
print (int(input())//3)

Buggy submission (verdict: Runtime Error):
print (int(input()//3)
Problem p03501 (python)

Fixed submission:
N, A, B = map(int, input().split())
print (min(N * A, B))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, C = map(int, input().split())

# 入力
lst = [[0] * (10 ** 5 + 10) for _ in range(C + 1)]

for _ in range(N):
    s, t, c = map(int, input().split())
    lst[c][s] += 1
    lst[c][t + 1] -= 1

#番組ごとに毎分必要かどうかを判断する
for i in range(1, C + 1):
    for j in range(1, 10 ** 5 + 10): #いもす法で計算
        lst[i][j] += lst[i][j - 1]
    for j in range(1, 10 ** 5 + 10): #重なっている時 = 連続して使う時
        lst[i][j] = min(1, lst[i][j])

for j in range(1, 10 ** 5 + 10):
    for i in range(1, C + 1):
        lst[i][j] += lst[i - 1][j]

print (max(lst[-1]))
Problem p03550 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5)

N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
a += [W] #最後にYさんの手札を加えておく
dp_T = [-1] * (N + 1) #Xの手番
dp_F = [-1] * (N + 1) #Yの手番

def calc(n, flag): #n: 残りの山の枚数　flag: ターンの管理
    #既に計算済みの場合その値を返す
    if flag == True and dp_T[n] != -1:
        return dp_T[n]
    if flag == False and dp_F[n] != -1:
        return dp_F[n]

    if n == 1: #山の残り枚数が1枚の時 -->引くしかなく得点が決定
        return abs(a[0] - a[n])
    if flag == True: #Xのターン-->大きくしたい
        tmp = 0
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n: #残り枚数0で相手に渡す = 自分が最後の1枚を引く -->相手の手札が決まっているから得点が決定
                tmp = max(tmp, abs(a[0]-a[n]))
            else: #残り枚数が1枚以上ある時、手番は相手に変える
                tmp = max(tmp, calc(n-i, not flag))
        dp_T[n] = tmp
        return tmp
    if flag == False: #Yのターン-->小さくしたい
        tmp = 10 ** 10 #最大値
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n - 1: #残り0枚で相手に渡す
                tmp = min(tmp, abs(a[0] - a[n]))
            else:
                tmp = min(tmp, calc(n-i, not flag))
        dp_F[n] = tmp
        return tmp

print (calc(N, True))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000)

N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
a += [W] #最後にYさんの手札を加えておく
dp_T = [-1] * (N + 1) #Xの手番
dp_F = [-1] * (N + 1) #Yの手番

def calc(n, flag): #n: 残りの山の枚数　flag: ターンの管理
    #既に計算済みの場合その値を返す
    if flag == True and dp_T[n] != -1:
        return dp_T[n]
    if flag == False and dp_F[n] != -1:
        return dp_F[n]

    if n == 1: #山の残り枚数が1枚の時 -->引くしかなく得点が決定
        return abs(a[0] - a[n])
    if flag == True: #Xのターン-->大きくしたい
        tmp = 0
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n: #残り枚数0で相手に渡す = 自分が最後の1枚を引く -->相手の手札が決まっているから得点が決定
                tmp = max(tmp, abs(a[0]-a[n]))
            else: #残り枚数が1枚以上ある時、手番は相手に変える
                tmp = max(tmp, calc(n-i, not flag))
        dp_T[n] = tmp
        return tmp
    if flag == False: #Yのターン-->小さくしたい
        tmp = 10 ** 10 #最大値
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n - 1: #残り0枚で相手に渡す
                tmp = min(tmp, abs(a[0] - a[n]))
            else:
                tmp = min(tmp, calc(n-i, not flag))
        dp_F[n] = tmp
        return tmp

print (calc(N, True))
Problem p03563 (python)

Fixed submission:
R = int(input())
G = int(input())
print (2 * G - R)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N = int(input())
T = [0] + list(map(int, input().split()))
V = [0] + list(map(int, input().split()))

max_speed_from_left = [0] * (N + 1)
max_speed_from_right = [0] * (N + 1)

for i in range(1, N):
    start = max_speed_from_left[i - 1]
    max_speed_from_left[i] = min(start + T[i], V[i], V[i + 1])

for i in range(N - 1, 0, -1):
    start = max_speed_from_right[i + 1]
    max_speed_from_right[i] = min(start + T[i + 1], V[i], V[i + 1])

speed = [min(x, y) for x, y in zip(max_speed_from_left, max_speed_from_right)]

def dist(left_speed, right_speed, t, v):
    x = (v - left_speed) + (v - right_speed)
    if x >= t:
        v = (left_speed + right_speed) / 2
        x = t
    d = (left_speed + v) * (v - left_speed) / 2
    d += v * (t - x)
    d += (right_speed + v) * (v - right_speed) / 2
    return d

ans = 0
for i in range(1, N + 1):
    ans += dist(speed[i - 1], speed[i], T[i], V[i])

print (ans)
Problem p03566 (python)

Fixed submission:
N = int(input())
t = list(map(int, input().split()))
v = list(map(int, input().split()))

# if N == 1: #最初の1区間だけ
#     if t[0] % 2 == 1: #奇数秒
#         if t[0]/2 < v[0]:
#             print ((t[0]/2) ** 2)
#             exit()
        

# print ('t', t)
# print ('v', v)
N_INF = - 10 ** 15
DP = [[N_INF] * (102) for _ in range(100 * 200 + 2)] #DP[T][V] := 時刻Tに速度Dで走っているときの時刻Tまでの走行距離の最大値
DP[0][0] = 0
# print (DP[0][0:5])
time = 1
for i in range(N):
    # print (i)
    # print ('time', time)
    for j in range(time, time + t[i]): #時刻
        # print (j, end = ' ')
        for k in range(0, v[i] + 1): #速度
            if k == 0: #速度0-->加速して速度0になることはない
                # print (j, k)
                # print (max(DP[j - 1][0], DP[j - 1][1] +  1/2))
                DP[j][k] = max(DP[j - 1][0] + 0.25, DP[j - 1][1] + 1/2)
            elif k != v[i]: #加速してくるときも減速してくることもある
                DP[j][k] = max(DP[j - 1][k -1] + k - 1/2, DP[j - 1][k] + k + 0.25, DP[j - 1][k + 1] + k + 1/2)
            else: #(k == v[i] and j != time) #減速してくることはない
                DP[j][k] = max(DP[j - 1][k -1] + k - 1/2, DP[j - 1][k] + k)
        # print (DP[j][0:5])
    time += t[i]
# print (time)
print (DP[time - 1][0])
# print ('T = 6, V = 6', DP[6][6])
# print ('T = 12, V = 2', DP[12][2])
# print ('T = 12, V = 3', DP[12][3])
# print ('T = 13, V = 2', DP[13][2])
# print ('T = 26 V = 2', DP[26][2])
# print ('T = 27 V = 2', DP[27][2])

Buggy submission (verdict: Runtime Error):
N = int(input())
t = [0] + list(map(int, input().split()))
v = [0] + list(map(int, input().split()))
max_speed_from_left = [0] * (N + 1) #index i = t[i] とt[i + 1]の間
max_speed_from_right = [0] * (N + 1) #index i = t[i] とt[i + 1]の間

for i in range(1, N):
    start = max_speed_from_left[i - 1]
    max_speed_from_left[i] = min(start + t[i], v[i], v[i + 1])

for i in range(N - 1, 0, -1):
    start = max_speed_from_right[i + 1]
    max_speed_from_right = min(start + t[i + 1], v[i], v[i + 1])

speed = [min(x, y) for x, y in zip(max_speed_from_left, max_speed_from_right)]

def dist(left_speed, right_speed, t, v):
    x = (v - left_speed) + (v - right_speed)
    if x >= t:
        v = (left_speed + right_speed + t)/2
        x = t
    #最高速度vに到達してt-x余る
    d = (left_speed + v) * (v - left_speed)/2
    d += v * (t - x)
    d += (right_speed + v) * (v - right_speed)/2
    return d

ans = 0
for i in range(1, N + 1):
    ans += dist(speed[i -1], speed[i], t[i], v[i])
print (ans)