Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

"""
参考：https://img.atcoder.jp/abc070/editorial.pdf
・公式解が再帰だったのでやってみる
"""

import sys

# 再帰呼び出しの回数制限(デフォルト1000)
sys.setrecursionlimit(10 ** 9)

from heapq import heappop, heappush

N = int(input())
nodes = [[] for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    # nodes[出発ノード](到着ノード, コスト))
    nodes[a].append((b, c))
    # 無向グラフなのでこっちも入れる
    nodes[b].append((a, c))

memo = [0] * (N+1)
def dfs(cur, prev, dist):
    memo[cur] = dist
    for nxt, cost in nodes[cur]:
        if nxt == prev:
            continue
        dfs(nxt, cur, dist+cost)

Q, K = map(int, input().split())
dfs(K, -1, 0)

for i in range(Q):
    x, y = map(int, input().split())
    # 今回は全てKを経由するので、Kからの距離さえ分かれば全部出る
    print(memo[x] + memo[y])


Fixed submission:
# -*- coding: utf-8 -*-

"""
・ダイクストラ(優先度付きキュー)
"""

from heapq import heappop, heappush

# 今回はダイクストラするので、データの持ち方は隣接リスト
N = int(input())
nodes = [[] for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    # nodes[出発ノード](到着ノード, コスト))
    nodes[a].append((b, c))
    nodes[b].append((a, c))

def dijkstra(nodes, start):
    # 最短距離メモ
    memo = [float('inf')] * (len(nodes))
    # 訪問済メモ
    visited = [False] * (len(nodes))
    # スタート位置
    que = [(0, start)]
    memo[start] = 0
    # キューが空になるまで
    while len(que) != 0:
        # startからの距離, 現在のノード
        dist, cur = heappop(que)
        # 訪問済にする
        visited[cur] = True
        # 出発ノードcurの到着ノードでループ
        for i in range(len(nodes[cur])):
            nxt, cost = nodes[cur][i]
            # 訪問済ではない時だけ
            if not visited[nxt]:
                memo[nxt] = min(memo[cur] + cost, memo[nxt])
                # 現在の移動距離をキューの優先度として、早い方から先に処理するようにする
                heappush(que, (memo[nxt], nxt))
    # ノードstartからの最短距離リストを返却
    return memo

Q, K = map(int, input().split())
# 引数：(ノードの隣接リスト, 出発ノード)
memo = dijkstra(nodes, K)

for i in range(Q):
    x, y = map(int, input().split())
    print(memo[x] + memo[y])
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

# 母体の削減を試みてみる
abM2 = []
for i in range(M):
    if abM[i][0] == 1 or abM[i][1] == N:
        abM2.append(abM[i])

transit = []
for i in range(len(abM2)):
    if abM2[i][0] == 1:
        transit.append(abM2[i][1])

for i in range(len(abM2)):
    if abM2[i][1] == N:
        if abM2[i][0] in transit:
            print("POSSIBLE")
            exit()
        
print("IMPOSSIBLE")

Fixed submission:
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

transit = []
transit2 = []
for i in range(M):
    if abM[i][0] == 1:
        transit.append(abM[i][1])
    if abM[i][1] == N:
        transit2.append(abM[i][0])

# setで共通要素の集合を取ってみる
if len(set(transit) & set(transit2)) != 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

transit = []
for i in range(M):
    if abM[i][0] == 1:
        transit.append(abM[i][1])

for i in range(M):
    if abM[i][1] == N:
        if abM[i][0] in transit:
            print("POSSIBLE")
            exit()
        
print("IMPOSSIBLE")

Fixed submission:
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

transit = []
transit2 = []
for i in range(M):
    if abM[i][0] == 1:
        transit.append(abM[i][1])
    if abM[i][1] == N:
        transit2.append(abM[i][0])

# setで共通要素の集合を取ってみる
if len(set(transit) & set(transit2)) != 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

transit = []
for i in range(M):
    if abM[i][0] == 1:
        transit.append(abM[i][1])

for i in range(M):
    if abM[i][0] in transit and abM[i][1] == N:
        print("POSSIBLE")
        exit()
        
print("IMPOSSIBLE")

Fixed submission:
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
abM = [list(map(int, input().split())) for i in range(M)]

transit = []
transit2 = []
for i in range(M):
    if abM[i][0] == 1:
        transit.append(abM[i][1])
    if abM[i][1] == N:
        transit2.append(abM[i][0])

# setで共通要素の集合を取ってみる
if len(set(transit) & set(transit2)) != 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = [None] * N
for i in range(N):
    A[i] = [a-1 for a in LIST()]

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            a = A[i][j]
            if valid[a]:
                # まだ有効なもののうち、一番順位が高いスポーツにカウント追加
                cnt[a] += 1
                break
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        # 票が最も集まったものは次から選ばないようにする
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = list2d(N, M, 0)
for i in range(N):
    for j, a in enumerate(LIST()):
        # データの持ち方を変える
        A[i][a-1] = j

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            if valid[j] and A[i][j] < mn:
                mn = A[i][j]
                idx = j
        # 一番順位が高いスポーツにカウント追加
        cnt[idx] += 1
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        # 票が最も集まったものは次から選ばないようにする
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

bN = []
if N % 2 == 0:
    for i in range(N):
        if i % 2 == 0:
            bN.append(str(aN[i]))
        else:
            bN.insert(0, str(aN[i]))
else:
    for i in range(N):
        if i % 2 == 1:
            bN.append(str(aN[i]))
        else:
            bN.insert(0, str(aN[i]))

print(" ".join(bN))

Fixed submission:
# -*- coding: utf-8 -*-

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

bN = [0] * N
if N % 2 == 0:
    for i in range(N):
        if i % 2 == 0:
            bN[(N//2) + i//2] = str(aN[i])
        else:
            bN[(N//2) - (i+1)//2] = str(aN[i])
else:
    for i in range(N):
        if i % 2 == 0:
            bN[(N//2) - i//2] = str(aN[i])
        else:
            bN[(N//2) + (i+1)//2] = str(aN[i])

print(" ".join(bN))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from bisect import insort, bisect_left

N = int(input())
aN = list(map(int, input().split()))
mids = aN[N:N*2]
L = sorted(aN[:N])
R = sorted(aN[N:])

ans = sum(L) - sum(R[:N])
for i in range(N):
    insort(L, mids[i])
    R.pop(bisect_left(R, mids[i]))
    ans = max(sum(L[-N:]) - sum(R[:N]), ans)
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

"""
参考：https://img.atcoder.jp/arc074/editorial.pdf
・優先度付きキュー
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from heapq import heappush, heappushpop

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

mids = aN[N:N*2]
L = []
R = []
for i in range(N):
    # とりあえず前後半確定の部分をキューに詰める
    heappush(L, aN[i])
    heappush(R, -aN[i+N*2])

# 未確定の真ん中1/3について調べていく
l_sum = [0] * (N+1)
l_sum[0] = sum(aN[:N])
for i in range(N):
    # ある値をpushして最小値をpopする
    num = heappushpop(L, mids[i])
    # それに合わせて合計値も更新
    l_sum[i+1] = l_sum[i] + mids[i] - num

# 後半部分は逆方向からやる
r_sum = [0] * (N+1)
r_sum[N] = sum(aN[N*2:])
for i in range(N-1, -1, -1):
    # 正負逆転させれば最大値にも使える
    num = -(heappushpop(R, -mids[i]))
    r_sum[i] = r_sum[i+1] + mids[i] - num

ans = -float('inf')
for i in range(N+1):
    ans = max(l_sum[i] - r_sum[i], ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from bisect import insort, bisect_left

N = int(input())
aN = list(map(int, input().split()))
mids = aN[N:N*2]
L = sorted(aN[:N])
R = sorted(aN[N:])

ans = sum(L) - sum(R[:N])
for i in range(N):
    insort(L, mids[i])
    R.pop(bisect_left(R, mids[i]))
    ans = max(sum(L[-N:]) - sum(R[:N]), ans)
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

"""
参考：https://img.atcoder.jp/arc074/editorial.pdf
・優先度付きキュー
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from heapq import heappush, heappushpop

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

mids = aN[N:N*2]
L = []
R = []
for i in range(N):
    # とりあえず前後半確定の部分をキューに詰める
    heappush(L, aN[i])
    heappush(R, -aN[i+N*2])

# 未確定の真ん中1/3について調べていく
l_sum = [0] * (N+1)
l_sum[0] = sum(aN[:N])
for i in range(N):
    # ある値をpushして最小値をpopする
    num = heappushpop(L, mids[i])
    # それに合わせて合計値も更新
    l_sum[i+1] = l_sum[i] + mids[i] - num

# 後半部分は逆方向からやる
r_sum = [0] * (N+1)
r_sum[N] = sum(aN[N*2:])
for i in range(N-1, -1, -1):
    # 正負逆転させれば最大値にも使える
    num = -(heappushpop(R, -mids[i]))
    r_sum[i] = r_sum[i+1] + mids[i] - num

ans = -float('inf')
for i in range(N+1):
    ans = max(l_sum[i] - r_sum[i], ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bellman_ford(N: int, edges: list, src: int) -> list:
    """ ベルマンフォード(頂点数, 辺集合(0-indexed), 始点) """

    # 頂点[ある始点からの最短距離] (経路自体を知りたい時はここに前の頂点も持たせる)
    res = [INF] * N
    res[src] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for _ in range(N-1):
        for src, dest, cost in edges:
            if res[dest] > res[src] + cost:
                res[dest] = res[src] + cost
    ans = res[N-1]
    # 負の閉路(いくらでもコストを減らせてしまう場所)がないかチェックする
    for _ in range(N-1):
        for src, dest, cost in edges:
            if res[dest] > res[src] + cost:
                res[dest] = res[src] + cost
    if ans != res[N-1]
        # あったら空リストを返却
        return []
    # 問題なければ頂点リストを返却
    return res

N, M = MAP()
edges = []
for i in range(M):
    a, b, c = MAP()
    a -= 1; b -= 1
    edges.append((a, b, -c))

res = bellman_ford(N, edges, 0)

if res:
    print(-res[N-1])
else:
    print('inf')


Fixed submission:
# -*- coding: utf-8 -*-

"""
・ベルマンフォード
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

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

edges = [None] * M
for i in range(M):
    a, b, c = map(int, input().split())
    # 0-indexed
    # コストの正負を逆転させれば最短経路問題として処理できる
    edges[i] = (a-1, b-1, -c)

def bellman_ford(N: int, edges: list, start: int) -> list:
    # 頂点[ある始点からの最短距離]
    # (経路自体を知りたい時はここに前の頂点も持たせる)
    verts = [float('inf')] * N
    verts[start] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N-1):
        for src, dest, cost in edges:
            verts[dest] = min(verts[src] + cost, verts[dest])
    # 無限に減らせる場所がないか確認
    for i in range(N):
        for src, dest, cost in edges:
            if verts[src] + cost < verts[dest]:
                verts[dest] = verts[src] + cost
                if dest == N-1:
                    # 頂点Nがそれに該当していたら空リストを返却
                    return []
    # 問題なければ頂点リストを返却
    return verts

verts = bellman_ford(N, edges, 0)
if not verts:
    print('inf')
else:
    # 正負を戻して出力
    print(-verts[N-1])
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

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

sum1 = aN[0]
cnt = 0
if aN[0] > 0:
    sei = True
elif aN[0] < 0:
    sei = False
else:
    print("和が0なので、これはないはず")

for i in range(1,N):
    sum1 += aN[i]
    if sei:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt += tmp
            sum1 -= tmp
        sei = False
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt += tmp
            sum1 += tmp
        sei = True
print(cnt)

Fixed submission:
# -*- coding: utf-8 -*-

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

sum1 = 0
cnt1 = 0
for i in range(N):
    sum1 += aN[i]
    if i % 2 == 0:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt1 += tmp
            sum1 -= tmp
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt1 += tmp
            sum1 += tmp

sum1 = 0
cnt2 = 0
for i in range(N):
    sum1 += aN[i]
    if i % 2 == 1:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt2 += tmp
            sum1 -= tmp
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt2 += tmp
            sum1 += tmp

# 開始を偶奇それぞれでやって、良かった方を出力
print(min(cnt1, cnt2))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N = int(input())

ans = 1
for i in range(N):
    ans = ans * (i + 1)

# 大き過ぎる値を調整する
ans = ans % (10 ** 9 + 7) 

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())

ans = 1
for i in range(N):
    # 調整の処理は都度行っても端数の側に影響を与えないので問題ない
    ans = (ans * (i + 1)) % (10 ** 9 + 7) 

# 大き過ぎる値を調整する
# ans = ans % (10 ** 9 + 7) 

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

total = sum(A)
each = N*(N+1) // 2
if total % each != 0:
    NO()
    exit()
cnt = total // each

for i in range(cnt):
    idx = A.index(min(A))
    for j in range(idx, idx+N):
        sub = j - idx + 1
        j %= N
        A[j] -= sub
if all(a == 0 for a in A):
    YES()
else:
    NO()


Fixed submission:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

total = sum(A)
each = N*(N+1) // 2
if total % each != 0:
    NO()
    exit()
# 操作回数
K = total // each

# A各値の差分で数列Bを作る
B = [0] * N
for i in range(N):
    j = (i+1) % N
    B[i] = A[j] - A[i]
# ここからK引けば、1度の操作につきBのいずれかの要素を+N、とできる
B = [b-K for b in B]

if all(b % N == 0 and b <= 0 for b in B):
    YES()
else:
    NO()
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

# 階乗しないで求める版

x = int(input())

# 素数判定用関数
def is_prime_2(num):
    if num < 2:
        return False
    if num == 2 or num == 3 or num == 5:
        return True
    if num % 2 == 0 or num % 3 == 0 or num % 5 == 0:
        return False
    # 疑似素数(2でも3でも5でも割り切れない数字)で次々に割っていく
    prime = 7
    step = 4
    num_sqrt = math.sqrt(num)
    while prime <= num_sqrt:
        if num % prime == 0:
            return False
        prime += step
        step = 6 - step
    return True

# xまでの間にある素数を列挙
sosu_list = []
for i in range(x+1):
    if is_prime_2(i):
        sosu_list.append(i)

# 素数のカウントは階乗前の値から個別に取って合算でも問題ない
sosu_cnt = {}
for i in range(1, x+1):
    for sosu in sosu_list:
        while i % sosu == 0:
            if sosu in sosu_cnt:
                sosu_cnt[sosu] += 1
            else:
                sosu_cnt[sosu] = 1
            i //= sosu

# 素数カウントを使って、約数の個数を求める
ans = 1
for key, value in sosu_cnt.items():
    sosu_cnt[key] += 1
    ans = (ans * sosu_cnt[key]) % (10 ** 9 + 7)
print(ans)

Fixed submission:
# -*- coding: utf-8 -*-

import math

x = int(input())

# 階乗
a = math.factorial(x)

# 素数判定用関数
def is_prime_2(num):
    if num < 2:
        return False
    if num == 2 or num == 3 or num == 5:
        return True
    if num % 2 == 0 or num % 3 == 0 or num % 5 == 0:
        return False
    # 疑似素数(2でも3でも5でも割り切れない数字)で次々に割っていく
    prime = 7
    step = 4
    num_sqrt = math.sqrt(num)
    while prime <= num_sqrt:
        if num % prime == 0:
            return False
        prime += step
        step = 6 - step
    return True

# xまでの間にある素数を列挙
sosu_list = []
for i in range(x+1):
    if is_prime_2(i):
        sosu_list.append(i)

# 素因数分解していく中で、使った素数のカウントを取る
sosu_cnt = {}
for sosu in sosu_list:
    while a % sosu == 0:
        if sosu in sosu_cnt:
            sosu_cnt[sosu] += 1
        else:
            sosu_cnt[sosu] = 1
        a //= sosu

# 素数カウントを使って、約数の個数を求める
ans = 1
for key, value in sosu_cnt.items():
    sosu_cnt[key] += 1
    ans = (ans * sosu_cnt[key]) % (10 ** 9 + 7)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

K, S = map(int, input().split())

# 絶対TLE…
ans = 0
for x in range(K+1):
    for y in range(K+1):
        for z in range(K+1):
            if x + y + z == S:
                ans += 1
print(ans)

Fixed submission:
# -*- coding: utf-8 -*-

K, S = map(int, input().split())

ans = 0
for x in range(K+1):
    for y in range(K+1):
        # zは全部回さないで、Sを満たすようなものだけ用意する
        z = S - x - y
        # 候補となるzがKの範囲内であれば有効
        if 0 <= z <= K:
            ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

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

BN = [0] * (N//2+1)

# 偶奇で分けて集計
for i in range(N):
    if N % 2 != 0:
        BN[N//2 - AN[i]//2] += 1
    else:
        BN[N//2 - (AN[i]-1)//2] += 1

# 偶数の場合は真ん中を考慮
ans = 0
for i in range(len(BN)):
    if BN[i] > 2 or N % 2 != 0 and BN[-1] > 1:
        print(0)
        exit()

# 結果出力
print(2 ** (N//2))

Fixed submission:
# -*- coding: utf-8 -*-

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

BN = [0] * (N//2+1)

# 偶奇で分けて集計
for i in range(N):
    if N % 2 != 0:
        BN[N//2 - AN[i]//2] += 1
    else:
        BN[N//2 - (AN[i]//2+1)] += 1

# 偶数の場合は真ん中を考慮
ans = 0
for i in range(len(BN)):
    if BN[i] > 2 or N % 2 != 0 and BN[-1] > 1:
        print(0)
        exit()

# 結果出力
print(2 ** (N//2) % (10 ** 9 +7))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
cura, curb = MAP()
for i in range(N-1):
    a, b = MAP()
    tmpa, tmpb = a, b
    # 比をそのままで、現在の票数より大きくする
    while a < cura or b < curb:
        a += tmpa
        b += tmpb
    cura, curb = a, b
ans = cura + curb
print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())

T,A = map(int, input().split())

for i in range(N-1):
    t,a = map(int, input().split())
    tmp1,tmp2 = 1,1
    # ループで足していくより、1回で出せる方法にする
    if t < T:
        # 切り上げはmath.ceilより演算子のみのが速い
        tmp1 = -(-T // t)
    if a < A:
        tmp2 = -(-A // a)
    # 多く増やす必要のある方に合わせて、同じ比で両方増やす
    T = t * max(tmp1,tmp2)
    A = a * max(tmp1,tmp2)

print(T + A)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N = int(input())

T,A = map(int, input().split())

for i in range(N-1):
    t,a = map(int, input().split())
    tmp1,tmp2 = t,a
    # 比はそのままで、あり得る投票数まで値を増やす
    while t < T or a < A:
        t += tmp1
        a += tmp2
    T = t
    A = a

print(T + A)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())

T,A = map(int, input().split())

for i in range(N-1):
    t,a = map(int, input().split())
    tmp1,tmp2 = 1,1
    # ループで足していくより、1回で出せる方法にする
    if t < T:
        # 切り上げはmath.ceilより演算子のみのが速い
        tmp1 = -(-T // t)
    if a < A:
        tmp2 = -(-A // a)
    # 多く増やす必要のある方に合わせて、同じ比で両方増やす
    T = t * max(tmp1,tmp2)
    A = a * max(tmp1,tmp2)

print(T + A)