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

Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 1
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt//2 <= K:
    f = 1
ans1 = ans2 = N-cnt
# print(ans1, ans2, cnt)
S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1
# ans1 += l
while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1
# ans1 += N-r-2
while 0 < r and S[r-1] == 0:
    r -= 1
# print(ans1)
while l < r and k < K:
    k += 1
    ans1 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1

S = [x for x in T]
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1
# ans2 += l
while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1
# ans2 += N-r-2
while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    ans2 += 2
    l += 1
    r -= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
print(max(ans1, ans2, f*N-1))

Buggy submission (verdict: Runtime Error):
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)
from collections import *
from bisect import *
from copy import deepcopy
mod = 10**9+7

N, K = map(int, input().split())
T = [0 if x == 'L' else 1 for x in input()]
f = 0
cnt = 0
for i in range(N-1):
    if T[i] != T[i+1]:
        cnt += 1
if cnt < 2:
    f = 1
S = deepcopy(T)
l, r, k = 0, N-1, 0
while l < N and S[l] == 1:
    l += 1
while l < N-1 and S[l+1] == 0:
    l += 1
while 0 <= r and S[r] == 1:
    r -= 1
while 0 < r and S[r-1] == 0:
    r -= 1
while l < r and k < K:
    k += 1
    for i in range(l+1, r):
        S[i] ^= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
ans1 = 0
i = 0
while i < N-1:
    c = 0
    while i < N-1:
        if S[i] == S[i+1]:
            i += 1
            c += 1
        else:
            ans1 += c
            c = 0
            i += 1
ans1 += c

S = deepcopy(T)
l, r, k = 0, N-1, 0
while l < N and S[l] == 0:
    l += 1
while l < N-1 and S[l+1] == 1:
    l += 1
while 0 <= r and S[r] == 0:
    r -= 1
while 0 < r and S[r-1] == 1:
    r -= 1
while l < r and k < K:
    k += 1
    for i in range(l+1, r):
        S[i] ^= 1
    while l < r and S[l] == S[l+1]:
        l += 1
    while l < r and S[r-1] == S[r]:
        r -= 1
ans2 = 0
i = 0
while i < N-1:
    c = 0
    while i < N-1:
        if S[i] == S[i+1]:
            i += 1
            c += 1
        else:
            ans2 += c
            c = 0
            i += 1
ans2 += c
print(max(ans1, ans2, f*N-1))
Problem p02928 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from collections import *
import math
from bisect import *
import functools
INF = float('inf')
mod = 10**9+7

class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0 for _ in range(N+1)]
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
bit = BIT(2001)
cnt = 0
for a in A:
    cnt += bit.sum(2000)-bit.sum(a)
    bit.add(a, 1)
X = 0
for a in A:
    X += bit.sum(a-1)
