Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict
from itertools import accumulate

input = sys.stdin.readline

N, K = map(int, input().split())
sushi = defaultdict(list)
for i in range(N):
    t, d = map(int, input().split())
    sushi[t].append(d)

X, Y = [], []
for v in sushi.values():
    v = sorted(v, reverse=True)
    X.append(v[0])
    if len(v) > 1:
        Y += v[1:]
X = sorted(X, reverse=True)

X = [0] + [x for x in accumulate(X)]
Y = [0] + [x for x in accumulate(Y)]

ans = 0
for i in range(1, K + 1):
    j = K - i
    if j < len(Y):
        ans = max(ans, X[i] + Y[j] + i * i)
print(ans)

Fixed submission:
import sys
from collections import defaultdict
from itertools import accumulate

input = sys.stdin.readline

N, K = map(int, input().split())
sushi = defaultdict(list)
for i in range(N):
    t, d = map(int, input().split())
    sushi[t].append(d)

X, Y = [], []
for v in sushi.values():
    v = sorted(v, reverse=True)
    X.append(v[0])
    if len(v) > 1:
        Y += v[1:]
X = sorted(X, reverse=True)
Y = sorted(Y, reverse=True)

X = [0] + [x for x in accumulate(X)]
Y = [0] + [x for x in accumulate(Y)]

ans = 0
for i in range(1, min(len(X), K + 1)):
    j = K - i
    if j < len(Y):
        ans = max(ans, X[i] + Y[j] + i * i)
print(ans)
Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())


def f(n):
    # レベルnの総数
    if n == 0:
        return 1
    else:
        return 2 * f(n - 1) + 3


def g(n):
    # レベルnのpの数
    if n == 0:
        return 1
    else:
        return 2 * g(n - 1) + 1


def dfs(n, x):
    # 総数
    m = f(n)
    # 真ん中
    a = (m + 1) // 2

    if x == a:
        return g(n - 1) + 1
    elif x == 1:
        return 0
    elif x < a:
        return dfs(n - 1, x - 1)
    elif x >= a:
        return g(n - 1) + 1 + dfs(n - 1, x - a)
    elif x == m:
        return 2 * g(n - 1) + 1


ans = dfs(N, X)
print(ans)


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


def f(n):
    # レベルnの総数
    if n == 0:
        return 1
    else:
        return 2 * f(n - 1) + 3


def g(n):
    # レベルnのpの数
    if n == 0:
        return 1
    else:
        return 2 * g(n - 1) + 1


def dfs(n, x):
    # 総数
    m = f(n)
    # 真ん中
    a = (m + 1) // 2

    if n == 0:
        return 1
    elif x == 1:
        return 0
    elif x < a:
        return dfs(n - 1, x - 1)
    elif x == a:
        return g(n - 1) + 1
    elif x >= a:
        return g(n - 1) + 1 + dfs(n - 1, x - a)
    elif x == m:
        return 2 * g(n - 1) + 1


ans = dfs(N, X)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())


def factorize(n):
    if n == 1:
        return [1]

    i, factors = 2, []
    while i * i <= n:
        while n % i == 0:
            n //= i
            factors.append(i)
        i += 1
    if n > 1:
        factors.append(n)
    return factors


factors = factorize(M)
n_factors = len(factors)

ans = 0
for i in range(1 << n_factors):
    x = 1
    for j in range(n_factors):
        if i & (1 << j):
            x *= factors[j]
    y = M // x
    if N <= y:
        ans = max(ans, x)
print(ans)


Fixed submission:
from collections import Counter

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


def factorize(n):
    if n == 1:
        return [1]

    i, factors = 2, []
    while i * i <= n:
        while n % i == 0:
            n //= i
            factors.append(i)
        i += 1
    if n > 1:
        factors.append(n)
    return factors


c = Counter(factorize(M))
a = [1]
for k, v in c.items():
    tmp = []
    for x in a:
        for i in range(v + 1):
            tmp.append(x * (k**i))
    a = list(set(a) | set(tmp))

