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

Fixed submission:
n = int(input())
s = str(input())

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    if r < g: x, y = r, g
    else: x, y = g, r

    ans += len(B)
    if x*2 - y >= 0:
      if s[x*2 - y] == 'B': ans -= 1
    if (x + y) % 2 == 0:
      if s[(x+y)//2] == 'B': ans -= 1
    if y*2 - x < len(s):
      if s[y*2 - x] == 'B': ans -= 1

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = str(input())

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    if r < g: x, y = r, g
    else: x, y = g, r

    ans += len(X3)
    if x*2 - y >= 0:
      if s[x*2 - y] == 'B': ans -= 1
    if (x + y) % 2 == 0:
      if s[(x+y)//2] == 'B': ans -= 1
    if y*2 - x < len(s):
      if s[y*2 - x] == 'B': ans -= 1

print(ans)
Problem p02795 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def keyence20_a():
    H, W, N = map(int, readlines())
    lg = max(H, W)
    ans = (N + lg - 1) // lg
    print(ans)

keyence20_a()

Buggy submission (verdict: Runtime Error):
def keyence20_a():
    H, W, N = map(int, readlines())
    lg = max(H, W)
    ans = (N + lg - 1) // lg
    print(ans)

keyence20_a()
Problem p02845 (python)

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
mod = 10**9 + 7
mem = [0, 0, 0]
ans = 1
for i in range(n):
    ans *= mem.count(A[i])
    ans %= mod
    if ans == 0: break
    idx = mem.index(A[i])
    mem[idx] += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
mod = 10**9 + 7
mem = [0, 0, 0]
ans = 1
for i in range(n):
    ans *= mem.count(A[i])
    ans %= mod
    idx = mem.index(A[i])
    mem[idx] += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
mod = 10**9 + 7
mem = [1, 0, 0]
ans = 3
for i in range(1, n):
    ans *= mem.count(A[i])
    ans %= mod
    mem[mem.index(A[i])] += 1
print(ans)
Problem p02850 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10010010)

def abc146_d():
    n = int(input())
    Q = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(n-1)]

    deg = [0] * n
    adj = [[] for _ in range(n)]
    color = dict()

    for a, b in Q:
        deg[a] += 1
        deg[b] += 1
        adj[a].append(b)
        adj[b].append(a)

    def dfs(s, p):
        nouse = -1
        if p > -1: nouse = color[(min(s, p), max(s, p))]
        c = 1
        for t in adj[s]:
            if t == p: continue
            if c == nouse: c += 1
            color[(min(s, t), max(s, t))] = c
            dfs(t, s)
            c += 1

    dfs(1, -1)

    print(max(deg))
    for a, b in Q:
        print(color[(a, b)])

if __name__ == '__main__':
    abc146_d()

Buggy submission (verdict: Runtime Error):
def abc146_d():
    n = int(input())
    Q = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(n-1)]

    deg = [0] * n
    adj = [[] for _ in range(n)]
    color = dict()

    for a, b in Q:
        deg[a] += 1
        deg[b] += 1
        adj[a].append(b)
        adj[b].append(a)

    def dfs(s, p):
        nouse = -1
        if p > -1: nouse = color[(min(s, p), max(s, p))]
        c = 1
        for t in adj[s]:
            if t == p: continue
            if c == nouse: c += 1
            color[(min(s, t), max(s, t))] = c
            dfs(t, s)
            c += 1

    dfs(1, -1)

    print(max(deg))
    for a, b in Q:
        print(color[(a, b)])

if __name__ == '__main__':
    abc146_d()
Problem p02861 (python)

Fixed submission:
# C - Average Length

N = int(input())
arr = []
for i in range(N):
    tmp = list(map(int, input().split()))
    arr.append(tmp)

####

import math
import statistics
import itertools

table = [[0]*N for i in range(N)]
for i in range(N):
    ti = arr[i]
    for j in range(N):
        tj = arr[j]
        dist = math.sqrt((ti[0] - tj[0])**2 + (ti[1] - tj[1])**2)
        table[i][j] = dist

kumi_all = list(itertools.permutations(range(N)))
kumi_all_count = len(kumi_all)

total_dist = 0

for i in range(kumi_all_count):
    this_dist = 0
    this_kumi = kumi_all[i]

    for j in range(N-1):
        this_dist += table[this_kumi[j]][this_kumi[j+1]]

    total_dist += this_dist

