Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def binary_search(a, f, target, left, right):
    ans = a[-1] * f[-1]
    while left + 1 < right:
        mid = (left+right)//2
        cost = 0
        for aa,ff in zip(a,f):
            if ff > mid:
                cost += aa
            elif aa - mid // ff > 0:
                cost += aa - mid // ff
        if cost > target:
            left = mid
        else:
            right = mid
    return right

n,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
f = sorted(map(int, input().split()))
target = binary_search(a,f,k,-1,max(a) * max(f))
mx = 0
cost = 0
for aa, ff in zip(a,f):
    if ff > target:
        cost += aa
        aa = 0
    elif aa - target // ff > 0:
        cost += aa - target // ff
        aa = (target // ff)
    if mx < aa * ff:
        mx = aa * ff
print(mx)


Fixed submission:
def binary_search(a, f, target, left, right):
    ans = a[-1] * f[-1]
    while left + 1 < right:
        mid = (left+right)//2
        cost = 0
        for aa,ff in zip(a,f):
            if ff > mid:
                cost += aa
            elif aa - mid // ff > 0:
                cost += aa - mid // ff
        if cost > target:
            left = mid
        else:
            right = mid
    return right

n,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
f = sorted(map(int, input().split()))
target = binary_search(a,f,k,-1,max(a) * max(f))
mx = 0
cost = 0
for aa, ff in zip(a,f):
    if ff > target:
        cost += aa
        aa = 0
    elif aa - target // ff > 0:
        cost += aa - target // ff
        aa = (target // ff)
    if mx < aa * ff:
        mx = aa * ff
print(mx)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush

n,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
f = sorted(map(int, input().split()))

heap = []
for aa,ff in zip(a,f):
    heappush(heap, [-aa*ff, aa, ff])

for _ in range(k):
    cost, aa, ff = heappop(heap)
    if aa == 0:
        break
    aa -= 1 
    heappush(heap, [-aa*ff, aa, ff])
print(-min(heap)[0])


Fixed submission:
def binary_search(a, f, target, left, right):
    ans = a[-1] * f[-1]
    while left + 1 < right:
        mid = (left+right)//2
        cost = 0
        for aa,ff in zip(a,f):
            if ff > mid:
                cost += aa
            elif aa - mid // ff > 0:
                cost += aa - mid // ff
        if cost > target:
            left = mid
        else:
            right = mid
    return right

n,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
f = sorted(map(int, input().split()))
target = binary_search(a,f,k,-1,max(a) * max(f))
mx = 0
cost = 0
for aa, ff in zip(a,f):
    if ff > target:
        cost += aa
        aa = 0
    elif aa - target // ff > 0:
        cost += aa - target // ff
        aa = (target // ff)
    if mx < aa * ff:
        mx = aa * ff
print(mx)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
l = sorted(map(int, input().split()))

ans = 0
for i in range(2,n)[::-1]:
    for j in range(1,i)[::-1]:
        a = bisect.bisect_left(l, l[i] + l[j] - 1, 0, j-1)
        b = bisect.bisect_right(l, l[i] - l[j], 0, j-1)
        #print(l[i] + l[j], l[i] , l[j], a, l[i] - l[j], l[i] - l[j] < l[a], a, b, l[:j])
        if l[i] - l[j] < l[a]:
            ans += a - b + 1
print(ans)


Fixed submission:
import bisect

n = int(input())
l = sorted(map(int, input().split()))

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        ans += bisect.bisect_left(l,l[j]+l[i])-j-1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
l = sorted(map(int, input().split()))
ll = [l[:i] for i in range(n)]

ans = 0
for i in range(2,n)[::-1]:
    for j in range(1,i)[::-1]:
        a = bisect.bisect(ll[j], l[i] + l[j])
        b = bisect.bisect(ll[j], l[i] - l[j])
#         print(l[i] + l[j], l[i] , l[j], a, ll[j], l[i] - l[j], l[i] - l[j] < ll[i][a])
        if l[i] - l[j] < ll[i][a]:
            ans += a -b
print(ans)


Fixed submission:
import bisect

n = int(input())
l = sorted(map(int, input().split()))

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        ans += bisect.bisect_left(l,l[j]+l[i])-j-1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,m,l = map(int, input().split())

inf = float('INF')
road = [[inf for i in range(n)] for j in range(n)]
for i in range(n):
    road[i][i] = 0
for i in range(m):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    road[a][b] = road[b][a] = c

def warshall(d, n):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    d[i][j] = d[i][k] + d[k][j]

warshall(road, n)
# [print(cost) for cost in road]
# print()
costs = [[1 if road[i][j] <= l and i != j else inf for j in range(n)] for i in range(n)]
for i in range(n):
    costs[i][i] = 0
warshall(costs, n)

# [print(cost) for cost in costs]
q = int(input())
for i in range(q):
    s,t = map(int, input().split())
    
    print(costs[s-1][t-1]-1 if costs[s-1][t-1] < inf else -1)


Fixed submission:
from scipy.sparse.csgraph import dijkstra as di

inf = float('INF')
n,m,l = map(int,input().split())

route = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int, input().split())
    route[a-1][b-1] = route[b-1][a-1] = c

p_route = di(route)
p_route[p_route == 0] = inf
p_route[l >= p_route] = 1
p_route[l < p_route] = 0
p_route = di(p_route)

# print(p_route)
query = []
q = int(input())
ans = []
for _ in range(q):
    s,t = map(int, input().split())
    print(-1 if p_route[s-1][t-1] == inf else int(p_route[s-1][t-1]) - 1)

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra as di

inf = float('INF')


n,m,l = map(int,input().split())

route = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int, input().split())
    route[a-1][b-1] = route[b-1][a-1] = c

p_route = di(route)
for i in range(n):
    for j in range(n):
        if p_route[i][j] == inf:
            continue
        elif p_route[i][j] <= l:
            p_route[i][j] = 1
p_route = di(p_route)

query = []
q = int(input())
ans = []
for _ in range(q):
    s,t = map(int, input().split())
    print(-1 if p_route[s-1][t-1] == inf else p_route[s-1][t-1])


Fixed submission:
from scipy.sparse.csgraph import dijkstra as di

inf = float('INF')
n,m,l = map(int,input().split())

route = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(m):
    a,b,c = map(int, input().split())
    route[a-1][b-1] = route[b-1][a-1] = c

p_route = di(route)
p_route[p_route == 0] = inf
p_route[l >= p_route] = 1
p_route[l < p_route] = 0
p_route = di(p_route)

# print(p_route)
query = []
q = int(input())
ans = []
for _ in range(q):
    s,t = map(int, input().split())
    print(-1 if p_route[s-1][t-1] == inf else int(p_route[s-1][t-1]) - 1)
Buggy python submission (verdict: Runtime Error):
s = input()
mod = 10**9+7

init = [0 for i in range(13)]
if s[0] == '?':
  dp = [1] * 10 + [0] * 3
else:
  dp = [0 for i in range(13)]
  dp[int(s[0])] = 1

for c in s[1:]:
  d = init.copy()
  if c == '?':
    for i in range(13):
      for j in range(10):
        idx = (i * 10 + j) % 13
        d[idx] = (dp[i] + d[idx]) % mod 
  else :
    for i in range(13):
      idx = (i * 10 + int(c)) % 13
      d[idx] = (dp[i] + d[idx]) % mod
  dp = d.copy()
print(dp[5])


Fixed submission:
def main():
    M=10**9+7
    r=tuple(tuple((j-k)*9%13for j in range(10))for k in range(13))
    d=[1]+[0]*12
    for c in input():
        if c > '9':
            d = [sum(d[j] for j in k)%M for k in r] 
        else:
            d = [d[(int(c)-k)*9%13] for k in range(13)]
    print(d[5])
main()

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
mod = 10**9+7

init = [0 for i in range(13)]
if s[0] == '?':
  dp = [1] * 10 + [0] * 3
else:
  dp = [0 for i in range(13)]
  dp[int(s[0])] = 1

for c in s[1:]:
  d = init.copy()
  if c == '?':
    for i in range(13):
      for j in range(10):
        idx = (i * 10 + j) % 13
        d[idx] = (dp[i] + d[idx]) % mod 
  else :
    for i in range(13):
      idx = (i * 10 + int(c)) % 13
      d[idx] = (dp[i] + d[idx]) % mod
  dp = d.copy()
print(dp[5])


Fixed submission:
def main():
    M=10**9+7
    r=tuple(tuple((j-k)*9%13for j in range(10))for k in range(13))
    d=[1]+[0]*12
    for c in input():
        if c > '9':
            d = [sum(d[j] for j in k)%M for k in r] 
        else:
            d = [d[(int(c)-k)*9%13] for k in range(13)]
    print(d[5])
main()

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
mod = 10**9+7

dp = [[0 for i in range(13)] for j in range(N+1)]
dp[0][0] = 1

for i in range(N):
    c = -1 if S[i] == '?' else int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k*10 + j) % 13] += dp[i][k]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[N][5])