ans = 0
for x in a:
    y = M // x
    if N <= y:
        ans = max(ans, x)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())


def factorize(n):
    if n == 1:
        return [1]

    i, factors = 2, []
    while i * i <= n:
        while n % i == 0:
            n //= i
            factors.append(i)
        i += 1
    if n > 1:
        factors.append(n)
    return factors


factors = factorize(M)
n_factors = len(factors)

ans = 0
for i in range(1 << n_factors):
    x = 1
    for j in range(n_factors):
        if i & (1 << j):
            x *= factors[j]
    y = M // x
    if N <= y:
        ans = max(ans, x)
print(ans)


Fixed submission:
from collections import Counter

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


def factorize(n):
    if n == 1:
        return [1]

    i, factors = 2, []
    while i * i <= n:
        while n % i == 0:
            n //= i
            factors.append(i)
        i += 1
    if n > 1:
        factors.append(n)
    return factors


c = Counter(factorize(M))
a = [1]
for k, v in c.items():
    tmp = []
    for x in a:
        for i in range(v + 1):
            tmp.append(x * (k**i))
    a = list(set(a) | set(tmp))

ans = 0
for x in a:
    y = M // x
    if N <= y:
        ans = max(ans, x)
print(ans)
Buggy python submission (verdict: Runtime Error):
from itertools import permutations

N, C = map(int, input().split())
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]

n_color = [[0 for _ in range(3)] for _ in range(C)]
for i in range(N):
    for j in range(N):
        X = c[i][j] - 1
        n_color[(i + j + 2) % 3][X] += 1

ans = float('inf')
for paint in permutations(range(C), 3):
    cost = 0
    for ij3 in range(3):
        Y = paint[ij3]
        for X in range(C):
            cost += D[X][Y] * n_color[ij3][X]
    ans = min(ans, cost)
print(ans)

Fixed submission:
from itertools import permutations

N, C = map(int, input().split())
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]

n_color = [[0 for _ in range(C)] for _ in range(3)]
for i in range(N):
    for j in range(N):
        X = c[i][j] - 1
        n_color[(i + j + 2) % 3][X] += 1

ans = float('inf')
for paint in permutations(range(C), 3):
    cost = 0
    for ij3 in range(3):
        Y = paint[ij3]
        for X in range(C):
            cost += D[X][Y] * n_color[ij3][X]
    ans = min(ans, cost)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

ans = float('inf')
for i in range(n):
    c = s[:i].count('W') + s[i + 1:].count('E')
    ans = min(ans, c)
print(ans)

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

cw = [0] * (n + 1)
for i in range(1, n + 1):
    if s[i - 1] == 'W':
        cw[i] = cw[i - 1] + 1
    else:
        cw[i] = cw[i - 1]

s2 = s[::-1]
ce = [0] * (n + 1)
for i in range(1, n + 1):
    if s2[i - 1] == 'E':
        ce[i] = ce[i - 1] + 1
    else:
        ce[i] = ce[i - 1]
ce = ce[::-1]

ans = float('inf')
for i in range(n):
    ans = min(ans, cw[i] + ce[i + 1])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

ans = n
for i in range(n):
    x = a[i]
    sum_a = a[i]
    for j in range(i + 1, n):
        x ^= a[j]
        sum_a += a[j]
        if sum_a == x:
            ans += 1
        else:
            break
print(ans)

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

ans = 0
sum_a = 0
r = 0
for l in range(n):
    while r < n:
        x = a[r]
        if sum_a & x != 0:
            break
        sum_a += x
        r += 1
    ans += r - l
    sum_a -= a[l]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())

substrings = []
for i in range(len(s)+1):
    for j in range(len(s)-i+1):
        # print('i={}, i+j={}, s[i:i+j]={}'.format(i, i+j, s[i:i+j]))
        if s[i:i+j] != '':
            substrings.append(s[i:i+j])
substrings = sorted(set(substrings))
print(substrings[K-1])

Fixed submission:
s = input()
K = int(input())