avr_dist = total_dist / kumi_all_count

print(avr_dist)

Buggy submission (verdict: Runtime Error):
# C - Average Length

N = int(input())
arr = []
for i in range(N):
    tmp = list(map(int, input().split()))
    arr.append(tmp)

####

import math
import statistics
import itertools

table = [[0]*N for i in range(N)]
for i in range(N):
    ti = arr[i]
    for j in range(N):
        tj = arr[j]
        dist = math.sqrt((ti[0] - tj[0])**2 + (ti[1] - tj[1])**2)
        #print(i, j, ti, tj, dist)
        table[i][j] = dist

#print(table)

kumi_all = list(itertools.permutations(range(N)))
kumi_all_count = len(kumi_all)

total_dist = 0

for i in range(kumi_all_count):
    this_dist = 0
    this_kumi = kumi_all[i]

    for j in range(N-1):
        this_dist += table[this_kumi[j]][this_kumi[j+1]]

    #print("{}: {}".format(i, this_dist))
    total_dist += this_dist

#print(total_dist, stop_loop)
avr_dist = total_dist / stop_loop

print(avr_dist)
Problem p02866 (python)

Fixed submission:
def nikkei19q_b():
    _ = int(input())
    D = list(map(int, input().split()))
    div = 998244353

    dmax = max(D)
    cntr = [0] * (dmax + 1)
    for d in D: cntr[d] += 1  # カウンター

    if D[0] != 0: return 0  # 最初に入力される頂点1は距離0マスト
    if cntr[0] != 1: return 0  # 距離0が無い、または2つ以上、はおかしい

    ans = 1
    for i in range(1, dmax+1):
        if cntr[i] == 0: return 0  # カウント0だとつながらないのでNG
        ans = ans * (cntr[i-1] ** cntr[i]) % div
    return ans

print(nikkei19q_b())

Buggy submission (verdict: Runtime Error):
def nikkei19q_b():
    _ = int(input())
    D = list(map(int, input().split()))
    div = 998244353

    dmax = max(D)
    cntr = [0] * (dmax + 1)
    for d in D: cntr[d] += 1  # カウンター

    if D[0] != 0: return 0  # 最初に入力される頂点1は距離0マスト
    if cntr[0] != 1: return 0  # 距離0が無い、または2つ以上、はおかしい

    ans = 1
    for i in range(1, dmax+1):
        if cntr[i] == 0: return 0  # カウント0だとつながらないのでNG
        ans = ans * (cntr[i-1][1] ** cntr[i][1]) % div
    print(ans)

nikkei19q_b()
Problem p02900 (python)

Fixed submission:
# D - Disjoint Set of Common Divisors
A, B = map(int, input().split())

from fractions import gcd

# nの素数判定
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

# nの約数列挙
def divisor(n):
    ass = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            ass.append(n//i)
    return ass #sortされていない

ans = 0
gcd1 = gcd(A, B)
for i in divisor(gcd1):
    if is_prime(i):
        ans += 1

print(ans+1)

Buggy submission (verdict: Runtime Error):
# D - Disjoint Set of Common Divisors
A, B = map(int, input().split())

from fractions import gcd

# n以下の素数列挙 O(n log(n))
def primes(n):
    ass = []
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    for i in range(len(is_prime)):
        if is_prime[i]:
            ass.append(i)
    return ass

ans = 0
prms = primes(gcd(A, B))
if len(prms) > 0:
    for pr in prms:
        if A%pr == 0 and B%pr == 0:
            ans += 1

print(ans+1)
# 1は素数に含まれないが今回の問題の回答に含まれるので+1

Buggy submission (verdict: Runtime Error):
# D - Disjoint Set of Common Divisors
A, B = map(int, input().split())

from fractions import gcd

# n以下の素数列挙 O(n log(n))
def primes(n):
    ass = []
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    for i in range(len(is_prime)):
        if is_prime[i]:
            ass.append(i)
    return ass

ans = 0
for pr in primes(gcd(A, B)):
    if A%pr == 0 and B%pr == 0:
        ans += 1

print(ans+1)
# 1は素数に含まれないが今回の問題の回答に含まれるので+1
Problem p02909 (python)

Fixed submission:
# A - Weather Prediction
S = str(input())

weather = ('Sunny', 'Cloudy', 'Rainy')
if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[0])