Fixed submission:
def main():
    M=10**9+7
    r=tuple(tuple((j-k)*9%13for j in range(10))for k in range(13))
    d=[1]+[0]*12
    for c in input():
        if c > '9':
            d = [sum(d[j] for j in k)%M for k in r] 
        else:
            d = [d[(int(c)-k)*9%13] for k in range(13)]
    print(d[5])
main()
Buggy python submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
A = [int(input()) for i in range(N)]

c = Counter(A)

mx = max(A)
B = list(set(A))
B.remove(mx)
bmx = max(B)
for i in range(N):
    if A[i] != mx:
        print(mx)
    elif c[mx] == 1:
        print(bmx)
    else:
        print(mx)

Fixed submission:
from collections import Counter

N = int(input())
A = [int(input()) for i in range(N)]

c = Counter(A)

mx = max(A)
B = list(set(A))
B.remove(mx)
for i in range(N):
    if A[i] != mx:
        print(mx)
    elif c[mx] == 1:
        print(max(B))
    else:
        print(mx)
Buggy python submission (verdict: Runtime Error):
import math
N,K = map(int, input().split())
M = (N-1)*(N-2)//2
if K > M:
    print(-1)
    exit()

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > n:
        s+=1
        d = s+1

Fixed submission:
import math
N,K = map(int, input().split())
M = (N-1)*(N-2)//2
if K > M:
    print(-1)
    exit()

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > N:
        s+=1
        d = s+1