print((cnt*K+X*(K-1)*K//2)%mod)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from collections import *
import math
from bisect import *
import functools
INF = float('inf')
mod = 10**9+7

class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0 for _ in range(N+1)]
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N, K = map(int, input().split())
A = [int(x) for x in input().split()]
bit = BIT(N+1)
cnt = 0
for a in A:
    cnt += bit.sum(N)-bit.sum(a)
    bit.add(a, 1)
X = 0
for a in A:
    X += bit.sum(a-1)
print((cnt*K+X*(K-1)*K//2)%mod)
Problem p02976 (python)

Fixed submission:
from heapq import *
import sys
sys.setrecursionlimit(1000000)
def input():
    return sys.stdin.readline()[:-1]
N, M = map(int, input().split())
es = [[] for i in range(N+1)]
for id in range(M):
    a, b = map(int, input().split())
    es[a].append([b, id])
    es[b].append([a, id])
edge = [None] * M
h = [0] * (N+1)
lev = [None] * (N+1)
visit = [0] * (N+1)
def dfs(v, d):
    if visit[v]: return
    lev[v] = d
    visit[v] = 1
    for u, id in es[v]:
        if edge[id] == None:
            edge[id] = [v, u]
            h[v] ^= 1
            dfs(u, d+1)
dfs(1, 0)
q = [(-lev[v], v) for v in range(1, N+1)]
heapify(q)
while q:
    lv, v = heappop(q)
    lv = -lv
    if h[v] % 2 == 0: continue
    for u, id in es[v]:
        if lev[u] < lv:
            h[v] ^= 1
            h[u] ^= 1
            k, l = edge[id]
            edge[id] = [l, k]
            break
if h[1]:
    print(-1)
else:
    for u, v in edge:
        print(u, v)

Buggy submission (verdict: Runtime Error):
import sys
def input():
    return sys.stdin.readline()[:-1]
N, M = map(int, input().split())
es = [[] for i in range(N+1)]
edge = [None] * M
h = [0] * (N+1)
for id in range(M):
    a, b = map(int, input().split())
    es[a].append([b, id])
    edge[id] = [a, b]
    h[a] += 1

def rev(v, p, t, visit):
    if v == t:
        return True
    visit[v] = 0
    for i, (u, id) in enumerate(es[v]):
        if u != -1 and visit[u] and rev(u, v, t, visit):
            es[v][i] = [-1, id]
            es[u].append([v, id])
            edge[id] = [u, v]
            h[v] -= 1
            h[u] += 1

ps, pt, s, t = 0, 0, 0, 0
for i in range(N):
    for v in range(t+1, N+1):
        if h[v] % 2:
            s = v
            break
    if s == ps:
        break
    for v in range(s+1, N+1):
        if h[v] % 2:
            t = v
            break
    if t == pt:
        print(-1)
        quit()
    rev(s, -1, t, [1] * (N+1))
    ps, pt = s, t
for u, v in edge:
    print(u, v)

Buggy submission (verdict: Runtime Error):
import sys
def input():
    return sys.stdin.readline()[:-1]
N, M = map(int, input().split())
es = [[] for i in range(N+1)]
edge = [None] * M
h = [0] * (N+1)
for id in range(M):
    a, b = map(int, input().split())
    es[a].append([b, id])
    edge[id] = [a, b]
    h[a] += 1

def rev(v, p, t, visit):
    if v == t:
        return True
    visit[v] = 0
    for i, (u, id) in enumerate(es[v]):
        if u != -1 and visit[u] and rev(u, v, t, visit):
            es[v][i] = [-1, id]
            es[u].append([v, id])
            edge[id] = [u, v]
            h[v] -= 1
            h[u] += 1

ps, pt, s, t = 0, 1, 0, 0
while True:
    for v in range(t+1, N+1):
        if h[v] % 2:
            s = v
            break
    if s == ps:
        break
    for v in range(s+1, N+1):
        if h[v] % 2:
            t = v
            break
    if t == pt:
        print(-1)
        quit()
    rev(s, -1, t, [1] * (N+1))
    ps, pt = s, t
for u, v in edge:
    print(u, v)
Problem p03006 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import defaultdict
mod = 10**9+7

N = int(input())
xy = [list(map(int, input().split())) for i in range(N)]
xy.sort()
Ans = N
for i in range(N):
    for j in range(i+1, N):
        a, b = xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]
        ans = N
        for k in range(N):
            for l in range(k+1, N):
                if xy[k][0]+a == xy[l][0] and xy[k][1]+b == xy[l][1]:
                    ans -= 1
        Ans = min(Ans, ans)
print(Ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import defaultdict
mod = 10**9+7

N = int(input())
xy = [list(map(int, input().split())) for i in range(N)]
cnt = defaultdict(int)
for i in range(N):
    for j in range(i+1, N):
        a, b = xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]
        cnt[(a, b)] += 1
        cnt[(-a, -b)] += 1
print(N-max(cnt.values()))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n=int(input())
p=[list(map(int,input().split())) for i in range(n)]
dic=defaultdict(int)
for i, (x,y)in enumerate(p):
  for j, (u,v) in enumerate(p):
    if i==j:
      continue
    dic[(x-u,y-v)]+=1
print(n-max(dic,key=lambda z:z[1])[1])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n=int(input())
p=[list(map(int,input().split())) for i in range(n)]
dic=defaultdict(int)
for i, (x,y)in enumerate(p):
  for j, (u,v) in enumerate(p):
    if i==j:
      continue
    dic[(x-u,y-v)]+=1
print(n-max(dic,key=lambda z:z[1]))
Problem p03043 (python)

Fixed submission:
import math

N, K = map(int, input().split())
ans = 0
for m in range(1, N+1):
    x = math.log2(K / m)
    if x - int(x) > 0:
        x += 1
    if x > 1:
        ans += (1/2) ** int(x) / N
    else:
        ans += 1 / N
print(ans)

Buggy submission (verdict: Runtime Error):
import math

N, K = map(int, input().split())
ans = 0
for m in range(1, N+1):
    x = math.log2(K / m)
    if x - int(x) > 0:
        x += 1
    if x > 1:
        ans += (1/2) ** int(x) / N
    else:
        ans += 1 / N
print(ans)
Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5+1)
N = int(input())
es = [[] for i in range(N+1)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    es[u].append([v,w])
    es[v].append([u,w])
dfs_map = [1 for i in range(N+1)]
col = [None] * (N+1)
def dfs(v, c):
    col[v] = c
    for e in es[v]:
        if dfs_map[e[0]]:
            dfs_map[e[0]] = 0
            if e[1] % 2:
                dfs(e[0], c^1)
            else:
                dfs(e[0], c)
dfs_map[1] = 0
dfs(1, 0)
for i in range(1, N+1):
    print(col[i])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000)
N = int(input())
es = [[] for i in range(N+1)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    es[u].append([v,w])
    es[v].append([u,w])
dfs_map = [1 for i in range(N+1)]
col = [None] * (N+1)
def dfs(v, c):
    col[v] = c
    for e in es[v]:
        if dfs_map[e[0]]:
            dfs_map[e[0]] = 0
            if e[1] % 2:
                dfs(e[0], c^1)
            else:
                dfs(e[0], c)
dfs_map[1] = 0
dfs(1, 0)
for i in range(1, N+1):
    print(col[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
es = [[] for i in range(N+1)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    es[u].append([v,w])
    es[v].append([u,w])
dfs_map = [1 for i in range(N+1)]
col = [None] * (N+1)
def dfs(v, c):
    col[v] = c
    for e in es[v]:
        if dfs_map[e[0]]:
            dfs_map[e[0]] = 0
            if e[1] % 2:
                dfs(e[0], c^1)
            else:
                dfs(e[0], c)
dfs_map[1] = 0
dfs(1, 0)
for i in range(1, N+1):
    print(col[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
es = [[] for i in range(N+1)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    es[u].append([v,w])
    es[v].append([u,w])
dfs_map = [1 for i in range(N+1)]
col = [None] * (N+1)
def dfs(v, c):
    col[v] = c
    for e in es[v]:
        if dfs_map[e[0]]:
            dfs_map[e[0]] = 0
            if e[1] % 2:
                dfs(e[0], c^1)
            else:
                dfs(e[0], c)
dfs(1, 0)
for i in range(1, N+1):
    print(col[i])
Problem p03047 (python)

Fixed submission:
N, K = map(int, input().split())
print(N - K + 1)

Buggy submission (verdict: Runtime Error):
import math
N = int(input())
n = int(math.sqrt(N))
if n * n != N:
    n += 1
ans = 0
for i in range(1, n):
    if N % i ==  0:
        ans += N // i - 1
if N <= 2:
    ans = 0
print(ans)
Problem p03050 (python)

Fixed submission:
import math
N = int(input())
n = int(math.sqrt(N))
ans = 0
for x in range(1, n+1):
    m = N // x - 1
    if m > 0 and N % x == 0 and x == N % m:
        ans += m
print(ans)

Buggy submission (verdict: Runtime Error):
import math
N = int(input())
n = int(math.sqrt(N))
ans = 0
for x in range(1, n+1):
    m = N // x - 1
    if N % x == 0 and x == N % m:
        ans += m
print(ans)
Problem p03069 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

n = int(input())
S = input()
f = [0] * (n+1)
for i in range(n):
    f[i+1] = f[i] + (S[i] == '#')
g = [0] * (n+1)
for i in range(n):
    g[i+1] = g[i] + (S[i] == '.')
k = S.index('#') if '#' in S else 0
ans = min(f[n], g[n]-g[k])
lst = []
for i in range(n-1):
    if S[i:i+2] == '#.':
        lst.append(i)
for i, j in zip(lst, lst[1:]):
    ans = min(ans, f[i+1]+g[n]-g[j])
print(ans)


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

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

n = int(input())
S = input()
ans = 0
l = 0
while l < n and S[l] == '.':
    l += 1
for r in range(l, n-1):
    if S[r:r+2] == '#.':
        m = r
    if S[r:r+2] == '.#':
        ans += min(r-m, m-l)+1
        l = r+1
if S[-1] == '.':
    ans += min(r-m, m-l)+1
print(ans)
Problem p03074 (python)

Fixed submission:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
if l0 < K:
    K = l0
ans = sum(c0[0:K]) + sum(c1[0:K])
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
if l0 == 2 and S[0] == '1':
    ans = N
else:
    for i in range(1, l):
        tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1]
        if i - 2 >= 0:
            tmp -= c1[i-2]
        if ans < tmp:
            ans = tmp
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
ans = c0[K-1] + c1[K-1]
for i in range(1, l):
    tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
    if ans < tmp:
        ans = tmp
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
ans = c0[K] + c1[K]
for i in range(1, l):
    tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
    if ans < tmp:
        ans = tmp
print(ans)
Problem p03088 (python)

Fixed submission:
import itertools
def memo_rec(N):
    if N == 3:
        return 61
    if N == 4:
        return 230
    L = "ATGC"
    MOD = 10 ** 9 + 7
    dic = {}
    def rec(s, k):
        if s.count("AGC"):
            return 0
        for i in range(3):
            t1, t2 = s[i], s[i + 1]
            if (s[:i] + t2 + t1 + s[i+2:]).count("AGC"):
                return 0
        if k == 0:
            return 1
        if s + str(k) in dic:
            return dic[s + str(k)]
        else:
            ret = 0
            for x in L:
                ret = (ret + rec(str(x + s[:3]), k - 1)) % MOD
            dic[s + str(k)] = ret
            return ret
    ret = 0
    for s in itertools.product(L, repeat = 4):
        t = ''
        for x in s:
            t += x
        ret = (ret + rec(t, N - 4)) % MOD
    return ret

N = int(input())
print(memo_rec(N))

Buggy submission (verdict: Runtime Error):
def memo_rec(N):
    if N == 3:
        return 61
    L = ['A', 'T', 'G', 'C']
    MOD = 10 ** 9 + 7
    dic = {}
    def rec(l1, l2, l3, k):
        s = l1 + l2 + l3
        if s in ["AGC", "ACG", "GAC"]:
            return 0
        if k == 1:
            if (l1 == 'A' and l2 == 'C') or (l1 == 'C' and l2 == 'G') or (l1 == 'G' and l3 == 'C') or (l2 == 'G' and l3 == 'C') or (l1 == 'G' and l2 == 'C'):
                return 3
            else:
                return 4
        if s + str(k) in dic:
            return dic[s + str(k)]
        else:
            ret = 0
            for x in L:
                ret = (ret + rec(x, l1, l2, k - 1)) % MOD
            dic[s + str(k)] = ret
            return ret
    ret = 0
    for s in itertools.product(L, repeat = 3):
        ret = (ret + rec(s[0], s[1], s[2], N - 3)) % MOD
    return ret

N = int(input())
print(memo_rec(N))

Buggy submission (verdict: Runtime Error):
import itertools

def memo_rec(N):
    L = ['A', 'T', 'G', 'C']
    MOD = 10 ** 9 + 7
    dic = {}
    def rec(l1, l2, l3, k):
        s = l1 + l2 + l3
        if s in ["AGC", "ACG", "GAC"]:
            return 0
        if k == 1:
            if (l1 == 'A' and l2 == 'C') or (l1 == 'C' and l2 == 'G') or (l1 == 'G' and l3 == 'C') or (l2 == 'G' and l3 == 'C') or (l1 == 'G' and l2 == 'C'):
                return 3
            else:
                return 4
        if s + str(k) in dic:
            return dic[s + str(k)]
        else:
            ret = 0
            for x in L:
                ret = (ret + rec(x, l1, l2, k - 1)) % MOD
            dic[s + str(k)] = ret
            return ret
    ret = 0
    for s in itertools.product(L, repeat = 3):
        ret = (ret + rec(s[0], s[1], s[2], N - 3)) % MOD
    return ret

N = int(input())
print(memo_rec(N))
Problem p03106 (python)

Fixed submission:
A, B, k = map(int, input().split())
lst = []
for i in range(1, 101):
  if A % i == 0 and B % i == 0:
    lst.append(i)
print(lst[-k])

Buggy submission (verdict: Runtime Error):
A, B, k = map(int, input().split())
lst = []
for i in range(1, 100):
  if A % i and B % i == 0:
    lst.append(i)
print(lst[-k])
Problem p03126 (python)

Fixed submission:
N, M = map(int, input().split())
d = {}
for i in range(N):
    lst = list(map(int, input().split()))
    for a in range(1, len(lst)):
        if lst[a] in d:
            d[lst[a]] += 1
        else:
            d[lst[a]] = 1
ans = 0
for key in d:
    if d[key] == N:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())
d = defaultdict(int)
for i in range(N):
    lst = list(map(int, input().split()))
    for a in range(1, len(lst)):
        d[lst[a]] += 1
ans = 0
for i in range(1, M+1):
    if d[i] == N:
        ans += 1
print(ans)
from collections import defaultdict
N, M = map(int, input().split())
d = defaultdict(int)
for i in range(N):
    lst = list(map(int, input().split()))
    for a in range(1, len(lst)):
        d[lst[a]] += 1
ans = 0
for i in range(1, M+1):
    if d[i] == N:
        ans += 1
print(ans)
Problem p03128 (python)

Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse = True)
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(N+1)]
INF = float('inf')
for i in range(1, N+1):
    t = -INF
    for a in A:
        if i >= x[a]:
            t = max(dp[i-x[a]] + 1, t)
    dp[i] = t

K = N
k = dp[N]
ans = ""
while k > 0:
    for a in A:
        xa = x[a]
        if K >= xa and k == dp[K-xa] + 1:
            ans += str(a)
            K -= xa
            k -= 1
            break
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100001)
def input():
    return sys.stdin.readline()[:-1]

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse = True)
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(N+1)]
for i in range(2, N+1):
    for a in A:
        if i >= x[a] and dp[i-x[a]] + 1 > dp[i]:
            dp[i] = dp[i-x[a]] + 1
K = N
ans = ''
while K > 0:
    for a in A:
        if dp[K] == dp[K-x[a]] + 1:
            if dp[K] == 1 and K != x[a]:
                continue
            ans += str(a)
            K -= x[a]
            break
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100001)
def input():
    return sys.stdin.readline()[:-1]
    
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse = True)
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(N+1)]
for i in range(2, N+1):
    for a in A:
        if i >= x[a] and dp[i-x[a]] + 1 > dp[i]:
            dp[i] = dp[i-x[a]] + 1
