Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
import string

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    for i,c in enumerate(s):
        sa[ord(c)-ord('a')].append(i+1)
#    print(sa)

    ls = len(s)
    ans = 1 # 条件を満たす整数
    i = 1 # s の何番目の文字から探し始めるか
    for c in t:
        a = sa[ord(c)-ord('a')]
        if len(a) == 0 or i > a[-1]:
            ans += ls-i+1
            i = 1
        for j in a:
            if j >= i:
                ans += j-i+1
                i = j+1
                break
#        print("c,a,ans,i=", c, a, ans, i)
    print(ans-1)

if __name__ == '__main__':
    main()


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

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    ai = ord('a')
    for i,c in enumerate(s):
        sa[ord(c)-ai].append(i+1)
#    print(sa)

    ls = len(s)
    ans = 1 # 条件を満たす整数
    i = 1 # s の何番目の文字から探し始めるか
    for c in t:
        a = sa[ord(c)-ai]
        if len(a) == 0 or i > a[-1]:
            ans += ls-i+1
            i = 1
        bi = bisect.bisect_left(a,i)
        if bi < len(a):
            ans += a[bi]-i+1
            i = a[bi]+1
#        print("c,a,ans,i=", c, a, ans, i)
    print(ans-1)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = []
for i in range(N):
    s = input()
    S.append("".join(sorted(s)))
S.sort()

k = 0
while len(S) > 1:
    ss = S[0]
    n = S.count(ss)
    k += n*(n-1)//2
    S = S[n:]

print(k)


Fixed submission:
N = int(input())
k = 0
h = {}
for i in range(N):
    s = input()
    ss = "".join(sorted(s))
    if ss in h:
        k += h[ss]
    else:
        h[ss] = 0
    h[ss] += 1

print(k)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = []
for i in range(N):
    s = input()
    S.append("".join(sorted(s)))

k = 0
while len(S) > 0:
    ss = S[0]
    n = S.count(ss)
    k += n*(n-1)//2
    while ss in S:
        S.remove(ss)
print(k)


Fixed submission:
N = int(input())
k = 0
h = {}
for i in range(N):
    s = input()
    ss = "".join(sorted(s))
    if ss in h:
        k += h[ss]
    else:
        h[ss] = 0
    h[ss] += 1

print(k)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = []
for i in range(N):
    s = input()
    S.append("".join(sorted(s)))

S.sort()

k = 0
for i in range(N):
    for j in range(i+1,N):
        if S[i] == S[j]:
            k += 1
        else:
            break
print(k)


Fixed submission:
N = int(input())
k = 0
h = {}
for i in range(N):
    s = input()
    ss = "".join(sorted(s))
    if ss in h:
        k += h[ss]
    else:
        h[ss] = 0
    h[ss] += 1

print(k)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import collections
import heapq
input = sys.stdin.readline


def main():
    N, M = map(int, input().split())
    AB = []
    for _ in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(key=lambda x: x[0])

    H = []
    I = 0
    ans = 0
    for m in range(M):
        for i in range(I, N):
            if AB[i][0] <= m+1:
                heapq.heappush(H, -AB[i][1])
                I += 1
        if H:
            ans += -heapq.heappop(H)
    print(ans)


if __name__ == '__main__':
    main()


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


def main():
    N, M = map(int, input().split())
    AB = []
    for _ in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(key=lambda x: x[0])

    H = []
    I = 0
    ans = 0
    for m in range(M):
        for i in range(I, N):
            if AB[i][0] <= m+1:
                heapq.heappush(H, -AB[i][1])
                I += 1
            else:
                break
        if H:
            ans += -heapq.heappop(H)
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

for i in range(0,n):
    hi = h[i]
    for j in range(i,n):
        if hi - h[j] > 1:
            print("No")
            exit()

print("Yes")


Fixed submission:
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

max = 0
for i in range(0,n):
    if max < h[i]:
        max = h[i]
    if max - h[i] > 1:
        print("No")
        exit()

print("Yes")

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

b = True
for i in range(0,n):
    for j in range(i,n):
        if h[i] - h[j] > 1:
            print("No")
            b = False
if b:
    print("Yes")


Fixed submission:
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

max = 0
for i in range(0,n):
    if max < h[i]:
        max = h[i]
    if max - h[i] > 1:
        print("No")
        exit()

print("Yes")

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
h = input().split()

b = True
for i in range(0,n):
    for j in range(i,n):
        if int(h[i]) - int(h[j]) > 1:
            print("No")
            b = False
if b:
    print("Yes")