Buggy python submission (verdict: Runtime Error):
import math
N,K = map(int, input().split())
if N == 2 and K != 0:
    print(-1)
    exit()
if K > math.factorial(N-1) // (math.factorial(N - 1 - 2) * math.factorial(2)):
    print(-1)
    exit()

for i in range(2,N+1):
    print(1,i)

for i in range(3,K+3):
    print(2,i)

Fixed submission:
import math
N,K = map(int, input().split())
M = (N-1)*(N-2)//2
if K > M:
    print(-1)
    exit()

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > N:
        s+=1
        d = s+1

Buggy python submission (verdict: Runtime Error):
import math
N,K = map(int, input().split())
if K > math.factorial(N-1) // (math.factorial(N - 1 - 2) * math.factorial(2)):
    print(-1)
    exit()

for i in range(2,N+1):
    print(1,i)

for i in range(3,K+3):
    print(2,i)

Fixed submission:
import math
N,K = map(int, input().split())
M = (N-1)*(N-2)//2
if K > M:
    print(-1)
    exit()

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > N:
        s+=1
        d = s+1

Buggy python submission (verdict: Runtime Error):
import math
N,K = map(int, input().split())
if K > math.factorial(n-1) // (math.factorial(N - 1 - 2) * math.factorial(2)):
    print(-1)
    exit()

for i in range(2,N+1):
    print(1,i)

for i in range(3,K+3):
    print(2,i)

Fixed submission:
import math
N,K = map(int, input().split())
M = (N-1)*(N-2)//2
if K > M:
    print(-1)
    exit()