substrings = []
for i in range(len(s)):
    for j in range(len(s)-i+1):
        # print('i={}, i+j={}, s[i:i+j]={}'.format(i, i+j, s[i:i+j]))
        if j <= K and s[i:i+j] != '':
            substrings.append(s[i:i+j])
substrings = sorted(set(substrings))
print(substrings[K-1])
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
from scipy.misc import comb

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

ans = [0, 0]
max_comb = 0
combs = {}
for a_i, a_j in itertools.combinations(a, 2):
    if a_j > a_i:
        tmp = a_j
        a_j = a_i
        a_i = tmp

    if (a_i, a_i - a_j) in combs:
        continue

    c = comb(a_i, a_j, exact=True)
    combs[(a_i, a_j)] = c
    if c > max_comb:
        max_comb = c
        ans[0], ans[1] = a_i, a_j

print(ans[0], ans[1])

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

a_i = max(a)
a_j = 0
min_d = float('inf')
for a_k in a:
    if a_k == a_i:
        continue
    d = abs(a_i / 2 - a_k)
    if d < min_d:
        min_d = d
        a_j = a_k
print(a_i, a_j)

Buggy python submission (verdict: Runtime Error):
import itertools
from scipy.special import comb

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

ans = [0, 0]
max_comb = 0
combs = {}
for a_i, a_j in itertools.combinations(a, 2):
    if a_j > a_i:
        tmp = a_j
        a_j = a_i
        a_i = tmp

    if (a_i, a_i - a_j) in combs:
        continue

    c = comb(a_i, a_j, exact=True)
    combs[(a_i, a_j)] = c
    # print(a_i, a_j, c)
    if c > max_comb:
        max_comb = c
        ans[0], ans[1] = a_i, a_j

print(ans[0], ans[1])


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

a_i = max(a)
a_j = 0
min_d = float('inf')
for a_k in a:
    if a_k == a_i:
        continue
    d = abs(a_i / 2 - a_k)
    if d < min_d:
        min_d = d
        a_j = a_k
print(a_i, a_j)

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

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

ans = [0, 0]
max_comb = 0
for a_i, a_j in itertools.combinations(a, 2):
    if a_j > a_i:
        tmp = a_j
        a_j = a_i
        a_i = tmp

    comb = len(list(itertools.combinations(range(a_i), a_j)))
    if comb > max_comb:
        max_comb = comb
        ans[0], ans[1] = a_i, a_j

print(ans[0], ans[1])

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

a_i = max(a)
a_j = 0
min_d = float('inf')
for a_k in a:
    if a_k == a_i:
        continue
    d = abs(a_i / 2 - a_k)
    if d < min_d:
        min_d = d
        a_j = a_k
print(a_i, a_j)
Buggy python submission (verdict: Runtime Error):
a, b, k = map(int, input().split())

z = list(range(a, b + 1))
ans = sorted(list(set(z[:k]) | set(z[-k:])))
for x in ans:
    print(x)


Fixed submission:
a, b, k = map(int, input().split())

for i in range(a, min(a + k, b - k + 1)):
    print(i)
for i in range(max(a, b - k + 1), b + 1):
    print(i)

Buggy python submission (verdict: Runtime Error):
a, b, k = map(int, input().split())

z = list(range(a, b + 1))
ans = sorted(list(set(z[:k]) | set(z[-k:])))
print(ans)


Fixed submission:
a, b, k = map(int, input().split())

for i in range(a, min(a + k, b - k + 1)):
    print(i)
for i in range(max(a, b - k + 1), b + 1):
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

for i in range(n):
    ans = 0
    now = 0
    for p in a[:i] + a[i + 1:] + [0]:
        ans += abs(p - now)
        now = p
    print(ans)

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

now = 0
d = []
for p in a + [0]:
    d.append(abs(p - now))
    now = p

a = [0] + a + [0]
sum_d = sum(d)
for i in range(n):
    ans = sum_d - (d[i] + d[i+1]) + abs(a[i] - a[i+2])
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
xy = {}
for i in range(h):
    a = list(map(int, input().split()))
    for j in range(w):
        xy[a[j]] = (i + 1, j + 1)