Fixed submission:
n = int(input())
hs = input().split()

h = []
for s in hs:
    h.append(int(s))

max = 0
for i in range(0,n):
    if max < h[i]:
        max = h[i]
    if max - h[i] > 1:
        print("No")
        exit()

print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
h = []
n = len(s)
for i in range(0,n):
    h.append(0)

n = len(s)
for i in range(0,n):
    if s[i] == "R":
        j = s.find("RL", i)
        d = j-i
    elif s[i] == "L":
        j = s.rfind("RL", 0, i+1)
        d = i-j
    if d % 2 == 1:
        h[j+1] += 1
    else:
        h[j] += 1

ans = ""
for x in h:
    ans += str(x) + " "
print(ans)


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

def runLength(s):
    a = []
    for c in s:
        if len(a)==0 or a[-1][0]!=c:
            a.append([c,1])
        else:
            a[-1][1] += 1
    return a

s = input().strip()
a = runLength(s)

#print(a)
ans = [0]*len(s)
j = 0
for i in range(len(s)-1):
    if s[i] == "R" and s[i+1] == "L":
        ans[i] += a[j][1]//2 + a[j+1][1]//2
        ans[i+1] += a[j][1]//2 + a[j+1][1]//2
        if a[j][1] % 2 != 0:
            ans[i] += 1
        if a[j+1][1] % 2 != 0:
            ans[i+1] += 1
#        print(j, ans)
        j += 2
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
h = []
for i in range(0,len(s)):
    h.append(0)

for i in range(0,len(s)):
    if s[i] == "R":
        j = s.find("RL", i)
        if (j-i) % 2 == 1:
            h[j+1] += 1
        else:
            h[j] += 1
    elif s[i] == "L":
        j = s.rfind("RL", 0, i+1)
        if (i-j) % 2 == 1:
            h[j+1] += 1
        else:
            h[j] += 1

ans = ""
for x in h:
    ans += str(x) + " "
print(ans)


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

def runLength(s):
    a = []
    for c in s:
        if len(a)==0 or a[-1][0]!=c:
            a.append([c,1])
        else:
            a[-1][1] += 1
    return a

s = input().strip()
a = runLength(s)

#print(a)
ans = [0]*len(s)
j = 0
for i in range(len(s)-1):
    if s[i] == "R" and s[i+1] == "L":
        ans[i] += a[j][1]//2 + a[j+1][1]//2
        ans[i+1] += a[j][1]//2 + a[j+1][1]//2
        if a[j][1] % 2 != 0:
            ans[i] += 1
        if a[j+1][1] % 2 != 0:
            ans[i+1] += 1
#        print(j, ans)
        j += 2
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []
a = list(map(int, input().split()))

ans = []
for i in range(n):
    ans.append(0)

for i in range(len(a)-1, -1, -1):
    k = 0
    for j in range(i, len(a), 1):
        if i > 0 and j % i == 0:
            k += ans[j]
    if a[i] != ans[j]:
        ans[i] = 1

print(ans.count(1))
for i in range(len(ans)):
    if ans[i] != 0:
        print(i+1)



Fixed submission:
n = int(input())
a = list(map(int, input().split()))

ans = []
for i in range(n):
    if i < n//2:
        ans.append(0)
    else:
        ans.append(a[i])

for i in range(n//2-1, -1, -1):
    ans[i] = (a[i] + sum(ans[i::i+1])) % 2

print(ans.count(1))
for i in range(n):
    if ans[i] != 0:
        print(i+1, "", end="")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int,input().split()))
    m = min(A)
    xi = A.index(m)
    for a in range(0, m*2+1, 2):
        B = A.copy()
        ans = [0]*N
        B[(xi-1)%N] -= a//2
        B[xi] -= a//2
        ans[xi] = a
        for i in range(xi+1, xi+N):
            x = B[(i-1)%N]*2
            if B[i%N]-x//2 < 0:
                break
            B[(i-1)%N] -= x//2
            B[i%N] -= x//2
            ans[i%N] += x
            if sum(B) == 0:
                print(*ans)
                return

if __name__ == '__main__':
    main()


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