print(N-1+M-K)
for i in range(2,N+1):
    print(1,i)
s = 2
d = 3
for i in range(M-K):
    print(s,d)
    d+=1
    if d > N:
        s+=1
        d = s+1
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

A.sort()
grid = {}
for i in range(N):
    for j in range(i+1,N):
        if (A[j][0] - A[i][0], A[j][1] - A[i][1]) not in grid:
            grid[A[j][0] - A[i][0], A[j][1] - A[i][1]] = 1
        else:
            grid[A[j][0] - A[i][0], A[j][1] - A[i][1]] += 1

print(N - max(grid.values()))

Fixed submission:
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

if N == 1:
    print(1)
    exit()
A.sort()
grid = {}
for i in range(N):
    for j in range(i+1,N):
        if (A[j][0] - A[i][0], A[j][1] - A[i][1]) not in grid:
            grid[A[j][0] - A[i][0], A[j][1] - A[i][1]] = 1
        else:
            grid[A[j][0] - A[i][0], A[j][1] - A[i][1]] += 1

print(N - max(grid.values()))
Buggy python submission (verdict: Runtime Error):
from collections import Counter
n = int(input())
w = list(map(int, input().split()))
mx = sum(w)
w = Counter(w)
ans = mx
tmp = 0
for k in sorted(w).keys():
  tmp += w[k] * k
  ans = min(ans, abs(mx - tmp * 2))
print(ans)

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

mn = sum(W)
for i in range(N):
  tmp = abs(sum(W[:i])-sum(W[i:]))
  if mn > tmp:
    mn = tmp
print(mn)
Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys

H, W = list(map(int, sys.stdin.readline().split()))
S = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)]) == '.'
ups = np.zeros((H, W), dtype=int)
downs = np.zeros((H, W), dtype=int)
rights = np.zeros((H, W), dtype=int)
lefts = np.zeros((H, W), dtype=int)

for h in range(1, H):
    ups[h] = (ups[h - 1] + 1) * S[h - 1]
    downs[H - h] = (downs[H - h + 1] + 1) * S[H - h + 1]
for w in range(1, W):
    lefts[:, w] = (lefts[:, w - 1] + 1) * S[:, w - 1]
    rights[:, W - w] = (rights[:, W - w + 1] + 1) * S[:, W - w + 1]
print(((ups + downs + lefts + rights) * S).max() + 1)


Fixed submission:
import numpy as np
import sys

H, W = list(map(int, sys.stdin.readline().split()))
S = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)]) == '.'
ups = np.zeros((H, W), dtype=int)
downs = np.zeros((H, W), dtype=int)
rights = np.zeros((H, W), dtype=int)
lefts = np.zeros((H, W), dtype=int)

for h in range(1, H):
    ups[h] = (ups[h - 1] + 1) * S[h - 1]
    downs[H - h - 1] = (downs[H - h] + 1) * S[H - h]
for w in range(1, W):
    lefts[:, w] = (lefts[:, w - 1] + 1) * S[:, w - 1]
    rights[:, W - w - 1] = (rights[:, W - w] + 1) * S[:, W - w]
print(((ups + downs + lefts + rights) * S).max() + 1)
Buggy python submission (verdict: Runtime Error):
N, M =map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = []

cards = [0 for i in range(10+1)]
for i in range(M):
    b,c = map(int, input().split())
    BC.append([b,c])
BC.sort(key=lambda x:x[1], reverse=True)

sum_val = 0
a_idx = 0
for i in range(M):
    num, val = BC[i]
    while num > 0 and A[a_idx] < val:
        sum_val += val
        num -= 1
        a_idx += 1
    if A[a_idx] > val:
        break
print(sum_val + sum(A[a_idx:]))
# print(a_idx, A, BC)


Fixed submission:
N, M =map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = []

cards = [0 for i in range(10+1)]
for i in range(M):
    b,c = map(int, input().split())
    BC.append([b,c])
BC.sort(key=lambda x:x[1], reverse=True)