q = int(input())
for _ in range(q):
    ans = 0
    l, r = map(int, input().split())

    m = list(range(l, r + 1, d))
    for i in range(len(m) - 1):
        x1, y1 = xy[m[i]]
        x2, y2 = xy[m[i + 1]]
        ans += abs(x1 - x2) + abs(y1 - y2)
    print(ans)

Fixed submission:
H, W, D = map(int, input().split())
xy = {}
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        xy[A[j]] = (i + 1, j + 1)

d = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
    x1, y1 = xy[i]
    x2, y2 = xy[i - D]
    d[i] = d[i - D] + abs(x1 - x2) + abs(y1 - y2)

Q = int(input())
for _ in range(Q):
    L, R = map(int, input().split())
    ans = d[R] - d[L]
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

li = 0
print(li, flush=True)
ls = input()
if ls == 'Vacant':
    exit()

ri = N - 1
print(ri, flush=True)
rs = input()
if rs == 'Vacant':
    exit()

for _ in range(18):
    mid = (li + ri) // 2

    print(mid, flush=True)
    s = input()

    if s == 'Vacant':
        exit()

    c1 = (mid - li - 1) % 2 == 1 and ls == s
    c2 = (mid - li - 1) % 2 == 0 and ls != s
    if c1 or c2
        ri = mid
        rs = s
    else:
        li = mid
        ls = s


Fixed submission:
N = int(input())

li = 0
print(li, flush=True)
ls = input()
if ls == 'Vacant':
    exit()

ri = N - 1
print(ri, flush=True)
rs = input()
if rs == 'Vacant':
    exit()

for _ in range(18):
    mid = (li + ri) // 2

    print(mid, flush=True)
    s = input()

    if s == 'Vacant':
        exit()

    # [li, mid]に空席がある
    c1 = (mid - li) % 2 == 1 and ls == s
    c2 = (mid - li) % 2 == 0 and ls != s
    if c1 or c2:
        ri = mid
        rs = s
    else:
        li = mid
        ls = s
Buggy python submission (verdict: Runtime Error):
import sys

input = sys.stdin.readline

N, M = map(int, input().split())
L, R, D = [None] * M, [None] * M, [None] * M
for i in range(M):
    L[i], R[i], D[i] = map(int, input().split())

x = {}
if M > 0:
    x[L[0]] = 0
ans = 'Yes'
for l, r, d in zip(L, R, D):
    if l in x and r in x:
        if x[r] - x[l] != d:
            ans = 'No'
            break
    elif l in x:
        x[r] = x[l] + d
    elif r in x:
        x[l] = x[r] - d
    else:
        raise Exception
print(ans, x)


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


class WeightedUnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.diff_weight = [0 for i in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            root = self.find(self.par[x])
            self.diff_weight[x] += self.diff_weight[self.par[x]]
            self.par[x] = root
            return root

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y, w):
        w += self.weight(x)
        w -= self.weight(y)
        x = self.find(x)
        y = self.find(y)
        if x != y:
            self.par[y] = x
            self.diff_weight[y] = w

    def weight(self, x):
        self.find(x)
        return self.diff_weight[x]

    def diff(self, x, y):
        return self.weight(y) - self.weight(x)


uf = WeightedUnionFind(N)
ans = 'Yes'
for _ in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    if uf.is_same(L, R):
        if uf.diff(L, R) != D:
            ans = 'No'
            break
    else:
        uf.union(L, R, D)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
C, S, F = [0] * (N - 1), [0] * (N - 1), [0] * (N - 1)
for i in range(N - 1):
    C[i], S[i], F[i] = map(int, input().split())


def get_t(x, i):
    t = S[i]
    while x > t:
        t += F[i]
    return t


for i in range(N - 1):
    x = S[i] + C[i]
    for j in range(i + 1, N - 1):
        t = get_t(x, j)
        x = t + C[j]
    print(x)