def main():
    N = int(input())
    A = list(map(int,input().split()))
    ans = [0]*N
    ans[0] = sum(A)
    for i in range(1,N,2):
        ans[0] -= 2*A[i]
    for i in range(1,N):
        ans[i] = 2*A[i-1]-ans[i-1]
    print(*ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math
N, K = list(map(int, input().split()))

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

for i in range(1,K+1):
    if N-K+1 - i < 0 or K-1 < i-1:
        print(0)
    else:
        k = combo(N-K+1,i) * combo(K-1,i-1)
        ans = k%(math.pow(10,9)+7)
        print("%d" %(ans))


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

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

for i in range(1,K+1):
    if N-K+1 < i or K-1 < i-1:
        print(0)
    else:
        k = combo(N-K+1,i) * combo(K-1,i-1)
        ans = k%(10**9+7)
        print("%d" %(ans))

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

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

if N == K:
    print(1)
else:
    for i in range(1,K+1):
        k = combo(N-K+1,i) * combo(K-1,i-1)
        ans = k%(math.pow(10,9)+7)
        print("%d" %(ans))


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

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

for i in range(1,K+1):
    if N-K+1 < i or K-1 < i-1:
        print(0)
    else:
        k = combo(N-K+1,i) * combo(K-1,i-1)
        ans = k%(10**9+7)
        print("%d" %(ans))

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

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

for i in range(1,K+1):
    n = combo(N-K+1,i)
    k = n * combo(K-1,i-1)
    ans = k%(math.pow(10,9)+7)
    print("%d" %(ans))


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

def combo(n, k):
    x = math.factorial(n)//(math.factorial(k)*math.factorial(n-k))
    return x

for i in range(1,K+1):
    if N-K+1 < i or K-1 < i-1:
        print(0)
    else:
        k = combo(N-K+1,i) * combo(K-1,i-1)
        ans = k%(10**9+7)
        print("%d" %(ans))
Buggy python submission (verdict: Runtime Error):
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

a,b,c,d = map(int, input().split())

x = (b//c - (a-1)//c) + (b//d - (a-1)//d -(b//lcm(c,d)) + (a-1)//lcm(c,d))
print(b-a+1-x)


Fixed submission:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b,c,d = map(int, input().split())

x = (b//c - (a-1)//c) + (b//d - (a-1)//d -(b//lcm(c,d)) + (a-1)//lcm(c,d))
print(b-a+1-x)

Buggy python submission (verdict: Runtime Error):
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

A,B,C,D= map(int, input().split())

mc = B//C - (A-1)//C
md = B//D - (A-1)//D
mcd = B//lcm(C,D) - (A-1)//lcm(C,D)

print(B-A+1-mc-md+mcd)


Fixed submission:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b,c,d = map(int, input().split())

x = (b//c - (a-1)//c) + (b//d - (a-1)//d -(b//lcm(c,d)) + (a-1)//lcm(c,d))
print(b-a+1-x)

Buggy python submission (verdict: Runtime Error):
import math

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

A,B,C,D= map(int, input().split())

mc = B//C - A//C
if A%C == 0:
    mc += 1
md = B//D - A//D
if A%D == 0:
    mc += 1

mcd = B//lcm(C,D) - A//lcm(C,D)
if A%lcm(C,D) == 0:
    mcd += 1

print(B-A+1-mc-md+mcd)


Fixed submission:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b,c,d = map(int, input().split())

x = (b//c - (a-1)//c) + (b//d - (a-1)//d -(b//lcm(c,d)) + (a-1)//lcm(c,d))
print(b-a+1-x)
Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
L = list(map(int, input().split()))

d = 0
k = 1
for i in range(N+1):
    d += L[i]
    if d <= X:
        k += 1
    else:
        break
print(k)


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

d = 0
k = 1
for i in L:
    d += i
    if d <= X:
        k += 1
    else:
        break
print(k)
Buggy python submission (verdict: Runtime Error):
w,h,x,y= map(int, input().split())

d=w*h/2
if x == w/2 or y == h/2:
    k = 0
if x == w/2 and y == h/2:
    k = 1
print(d, k)


Fixed submission:
w,h,x,y= map(int, input().split())

k = 0
if x == w/2 or y == h/2:
    k = 0
if x == w/2 and y == h/2:
    k = 1

print("%f %d" %(w*h/2, k))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = []
    for _ in range(N):
        x, y = map(int, input().split())
        A.append((x, y))

    import collections
    D = collections.defaultdict(int)
    import itertools
    for a, b in itertools.combinations(A, 2):
        p = a[0] - b[0]
        q = a[1] - b[1]
        D[(p, q)] += 1
    d = sorted(D.values())
    print(N - d[-1])


if __name__ == '__main__':
    main()


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


def main():
    N = int(input())
    A = []
    for _ in range(N):
        x, y = map(int, input().split())
        A.append((x, y))
    if N == 1:
        print(1)
        return

    A.sort()
    import collections
    D = collections.defaultdict(int)
    import itertools
    for a, b in itertools.combinations(A, 2):
        p = a[0] - b[0]
        q = a[1] - b[1]
        D[(p, q)] += 1
    d = sorted(D.values())
    print(N - d[-1])


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

n,m = map(int, input().split())
a = []
for i in range(m):
    a.append(int(input()))

dp = [0]*(n+1)
dp[0] = 1
dp[1] = 0 if 1 in a else 1
for i in range(2,n+1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = dp[i-1] + dp[i-2]

print(dp[n]%(10**9+7))


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

def fib(n):
    a, b = 1, 0
    for _ in range(n):
        a, b = a + b, a
    return b

n,m = map(int, input().split())
a = []
for i in range(m):
    a.append(int(input()))
if len(a) >= 2:
    for i in range(m-1):
        if a[i]+1 == a[i+1]:
            print(0)
            exit()

tmp = 0
ans = 1
for i in range(len(a)):
    ans *= fib(a[i]-tmp)
    tmp = a[i]+1
if tmp < n:
    ans *= fib(n-tmp+1)
print(ans%(10**9+7))

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

n,m = map(int, input().split())
a = []
for i in range(m):
    a.append(int(input()))
if len(a) >= 2:
    for i in range(m-1):
        if a[i]+1 == a[i+1]:
            print(0)
            exit()

def fib(n):
    if n <= 1:
        return 1
    return fib(n-1) + fib(n-2)
tmp = 0
ans = 1
for i in range(len(a)):
    ans *= fib(a[i]-tmp-1)
    tmp = a[i]+1
if tmp < n:
    ans *= fib(n-tmp)
print(ans%(10**9+7))


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

def fib(n):
    a, b = 1, 0
    for _ in range(n):
        a, b = a + b, a
    return b

n,m = map(int, input().split())
a = []
for i in range(m):
    a.append(int(input()))
if len(a) >= 2:
    for i in range(m-1):
        if a[i]+1 == a[i+1]:
            print(0)
            exit()

tmp = 0
ans = 1
for i in range(len(a)):
    ans *= fib(a[i]-tmp)
    tmp = a[i]+1
if tmp < n:
    ans *= fib(n-tmp+1)
print(ans%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    ans = 0
    while 1:
        i = S.find("ABC")
        if i < 0:
            break
        li = 0
        for l in range(i-1,-1,-1):
            if S[l] == "A":
                li += 1
            else:
                break
        ri = 0
        for r in range(i+3,len(S)-1,2):
            if S[r] == "B" and S[r+1] == "C":
                ri += 1
            else:
                break
        x = min(li, ri)
        S = S[:i-x] + "BC"*(x+1) + "A"*(x+1) + S[i+3 + x*2:]
        ans += 1 if x == 0 else (x+1)**2
        
    print(ans)

if __name__ == '__main__':
    main()


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

def main():
    S = input().strip()
    S = S.replace("BC","D")
    ans = 0
    a = 0
    for c in S:
        if c == "A":
            a += 1
        elif c == "D":
            ans += a
        else:
            a = 0
        
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    ans = 0
    while 1:
        c = S.count("ABC")
        if c == 0:
            break
        else:
            ans += c
            S = S.replace("ABC", "BCA")
    print(ans)

if __name__ == '__main__':
    main()


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

def main():
    S = input().strip()
    S = S.replace("BC","D")
    ans = 0
    a = 0
    for c in S:
        if c == "A":
            a += 1
        elif c == "D":
            ans += a
        else:
            a = 0
        
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

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

g = set(i for i in range(1,N+1))
for i in range(M):
    l,r = map(int, input().split())
    t = set(i for i in range(l,r+1))
    g = g & t

print(len(g))


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

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

l,r = 1,N
for i in range(M):
    l1,r1 = map(int, input().split())
    if l1>l:
        l = l1
    if r1<r:
        r = r1
if r-l<0:
    print(0)
else:
    print(r-l+1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


def main():
    R, G, B, N = map(int, input().split())
    ans = 0
    for r in range(N+1):
        if r*R > N:
            continue
        for g in range(N+1):
            if r*R + g*G > N:
                continue
            if (N - r*R - g*G) % B == 0:
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()


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


def main():
    R, G, B, N = map(int, input().split())
    ans = 0
    for r in range(N+1):
        if r*R > N:
            continue
        for g in range(N+1):
            if r*R + g*G > N:
                continue
            if (N - r*R - g*G) % B == 0:
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

n,q = map(int, input().split())
s = input()

for i in range(q):
    l,r = map(int, input().split())
    ss = s[l-1:r]
    k = 0
    for j in range(len(ss)-1):
        if ss[j] == "A" and ss[j+1] == "C":
            k += 1
    print(k)


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

n,q = map(int, input().split())
s = input()

t = [0]*(n)
for i in range(n-1):
    if s[i] == "A" and s[i+1] == "C":
        t[i+1] = t[i] + 1
    else:
        t[i+1] = t[i]

for i in range(q):
    l,r = map(int, input().split())
    print(t[r-1] - t[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

ans = 0
# 文字列 s2 の末尾までを走査して、
# s1 にまだ表れていない文字があれば連結して、
# 連結後の s1 と、s2 から走査済みの文字を除いたものを返す
def func(s1, s2):
    global ans
    if s2 == "":
        return None
    for i,c in enumerate(s2):
        if c not in s1:
            ans += 1
            t1 = s1 + c
            t2 = s2[i+1:]
#            print(t1, t2, ans)
            func(t1, t2)

N = int(input())
S = input().strip()

func("", S)
print(ans)


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

N = int(input())
S = input().strip()
h = {}
for c in S:
    if c in h:
        h[c] += 1
    else:
        h[c] = 1

ans = 1
for v in h.values():
    ans *= v+1
ans -= 1

print(ans%(10**9+7))
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

def func(dst, src, mp):
    if len(dst) == 0: # dst が空になったら終了
        return mp
    for s in src: # 長さが一致していたら何もせず除外
        if s in dst:
            src.remove(s)
            dst.remove(s)
#            print("removed", s, src, dst, mp)
    for i in dst: # 持っているどの竹よりも短い dst には、短縮魔法を使うしかない
        if i < min(src):
            tmp = min(src)
            mp += min(src) - i
            src.remove(tmp)
            dst.remove(i)
#            print("shorten", tmp, src, dst, mp)
    # 持っている竹のうち、mp 消費が10未満で処理できるものがあれば、その中で最小のものを延長
    mi = 10
    dd = None
    ss = None
    for s in src:
        for d in dst:
            tmp = mi
            mi = min(mi, abs(d-s))
            if mi != tmp:
                dd = d
                ss = s
    if mi < 10:
        mp += dd - ss
        src.remove(ss)
        dst.remove(dd)
#        print("extend", ss, src, dst, mp)
    # 結合した時に、目標に近くなる 2 本を結合
    mi = 1000
    for s in src:
        for d in dst:
            mi = min(mi, abs(s-d))
    s1 = s2 = None
    for i in range(len(src)):
        for j in range(i+1,len(src)):
            for d in dst:
                tmp = mi
                mi = min(mi, abs(d - (src[i] + src[j])))
                if mi != tmp:
                    s1 = src[i]
                    s2 = src[j]
    if s1 != None and s2 != None:
        src.remove(s1)
        src.remove(s2)
        mp += 10
#        print("synthesize", s1, s2, src, dst,mp)
        src.append(s1+s2)
    else:
        # 結合しても意味が無い場合、最も差が小さいものを変更
        mi = 1000
        ss = None
        dd = None
        for s in src:
            for d in dst:
                tmp = mi
                mi = min(mi, abs(s-d))
                if tmp != mi:
                    ss = s
                    dd = d
        if ss != None and dd != None:
            mp += abs(s-d)
            src.remove(s)
            dst.remove(d)
    return func(dst, src, mp)

def main():
    N,A,B,C = map(int, input().split())
    T = [A,B,C]
    L = []
    for _ in range(N):
        L.append(int(input()))
    print(func(T,L,0))

if __name__ == '__main__':
    main()


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

def main():
    N,A,B,C = map(int, input().split())
    L = []
    for _ in range(N):
        L.append(int(input()))
    mi = 1000*N
    for i in range(4**N):
        mp = 0
        a = b = c = 0
        x = 0
#        print(bin(i) + " ", end="")
        while i > 0:
            if i%4 == 1:
                if a > 0:
                    mp += 10
                a += L[x]
            if i%4 == 2:
                if b > 0:
                    mp += 10
                b += L[x]
            if i%4 == 3:
                if c > 0:
                    mp += 10
                c += L[x]
            i >>= 2
            x += 1
        if a > 0 and b > 0 and c > 0:
            mi = min(mi, mp + abs(A-a) + abs(B-b) + abs(C-c))
#            print(a,b,c,mi)
    print(mi)

if __name__ == '__main__':
    main()