sum_val = 0
a_idx = 0
for i in range(M):
    num, val = BC[i]
    while num > 0 and a_idx < N and A[a_idx] < val:
        sum_val += val
        num -= 1
        a_idx += 1
    if a_idx < N and A[a_idx] > val:
        break
print(sum_val + sum(A[a_idx:]))
# print(a_idx, A, BC)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
print(S[:K]+S[K].lower()+S[K+1:])

Fixed submission:
N,K = map(int, input().split())
S = input()
print(S[:K-1]+S[K-1].lower()+S[K:])
Buggy python submission (verdict: Runtime Error):
S = int(input())
if int(S[:2]) > 12 and int(S[2:]) <= 12:
  print('YYMM')
elif int(S[:2]) <= 12 and int(S[2:]) > 12 and int(S[2:]) != 0:
  print('MMYY')
elif int(S[:2]) <= 12 and int(S[2:]) <= 12 and int(S[:2]) != 0  and int(S[2:]) != 0:
  print('AMBIGUOUS')
else:
  print('NA')

Fixed submission:
S = input()
a,b = int(S[:2]), int(S[2:])
if (a > 12 or a == 0 )and b <= 12 and b > 0:
  print('YYMM')
elif a <= 12 and (b > 12 or b == 0) and a > 0:
  print('MMYY')
elif a <= 12 and b <= 12 and a > 0  and b > 0:
  print('AMBIGUOUS')
else:
  print('NA')
Buggy python submission (verdict: Runtime Error):
a = list(map(int, input().split()))
a.append(a[0]-1)
a.append(a[1]-1)
a.sort()
pirnt(a[-1]+ a[-2])



Fixed submission:
A, B = map(int , input().split())
maxi = 0
if A < B:
    maxi = B
    B -= 1
else :
    maxi = A
    A -= 1
if A < B:
    maxi += B
else :
    maxi += A
print(maxi)
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = [i if i in 'AC' else '-' for i in input()]
for i in range(Q):
  l, r = map(int, input().split())
  a = ''.join([S[i] if S[i] == 'A' and S[i+1] == 'C' else '-' for i in range(l-1, r-1)])
  a = [i for i in a.split('-') if len(i) > 0]
  print(len(a))



Fixed submission:
N, Q = map(int, input().split())
S = input()
S = [1 if i > 0 and S[i-1] == 'A' and S[i] == 'C' else 0 for i in range(N)]
T = [0 for _ in range(N)]
for i in range(N):
    T[i] = S[i]
    if i > 0:
        T[i] += T[i-1]
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):
A,B = map(int, input().split())
tmp = B ^ B
for i in range(A,B+1)[::-1]:
    tmp = tmp ^ i
print(tmp)

Fixed submission:
a,b = map(int, input().split())
get = lambda x:[x,1,x+1,0][x%4]
print(get(a-1)^get(b))

Buggy python submission (verdict: Time Limit Exceeded):
A,B = map(int, input().split())
tmp = B ^ B
for i in range(A,B+1)[::-1]:
    tmp = tmp ^ i
print(tmp)

Fixed submission:
a,b = map(int, input().split())
get = lambda x:[x,1,x+1,0][x%4]
print(get(a-1)^get(b))
Buggy python submission (verdict: Runtime Error):
A,B,K = map(int,input().split())
cnt = 0
for i in range(1, min(A,B)):
  if A % i == 0 and B % i == 0:
    cnt += 1
  if cnt == K:
    break
print(i)

Fixed submission:
A,B,K = map(int,input().split())
x = []
for i in range(1, min(A,B)+1):
  if A % i == 0 and B % i == 0:
    x.append(i)
print(x[-K])
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
A = [0]+list(map(int,input().split()))
inf = float('INF')
costs = [inf for i in range(N+1)]
costs[0] = costs[1] = 0
for i in range(1,N+1):
    for j in range(i+1,i+K+1):
        if j > N:
            break
        costs[j] = min(costs[i]+abs(A[i]-A[j]), costs[j])
print(costs[N])