print(0)


Fixed submission:
N = int(input())
C, S, F = [0] * (N - 1), [0] * (N - 1), [0] * (N - 1)
for i in range(N - 1):
    C[i], S[i], F[i] = map(int, input().split())

for i in range(N):
    t = 0
    for j in range(i, N - 1):
        if t < S[j]:
            t = S[j]
        elif t % F[j] == 0:
            pass
        else:
            t += F[j] - t % F[j]
        t += C[j]
    print(t)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()

N = len(S)
if N % 2 == 0:
    M = N // 2
    ans = M
    for i in range(M):
        T = S[i:N - i]
        if len(set(T)) == 1:
            ans = N - i
            break
else:
    M = N // 2 + 1
    ans = M
    for i in range(M):
        T = S[i:N - i]
        if len(set(T)) == 1:
            ans = N - i
            break
print(ans)


Fixed submission:
S = list(input())

k = len(S) // 2 + 1
x = S[k - 1]
for i in range(k, len(S) + 1):
    if x != S[i - 1] or x != S[len(S) - i]:
        i -= 1
        break
print(i)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

N, C = map(int, input().split())
stc = [[int(x) for x in input().split()] for _ in range(N)]

T = 10 ** 5 + 1
A = [[0] * T for _ in range(C)]
for s, t, c in stc:
    A[c - 1][s] += 1
    A[c - 1][t] -= 1

for i in range(C):
    for j in range(1, T):
        A[i][j] += A[i][j - 1]
        if A[i][j] == 1 and A[i][j - 1] == 0:
            A[i][j - 1] = 1

ans = 0
for j in range(1, T):
    cnt = 0
    for i in range(C):
        cnt += A[i][j]
    ans = max(ans, cnt)
print(ans)


Fixed submission:
N, C = map(int, input().split())
tv = [list(map(int, input().split())) for _ in range(N)]

max_t = max(tv, key=lambda x: x[1])[1]
channels = [[0 for _ in range(max_t)] for _ in range(C)]

for s, t, c in tv:
    channels[c - 1][s - 1:t] = [1] * (t - s + 1)

ans = 0
for T in list(zip(*channels)):
    ans = max(ans, sum(T))
print(ans)
Buggy python submission (verdict: Runtime Error):
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]

memo = [[None for _ in range(2)] for _ in range(2010)]


def f(i, p, x, y):
    """
    i番目にx, yが手持ちのとき、プレイヤーpが引いたときのスコア
    """
    if i == N:
        return abs(x - y)
    if memo[i][p]:
        return memo[i][p]

    if p == 0:
        score = float('-inf')
        for j in range(i, N):
            score = max(score, f(j + 1, 1, A[j], y))
    else:
        score = float('inf')
        for j in range(i, N):
            score = min(score, f(j + 1, 0, x, A[j]))

    memo[i][p] = score
    return score


print(f(0, 0, Z, W))


Fixed submission:
X, Y = input().split()

if X < Y:
    print('<')
elif X > Y:
    print('>')
else:
    print('=')
Buggy python submission (verdict: Runtime Error):
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]

memo = [[None for _ in range(2)] for _ in range(2010)]


def f(i, p, x, y):
    """
    i番目にx, yが手持ちのとき、プレイヤーpが引いたときのスコア
    """
    if i == N:
        return abs(x - y)
    if memo[i][p]:
        return memo[i][p]

    if p == 0:
        score = float('-inf')
        for j in range(i, N):
            score = max(score, f(j + 1, 1, A[j], y))
    else:
        score = float('inf')
        for j in range(i, N):
            score = min(score, f(j + 1, 0, x, A[j]))

    memo[i][p] = score
    return score


print(f(0, 0, Z, W))


Fixed submission:
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]

if N == 1:
    print(abs(A[0] - W))
else:
    print(max(abs(A[N - 2] - A[N - 1]), abs(A[N - 1] - W)))

Buggy python submission (verdict: Runtime Error):
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]