Buggy submission (verdict: Runtime Error):
# A - Weather Prediction
S = str(input())

weather = ('Sunny', 'Cloudy', 'Rainy')
if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[3])
Problem p02936 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = [tuple(map(int, ln.strip().split())) for ln in readlines()]

    # 隣接リスト作成
    graph = [[] for _ in range(N+1)]
    for a, b in input_data[:N-1]:
        graph[a].append(b)
        graph[b].append(a)

    # クエリをもとにスコア加算　※頂点pだけに加算し、子へは加算しない
    val = [0] * (N+1)
    for p, x in input_data[N-1:]:
        val[p] += x

    # DFSで根元から順に足していく
    stack = [1]
    visited =set([1])
    while stack:
        x = stack.pop()
        for y in graph[x]:
            if y in visited: continue
            stack.append(y)
            visited.add(y)
            val[y] += val[x]

    print(*val[1:], sep=' ')

abc138_d()

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

tree = dict()
parent = [0] * (N+1)
for _ in range(N-1):
  a, b = map(int, input().split())
  parent[b] = a
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

action = [0] * (N+1)
for _ in range(Q):
  p, x = map(int, input().split())
  action[p] += x

counter = [0] * (N+1)

def score(i):
  global counter
  counter[i] = counter[parent[i]] + action[i]
  if i in tree:
    for ch in tree[i]:
      score(ch)

score(1)

ans = ' '.join([str(c) for c in counter[1:]])
print(ans)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
tree = dict()
for _ in range(N-1):
  a, b = map(int, input().split())
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

point = [0] * (N+1)

def act(node, add):
  point[node] = point[node] + add
  if node in tree:
    for ch in tree[node]:
      act(ch, add)

for _ in range(Q):
  p, x = map(int, input().split())
  act(p, x)

ans = ' '.join([str(n) for n in point[1:]])
print(ans)
Problem p02975 (python)

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
cntr = Counter(A)
ans = 'No'
if len(set(A)) == 1 and A[0] == 0:
  ans = 'Yes'
elif n % 3 == 0:
  if len(cntr) == 3:
    k = list(cntr.keys())
    v = list(cntr.values())
    if k[0]^k[1]^k[2] == 0 and v[0] == v[1] and v[1] == v[2]:
      ans = 'Yes'
  elif len(cntr) == 2:
    if sorted(cntr.keys())[0] == 0 and cntr.get(0) == n//3:
      ans = 'Yes'
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
cntr = Counter(A)
ans = 'No'
if len(cntr) == 1 and cntr[0][0] == 0:
  ans = 'Yes'
elif n % 3 == 0:
  if len(cntr) == 3:
    k = list(cntr.keys())
    v = list(cntr.values())
    if k[0]^k[1]^k[2] == 0 and v[0] == v[1] and v[1] == v[2]:
      ans = 'Yes'
  elif len(cntr) == 2:
    if sorted(cntr.keys())[0] == 0 and cntr.get(0) == n//3:
      ans = 'Yes'
print(ans)
Problem p02982 (python)

Fixed submission:
N, D = map(int, input().split())
X = []
for i in range(N):
  X.append(list(map(int, input().split())))
#print(X)

ans = 0
for i in range(N):
  for j in range(i+1, N):
    dist_sq = 0
    for x, y in zip(X[i], X[j]):
      dist_sq += (x - y) ** 2

    dist = dist_sq ** 0.5
    #print(i, j, dist)

    if dist - int(dist) == 0:
      ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, D = map(int, input().split())
X = np.zeros((N, D), np.int8)
for i in range(N):
  X[i,:] = list(map(int, input().split()))

ans = 0
for i in range(N):
  for j in range(i+1, N):
    dist_sq = 0
    for x, y in zip(X[i], X[j]):
      dist_sq += (x - y) ** 2

    dist = dist_sq ** 0.5
    #print(i, j, dist)

    if dist - int(dist) == 0:
      ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

N, D = map(int, input().split())
X = np.zeros((N, D), np.int8)
for i in range(N):
  X[i,:] = list(map(int, input().split()))

#print(X)

ans = 0

for i in range(N):
  for j in range(i+1, N):
    dist = np.linalg.norm(X[i] - X[j])
    #print(i, j, dist)
    if dist - int(dist) == 0:
      ans += 1