Fixed submission:
N,K = map(int, input().split())
A = [0]+list(map(int,input().split()))
inf = float('INF')
costs = [inf for i in range(N+1)]
costs[0] = costs[1] = 0
for i in range(1,N+1):
    for j in range(i+1,i+K+1):
        if j > N:
            break
        costs[j] = min(costs[i]+abs(A[i]-A[j]), costs[j])
print(costs[N])
Buggy python submission (verdict: Time Limit Exceeded):
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
bag = [0 for _ in range(W+1)]
for w,v in a:
  for i in range(min(w,W+1),W+1)[::-1]:
    bag[i] = max(bag[i], bag[i-w] + v)
print(bag[W])  

Fixed submission:
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
bag = [0 for _ in range(W+1)]
for w,v in a:
  for i in range(min(w,W+1),W+1)[::-1]:
    bag[i] = max(bag[i], bag[i-w] + v)
print(bag[W])

Buggy python submission (verdict: Time Limit Exceeded):
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
bag = [0 for _ in range(W+1)]
for w,v in a:
  for i in range(w,W+1)[::-1]:
    bag[i] = max(bag[i], bag[i-w] + v)
print(bag[W])  

Fixed submission:
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
bag = [0 for _ in range(W+1)]
for w,v in a:
  for i in range(min(w,W+1),W+1)[::-1]:
    bag[i] = max(bag[i], bag[i-w] + v)
print(bag[W])
Buggy python submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc114/submissions/6262572

from itertools import product
N = input()
n = int(N)
ans = 0
for i in range(len(N)+1):
    for s in product(["3","5","7"],repeat=i):
        if int("".join(s)) <= n and "3" in s and "5" in s and "7" in s:
            ans += 1
print(ans)


Fixed submission:
from collections import deque
N = int(input())
memo = {i:1 for i in [357, 375, 537, 573, 735, 753]}
ans = 0
queue = deque()
mod = [10**i for i in range(10)]
for key in memo.keys():
  if key <= N:
    ans += 1
    queue.append(key)
while queue:
    key = queue.popleft()
    len_key = len(str(key))
    for i in range(len_key):
        for j in [3,5,7]:
            tmp = (key // mod[i]) * mod[i+1] + j * mod[i] + key % mod[i]
            if tmp not in memo and tmp <= N:
                ans += 1
                memo[tmp] = 1
                queue.append(tmp)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = 0
for i in range(1, N+1, 2):
    c = Counter(str(i))
    if len(c) == 3 and '7' in c and '5' in c and '3' in c:
        # print(c)
        cnt += 1
print(cnt)


Fixed submission:
from collections import deque
N = int(input())
memo = {i:1 for i in [357, 375, 537, 573, 735, 753]}
ans = 0
queue = deque()
mod = [10**i for i in range(10)]
for key in memo.keys():
  if key <= N:
    ans += 1
    queue.append(key)
while queue:
    key = queue.popleft()
    len_key = len(str(key))
    for i in range(len_key):
        for j in [3,5,7]:
            tmp = (key // mod[i]) * mod[i+1] + j * mod[i] + key % mod[i]
            if tmp not in memo and tmp <= N:
                ans += 1
                memo[tmp] = 1
                queue.append(tmp)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = 0
for i in range(N+1):
    c = Counter(str(i))
    if len(c) == 3 and '7' in c and '5' in c and '3' in c:
        # print(c)
        cnt += 1
print(cnt)


Fixed submission:
from collections import deque
N = int(input())
memo = {i:1 for i in [357, 375, 537, 573, 735, 753]}
ans = 0
queue = deque()
mod = [10**i for i in range(10)]
for key in memo.keys():
  if key <= N:
    ans += 1
    queue.append(key)
while queue:
    key = queue.popleft()
    len_key = len(str(key))
    for i in range(len_key):
        for j in [3,5,7]:
            tmp = (key // mod[i]) * mod[i+1] + j * mod[i] + key % mod[i]
            if tmp not in memo and tmp <= N:
                ans += 1
                memo[tmp] = 1
                queue.append(tmp)
print(ans)