memo = [[None for _ in range(2)] for _ in range(N + 1)]


def f(i, p, x, y):
    """
    i番目にx, yが手持ちのとき、プレイヤーpが引いたときのスコア
    """
    if i == N:
        return abs(x - y)
    if memo[i][p]:
        return memo[i][p]

    if p == 0:
        score = float('-inf')
        for j in range(i, N):
            score = max(score, f(j + 1, 1, A[j], y))
    else:
        score = float('inf')
        for j in range(i, N):
            score = min(score, f(j + 1, 0, x, A[j]))

    memo[i][p] = score
    return score


print(f(0, 0, Z, W))

Fixed submission:
N, Z, W = map(int, input().split())
A = [int(x) for x in input().split()]

if N == 1:
    print(abs(A[0] - W))
else:
    print(max(abs(A[N - 2] - A[N - 1]), abs(A[N - 1] - W)))
Buggy python submission (verdict: Runtime Error):
S2 = input()
T = input()

N = len(S2)

index = -1
q_num = 0
q_index = -1
tmp = 0
for i in range(N):
    if S2[i] == '?':
        tmp += 1
        q_num = max(q_num, tmp)
        if len(T) <= q_num:
            q_index = i - q_num + 1
    else:
        tmp = 0
    if S2[i] == T[0] and i + len(T) <= N:
        index = i

if len(T) > N:
    S = list('UNRESTORABLE')
elif len(T) <= q_num:
    S = list(S2)
    S[q_index:q_index + len(T)] = list(T)
    for i in range(N):
        if S[i] == '?':
            S[i] = 'a'
elif index == -1:
    S = list('UNRESTORABLE')
else:
    S = [None] * N
    for i in range(len(S)):
        if i > index:
            if S2[i] == T[i - index]:
                S[i] = S2[i]
            elif S2[i] == '?':
                S[i] = T[i - index]
            else:
                S = list('UNRESTORABLE')
        else:
            if S2[i] == '?':
                S[i] = 'a'
            else:
                S[i] = S2[i]
print(''.join(S))


Fixed submission:
S2 = input()
T = input()

N = len(S2)


def check(S, T):
    for i in range(len(S)):
        if S[i] == '?' or S[i] == T[i]:
            continue
        else:
            return False
    return True


S = 'UNRESTORABLE'
if len(T) <= N:
    C = []
    for i in range(N - len(T) + 1):
        j = i + len(T)
        if (S2[i] == T[0] or S2[i] == '?') and check(S2[i:j], T):
            S = list(S2)
            S[i:j] = list(T)
            S = ''.join(S).replace('?', 'a')
            C.append(S)
    if len(C) > 0:
        S = sorted(C)[0]
print(S)

Buggy python submission (verdict: Runtime Error):
S2 = input()
T = input()

index = S2.find(T[0])
if index == -1:
    S = list('UNRESTORABLE')
elif len(S2) - index < len(T):
    S = list('UNRESTORABLE')
else:
    S = [None] * len(S2)
    for i in range(len(S)):
        if i > index:
            if S2[i] == T[i - index]:
                S[i] = S2[i]
            elif S2[i] == '?':
                S[i] = T[i - index]
            else:
                S = list('UNRESTORABLE')
        else:
            if S2[i] == '?':
                S[i] = 'a'
            else:
                S[i] = S2[i]
print(''.join(S))


Fixed submission:
S2 = input()
T = input()

N = len(S2)


def check(S, T):
    for i in range(len(S)):
        if S[i] == '?' or S[i] == T[i]:
            continue
        else:
            return False
    return True


S = 'UNRESTORABLE'
if len(T) <= N:
    C = []
    for i in range(N - len(T) + 1):
        j = i + len(T)
        if (S2[i] == T[0] or S2[i] == '?') and check(S2[i:j], T):
            S = list(S2)
            S[i:j] = list(T)
            S = ''.join(S).replace('?', 'a')
            C.append(S)
    if len(C) > 0:
        S = sorted(C)[0]
print(S)