print(ans)
Problem p03006 (python)

Fixed submission:
from collections import defaultdict

def diverta192_b():
    N = int(input())
    P = [tuple(map(int, input().split())) for _ in range(N)]
    if len(P) == 1:
        print(1)
        return
    dcnt = defaultdict(int)
    for i in range(N):
        xi, yi = P[i]
        for j in range(i):
            xj, yj = P[j]
            dx1, dy1 = xi-xj, yi-yj
            dx2, dy2 = xj-xi, yj-yi
            dcnt[(dx1,dy1)] += 1
            dcnt[(dx2,dy2)] += 1
    most = sorted(dcnt.values(), reverse=True)[0]
    ans = N - most
    print(ans)

diverta192_b()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def diverta192_b():
    N = int(input())
    P = [tuple(map(int, input().split())) for _ in range(N)]
    dcnt = defaultdict(int)
    for i in range(N):
        xi, yi = P[i]
        for j in range(i):
            xj, yj = P[j]
            dx1, dy1 = xi-xj, yi-yj
            dx2, dy2 = xj-xi, yj-yi
            dcnt[(dx1,dy1)] += 1
            dcnt[(dx2,dy2)] += 1
    most = sorted(dcnt.values(), reverse=True)[0]
    ans = N - most
    print(ans)

diverta192_b()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def diverta192_b():
    N = int(input())
    P = [tuple(map(int, input().split())) for _ in range(N)]
    dcnt = defaultdict(int)
    for i in range(N):
        xi, yi = P[i]
        for j in range(i):
            xj, yj = P[j]
            if xi > xj: dx, dy = xi-xj, yi-yj
            else: dx, dy = xj-xi, yj-yi
            dcnt[(dx,dy)] += 1
    most = sorted(dcnt.values(), reverse=True)[0]
    ans = N - most
    print(ans)

diverta192_b()

Buggy submission (verdict: Runtime Error):
def diverta192_b():
    N = int(input())
    P = [tuple(map(int, input().split())) for _ in range(N)]
    dcnt = defaultdict(int)
    for i in range(N):
        xi, yi = P[i]
        for j in range(i):
            xj, yj = P[j]
            if xi > xj: dx, dy = xi-xj, yi-yj
            else: dx, dy = xj-xi, yj-yi
            dcnt[(dx,dy)] += 1
    most = sorted(dcnt.values(), reverse=True)[0]
    ans = N - most
    print(ans)

diverta192_b()
Problem p03049 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    # _AB_ , B_A , B_ , _A の個数をそれぞれ数える
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  # これは確定
    if sted > 0:
        ans += (sted - 1)  # x個でx-1組のABができる
        if st > 0:
            ans += 1  # B_A + B_ で1組
            st -= 1
        if ed > 0:
            ans += 1  # _A + B_A で1組
            ed -= 1
    ans += min(st, ed)  # _A + B_ で1組
    print(ans)

diverta19_c()

Buggy submission (verdict: Runtime Error):
def diverta19_c():
    _ = int(readline())
    S = [ln.strip().decode('UTF-8') for ln in read().split()]

    # _AB_ , B_A , B_ , _A の個数をそれぞれ数える
    inner, st, ed, sted = 0, 0, 0, 0
    for si in S:
        inner += si.count('AB')
        if si[0] == 'B' and si[-1] == 'A': sted += 1
        elif si[0] == 'B': st += 1
        elif si[-1] == 'A': ed += 1

    ans = inner  # これは確定
    if sted > 0:
        ans += (sted - 1)  # x個でx-1組のABができる
        if st > 0:
            ans += 1  # B_A + B_ で1組
            st -= 1
        if ed > 0:
            ans += 1  # _A + B_A で1組
            ed -= 1
    ans += min(st, ed)  # _A + B_ で1組
    print(ans)

diverta19_c()
Problem p03074 (python)

Fixed submission:
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)
if s[-1] == '0':
  acc.append(acc[-1])

ans = 1
if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(acc)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(acc)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)

if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(counts)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(counts)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)
Problem p03089 (python)

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

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return list(reversed(ret))

A = solve(B)
#print(A)

if A == None:
  print(-1)
else:
  for a in A:
    print(a)

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