K = N
ans = ''
while K > 0:
    for a in A:
        if dp[K] == dp[K-x[a]] + 1:
            ans += str(a)
            K -= x[a]
            break
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A, reverse = True)
c = {}
for key in A:
    if key == 1:
        c[key] = 2
    elif key in [2, 3, 5]:
        c[key] = 5
    elif key == 4:
        c[key] = 4
    elif key in [6, 9]:
        c[key] = 6
    elif key == 7:
        c[key] = 3
    else:
        c[key] = 7

dp = [-1e9] * (N+1)
dp[0] = 0
for i in range(1, N+1):
    for key in c:
        if dp[i-c[key]] >= 0:
            dp[i] = max(dp[i-c[key]] + 1, dp[i])

K = N
while (K > 0):
    for key in c:
        if dp[K - c[key]] == dp[K] - 1:
            print(key, end = "")
            K -= c[key]
            break
print()
Problem p03145 (python)

Fixed submission:
a, b, c = map(int, input().split())
print(int(a * b / 2))

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
print(a＊b/2)
Problem p03146 (python)

Fixed submission:
s = int(input())
lst = []
lst.append(s)
a = s
i = 2
while True:
  if a % 2 == 0:
    b = int(a / 2)
  else:
    b = 3 * a + 1
  if b in lst:
    print(i)
    break
  lst.append(b)
  a = b
  i += 1