B = []
Btmp = [Bin[0]]
for i in range(1, N):
  if Bin[i] == 1:
    B.append(Btmp)
    Btmp = []
  Btmp.append(Bin[i])
else:
  B.append(Btmp)
#print(B)
#print(list(reversed(B)))

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return reversed(ret)

A = []
for Bsub in reversed(B):
  a = solve(Bsub)
  A.extend(a)
#print(A)
for a in A:
  print(a)
Problem p03157 (python)

Fixed submission:
def aising19_c():
    from collections import deque

    h, w = (int(x) for x in input().split())
    color = [[s == '#' for s in str(input())] for _ in range(h)]
    used = [[False]*w for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if used[i][j]: continue
            bk, wt = 0, 0
            que = deque()
            used[i][j] = True
            que.append((i, j))
            while que:
                ci, cj = que.popleft()
                if color[ci][cj]: bk += 1
                else: wt += 1
                for di, dj in ((-1,0),(0,-1),(0,1),(1,0)):
                    ni, nj = ci+di, cj+dj
                    if ni < 0 or h <= ni or nj < 0 or w <= nj: continue
                    if color[ci][cj] == color[ni][nj]: continue
                    if used[ni][nj]: continue
                    used[ni][nj] = True
                    que.append((ni, nj))
            ans += bk * wt
    print(ans)

if __name__ == '__main__':
    aising19_c()

Buggy submission (verdict: Runtime Error):
def aising19_c():
    h, w = (int(x) for x in input().split())
    grid = [[s for s in str(input())] for _ in range(h)]
    visited = [[False]*w for _ in range(h)]
    ans = 0

    def dfs(x, y):
        nonlocal ans
        visited[x][y] = True
        for dx, dy in ((-1,0),(0,-1),(0,1),(1,0)):
            nx, ny = x+dx, y+dy
            if nx < 0 or h <= nx or ny < 0 or w <= ny: continue
            if grid[nx][ny] == grid[x][y]: continue
            if visited[nx][ny]: continue
            if grid[nx][ny] == '.': ans += 1
            dfs(nx, ny)

    for i in range(h):
        for j in range(w):
            if grid[i][j] == '#':
                dfs(i, j)
                visited = [[False]*w for _ in range(h)]
    print(ans)

if __name__ == '__main__':
    aising19_c()
Problem p03160 (python)

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

cost = [0] * N
for i in range(1, N):
  d1 = cost[i-1] + abs(H[i] - H[i-1])
  d2 = cost[i-2] + abs(H[i] - H[i-2]) if i > 1 else 1e6
  cost[i] = min(d1, d2)
#print(cost)
ans = cost[N-1]
print(ans)

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

H = H + [10**9]
dp = [-1] * N

def dfs(pos):
  global N, H, dp

  if pos >= N:
    return 10**9
  if pos == N - 1:
    return 0
  if dp[pos] != -1:
    return dp[pos]

  step1 = dfs(pos+1) + abs(H[pos+1] - H[pos])
  step2 = dfs(pos+2) + abs(H[pos+2] - H[pos])
  dp[pos] = min(step1, step2)
  #print(pos, step1, step2)
  return(dp[pos])

ans = dfs(0)
#print(dp)
print(ans)
Problem p03163 (python)

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

weight = [None] * N
value = [None] * N
dp = [[None] * (W+1) for _ in range(N+1)]

for i in range(N):
  weight[i], value[i] = map(int, input().split())

for j in range(W+1):
  dp[0][j] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w])
    else:
      dp[i+1][w] = dp[i][w]

ans = dp[N][W]
print(ans)

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

import numpy as np
weight = np.empty((110), int)
value = np.empty((110), int)
dp = np.zeros((110, 10010), int)
#print(weight.shape, value.shape, dp.shape)

for i in range(N):
  weight[i], value[i] = map(int, input().split())

dp[0, :W+1] = 0

for i in range(N):
  for w in range(W+1):
    if w >= weight[i]:
      dp[i+1, w] = max(dp[i, w-weight[i]] + value[i], dp[i, w])
    else:
      dp[i+1, w] = dp[i, w]

ans = dp[N, W]
print(ans)
Problem p03215 (python)

Fixed submission:
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*41
    cand = B.copy()
    for i in range(40, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx.copy()
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()

Buggy submission (verdict: Runtime Error):
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*30
    cand = B
    for i in range(30, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()