Buggy submission (verdict: Runtime Error):
s = int(input())
lst = []
lst.append(s)
i = 2
while true:
  if i % 2 == 0:
    a = i / 2
  else:
    a = 3 * i + 1
  if a in lst:
    print(i)
    break
  lst.append(a)
Problem p03147 (python)

Fixed submission:
def rec(lst, left, right):
    if left == right:
        return 0
    if not 0 in lst[left:right]:
        m = min(lst[left:right])
        lst[left:right] = [x - m for x in lst[left:right]]
        return m + rec(lst, left, right)
    k = lst[left:right].index(0)
    return rec(lst, left, left + k) + rec(lst, left + k+1, right)


N = int(input())
lst = list(map(int, input().split()))
print(rec(lst, 0, len(lst)))

Buggy submission (verdict: Runtime Error):
def solve(lst):
    def rec(left, right):
        if left == right:
            return 0
        res = 0
        if not 0 in lst[left:right]:
            m = min(lst)
            for i in range(left, right):
                lst[i] = lst[i] - m
            res += m
        m = 1000
        for i in range(left, right):
            if lst[i] > 0:
                m = min(m, lst[i])
            else:
                for j in range(left, i):
                    lst[j] = lst[j] - m
                if left != i:
                    res += m
                return res + rec(left, i) + rec(i+1, right)
    return rec(0, len(lst))


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