Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def getMinX(i, y):
    B = [[]]
    for j, a in enumerate(A):
        if i == j:
            continue
        if a < y and len(B[-1]) > 0:
            B.append([])
        if a >= y:
            B[-1].append(a)

    C = []
    for grp in B:
        grp.sort()
        C += grp[:len(grp) - K + 1]
    C.sort()
    return C[Q - 2] if len(C) >= Q - 1 else 10**18

ans = 10**18
for i, y in enumerate(A):
    ans = min(ans, getMinX(i, y) - y)
print(ans)

Fixed submission:
N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

def sol(y):
    B = [[]]
    for a in A:
        if a < y and len(B[-1]) > 0:
            B.append([])
        if a >= y:
            B[-1].append(a)

    C = []
    for grp in B:
        grp.sort()
        C += grp[:max(0, len(grp) - K + 1)]
    C.sort()

    return C[Q - 1] - y if len(C) >= Q and C[0] == y else 10**18

ans = 10**18
for y in A:
    ans = min(ans, sol(y))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

def sol():
    N = int(input())
    P = [0] * N

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

    dp = [(0, 0)]

    for p in P:
        i = bisect_right(dp, (p, INF))
        if dp[i - 1][0] == p - 1:
            dp[i - 1] = (p, dp[i - 1][1] + 1)
        else:
            dp.insert(i, (p, 1))

    length = 1
    for _, l in dp:
        length = max(length, l)

    print(N - length)

sol()

Fixed submission:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

def sol():
    N = int(input())
    P = [0] * N

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

    pToIndex = {p : i for i, p in enumerate(P)}

    maxLeng = 1
    length = 1
    for n in range(1, N):
        if pToIndex[n] < pToIndex[n + 1]:
            length += 1
            maxLeng = max(maxLeng, length)
        else:
            length = 1

    print(N - maxLeng)

sol()
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
K = int(input())

subList = set([])

for front in range(0, len(S)):
    for length in range(1, len(S) + 1):
        subList.add(S[front: front + length])

subList = list(subList)
subList.sort()

print(subList[K - 1])


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

subList = set([])

for front in range(0, len(S)):
    for length in range(1, K + 1):
        subList.add(S[front: front + length])

subList = list(subList)
subList.sort()

print(subList[K - 1])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
K = int(input())

subList = set([])

for front in range(0, len(S)):
    for length in range(1, len(S) + 1):
        subList.add(S[front: front + length])

subList = list(subList)
subList.sort()

print(subList[K - 1])


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

subList = set([])

for front in range(0, len(S)):
    for length in range(1, K + 1):
        subList.add(S[front: front + length])

subList = list(subList)
subList.sort()

print(subList[K - 1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

cumA = [0] * (N + 1)

for i in range(N):
    cumA[i + 1] += cumA[i] + A[i]

cntA = Counter(cumA)

fact = [1] * (N + 2)
for i in range(1, N + 1):
    fact[i] *= fact[i - 1] * i

def ncr(n, r):
    if r > n or n < 0 or r < 0:
        return 0
    return fact[n] // fact[n - r] // fact[r]

ans = 0
for _, c in cntA.items():
    ans += ncr(c, 2)

print(ans)

Fixed submission:
from collections import Counter

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

cumA = [0] * (N + 1)

for i in range(N):
    cumA[i + 1] += cumA[i] + A[i]

cntA = Counter(cumA)

ans = sum([c * (c - 1) // 2 for c in cntA.values()])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

cumA = [0] * (N + 1)

for i in range(N):
    cumA[i + 1] += cumA[i] + A[i]

cntA = Counter(cumA)

fact = [1] * (N + 2)
for i in range(1, N + 1):
    fact[i] *= fact[i - 1] * i

def ncr(n, r):
    if r > n or n < 0 or r < 0:
        return 0
    return fact[n] // fact[n - r] // fact[r]

ans = 0
for _, c in cntA.items():
    ans += ncr(c, 2)

print(ans)

Fixed submission:
from collections import Counter

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

cumA = [0] * (N + 1)

for i in range(N):
    cumA[i + 1] += cumA[i] + A[i]

cntA = Counter(cumA)

ans = sum([c * (c - 1) // 2 for c in cntA.values()])

print(ans)
Buggy python submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify

N = int(input())

ans = []

que = [6, 10, 15]
heapify(que)

while N > 0:
    a = heappop(que)
    ans.append(a)
    N -= 1
    for p in [2, 3, 5]:
        if a % p != 0:
            heappush(que, a * p)

print(*ans)

Fixed submission:
N = int(input())

if N == 3:
    print('2 5 63')
else:
    A2 = [a for a in range(2, 30001, 2) if a % 6 != 0]
    A3 = [a for a in range(3, 30001, 6)]
    A6 = [a for a in range(6, 30001, 6)]

    a2 = min(10000, (N - 2) // 2 * 2)
    N -= a2
    a3 = min(5000, N // 2 * 2)
    a6 = N - a3

    ans = A2[: a2] + A3[: a3] + A6[: a6]
    print(*ans)
Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

H, W, D = map(int, input().split())

A = [list(map(int, input().split())) for _ in range(H)]
aToHW = {}
for h, row in enumerate(A):
    for w, a in enumerate(row):
        aToHW[a] = (h, w)

Q = int(input())

@lru_cache(maxsize=None)
def search(now):
    if not now + D in aToHW:
        return 0

    l, r = aToHW[now], aToHW[now + D]
    return abs(l[0] - r[0]) + abs(l[1] - r[1]) + search(now + D)

ans = []
for _ in range(Q):
    l, r = map(int, input().split())

    ans.append(search(l) - search(r))

print(*ans, sep='\n')


Fixed submission:
H, W, D = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]

aToHW = {}
for h in range(H):
    for w, a in enumerate(A[h]):
        aToHW[a] = (h, w)

cnt = [-1] * (H * W + 1)
for start in range(1, H * W + 1)[:: -1]:
    c = 0
    for now in range(start, H * W + 1, D):
        if now + D > H * W:
            break
        if cnt[now] >= 0:
            c += cnt[now]
            break

        nH, nW = aToHW[now]
        tH, tW = aToHW[now + D]
        c += abs(nH - tH) + abs(nW - tW)
    cnt[start] = c

Q = int(input())
ans = []
for _ in range(Q):
    L, R = map(int, input().split())
    ans.append(cnt[L] - cnt[R])

print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = map(int, input().split())

A = [-1] * (H * W + 1)

for i in range(1, H+1) :
    line = list(map(int, input().split()))
    for j in range(1, W + 1) :
        A[line[j - 1]] = (i, j)

memo = [[-1 for _ in range(H * W + 1)] for _ in range(H * W + 1)]

def cost(nowPoint, endPoint) :
    if nowPoint == endPoint :
        return 0
    if memo[nowPoint][endPoint] >= 0 :
        return memo[nowPoint][endPoint]

    nextPoint = nowPoint + D

    nowCost = abs(A[nextPoint][0] - A[nowPoint][0]) + abs(A[nextPoint][1] - A[nowPoint][1])
    nowCost += cost(nextPoint, endPoint)

    memo[nowPoint][endPoint] = nowCost
    return nowCost

Q = int(input())

for _ in range(Q) :
    start, end = map(int, input().split())
    print(cost(start, end))



Fixed submission:
H, W, D = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]

aToHW = {}
for h in range(H):
    for w, a in enumerate(A[h]):
        aToHW[a] = (h, w)

cnt = [-1] * (H * W + 1)
for start in range(1, H * W + 1)[:: -1]:
    c = 0
    for now in range(start, H * W + 1, D):
        if now + D > H * W:
            break
        if cnt[now] >= 0:
            c += cnt[now]
            break

        nH, nW = aToHW[now]
        tH, tW = aToHW[now + D]
        c += abs(nH - tH) + abs(nW - tW)
    cnt[start] = c

Q = int(input())
ans = []
for _ in range(Q):
    L, R = map(int, input().split())
    ans.append(cnt[L] - cnt[R])

print(*ans, sep='\n')
Buggy python submission (verdict: Runtime Error):
S = input()
X, Y = map(int, input().split())

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums) + 10
    g += bias
    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')


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

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums) + 10
    g += bias
    if g < 0:
        return False

    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
S = input()
X, Y = map(int, input().split())

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums)
    g += bias
    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')


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

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums) + 10
    g += bias
    if g < 0:
        return False

    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())

channels = [[] for _ in range(C)]
maxT = 0

for _ in range(N) :
    s, t, c = map(int, input().split())
    maxT = max(maxT, t)
    channels[c - 1].append((s, t))

maxT = maxT * 2 + 10
tvSum = [0 for _ in range(maxT)]

for tv in channels :
    imos = [0 for _ in range(maxT)]
    for (s, t) in tv :
        imos[s * 2 - 1] += 1
        imos[t * 2] -= 1
    for i in range(1, maxT) :
        imos[i] += imos[i - 1]
    for i in range(maxT) :
        if imos[i] > 0 :
            tvSum[i] += 1

ans = 0
for i in range(maxT) :
    ans = max(ans, tvSum[i])

print(ans)


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

channels = [[] for _ in range(C)]
maxT = 0

for _ in range(N) :
    s, t, c = map(int, input().split())
    maxT = max(maxT, t)
    channels[c - 1].append((s, t))

maxT = maxT * 2 + 10
tvSum = [0 for _ in range(maxT)]

for tv in channels :
    imos = [0 for _ in range(maxT)]
    for (s, t) in tv :
        imos[s * 2 - 1] += 1
        imos[t * 2] -= 1
    for i in range(1, maxT) :
        imos[i] += imos[i - 1]
    for i in range(maxT) :
        if imos[i] > 0 :
            tvSum[i] += 1

print(max(tvSum))
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N, K = map(int, input().split())
XY = [tuple(map(int, input().split())) for _ in range(N)]

ans = 10**20
for rect in combinations(XY, r=min(K, 4)):
    X = [x for x, _ in rect]
    Y = [y for _, y in rect]

    miX, mxX = min(X), max(X)
    miY, mxY = min(Y), max(Y)

    cnt = len([x for x, y in XY if miY <= y <= mxY and miX <= x <= mxX])
    if cnt >= K:
        ans = min(ans, (mxX - miX) * (mxY - miY))
print(ans)


Fixed submission:
N, K = map(int, input().split())
point = []
for _ in range(N) :
    x, y = map(int, input().split())
    point.append((x, y))

ans = float('inf')
for lx, _ in point :
    for rx, _ in point :
        if lx > rx :
            continue
        for _, ly in point :
            for _, ry in point :
                if ly > ry :
                    continue
                count = 0
                for x, y in point :
                    if lx <= x <= rx and ly <= y <= ry :
                        count += 1
                if count >= K :
                    ans = min(ans, (rx - lx) * (ry - ly))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
edges = [[] for _ in range(N)]

for _ in range(M):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

def isBipartite():
    sign = [-1] * N
    st = [(0, 0)]
    while st:
        now, p = st.pop()
        sign[now] = p

        for to in edges[now]:
            if sign[to] == -1:
                st.append((to, (p + 1) % 2))
                continue
            if sign[to] != (p + 1) % 2:
                return -1

    return sum(sign)

s = isBipartite()

if s == -1:
    print(N * (N - 1) // 2 - M)
else:
    print(s * (N - s) - M)


Fixed submission:
N, M = map(int, input().split())
edges = [[] for _ in range(N)]

for _ in range(M):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

def isBipartite():
    sign = [-1] * N
    st = [(0, 0)]
    while st:
        now, p = st.pop()

        if sign[now] != -1 and sign[now] != p:
            return -1
        if sign[now] == p:
            continue
        sign[now] = p

        for to in edges[now]:
            st.append((to, (p + 1) % 2))

    return sum(sign)

s = isBipartite()

if s == -1:
    print(N * (N - 1) // 2 - M)
else:
    print(s * (N - s) - M)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [[] for _ in range(N)]

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

for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j] > A[i][k] + A[k][j]:  # 最短でない
                print('-1')
                exit()

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if k == i or k == j:
                continue
            if A[i][j] == A[i][k] + A[k][j]:  # 代替可能
                break
        else:
            ans += A[i][j]

print(ans)


Fixed submission:
N = int(input())
A = [[] for _ in range(N)]

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

for k in range(N):
    for i in range(N):
        for j in range(N):
            if A[i][j] > A[i][k] + A[k][j]:  # 最短でない
                print('-1')
                exit()

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if k == i or k == j:
                continue
            if A[i][j] == A[i][k] + A[k][j]:  # 代替可能
                break
        else:
            ans += A[i][j]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

N, M, R = map(int, input().split())
R = list(map(lambda a: int(a) - 1, input().split()))

minDist = [[10**18] * N for _ in range(N)]
for _ in range(M):
    fr, to, cost = map(int, input().split())
    fr -= 1
    to -= 1
    minDist[fr][to] = cost
    minDist[to][fr] = cost
for i in range(N):
    minDist[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = minDist[i][k] + minDist[k][j]
            if minDist[i][j] > d:
                minDist[i][j] = d

ans = 10**18
for P in permutations(R, r=len(R)):
    cost = 0
    for fr, to in zip(P, P[1:]):
        cost += minDist[fr][to]
    ans = min(ans, cost)
print(ans)


Fixed submission:
from itertools import permutations

N, M, R = map(int, input().split())
R = list(map(lambda a: int(a) - 1, input().split()))

minDist = [[10**18] * N for _ in range(N)]
for _ in range(M):
    fr, to, cost = map(int, input().split())
    fr -= 1
    to -= 1
    minDist[fr][to] = cost
    minDist[to][fr] = cost
for i in range(N):
    minDist[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = minDist[i][k] + minDist[k][j]
            if minDist[i][j] > d:
                minDist[i][j] = d

ans = 10**18
for P in permutations(R, r=len(R)):
    cost = 0
    for fr, to in zip(P, P[1:]):
        cost += minDist[fr][to]
    ans = min(ans, cost)
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
P = list(map(int, input().split()))

ans = 0
for i in range(N):
    if P[i] == i + 1:
        P[i], P[i + 1] = P[i + 1], P[i]
        ans += 1
print(ans)


Fixed submission:
N = int(input())
P = list(map(lambda a: int(a) - 1, input().split()))

ans = 0
for i in range(N):
    p = P[i]
    if p == i:
        if i < N - 1:
            P[i], P[i + 1] = P[i + 1], P[i]
        ans += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
P = list(map(lambda a: int(a) - 1, input().split()))

ans = 0
for i in range(N):
    p = P[i]
    if p == i and i < N:
        P[i], P[i + 1] = P[i + 1], P[i]
        ans += 1

print(ans)

Fixed submission:
N = int(input())
P = list(map(lambda a: int(a) - 1, input().split()))

ans = 0
for i in range(N):
    p = P[i]
    if p == i:
        if i < N - 1:
            P[i], P[i + 1] = P[i + 1], P[i]
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = Counter(map(int, input().split()))

L = sum([[a for _ in range(min(2, c // 2))] for a, c in cnt.items()], [])
L.sort()

ans = 0 if len(L) < 2 else L[-1] * L[-2]
print(ans)


Fixed submission:
from collections import Counter

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

cntA = Counter(A)
B = []
for a, c in cntA.items():
    if c // 2 == 0:
        continue
    for _ in range(c // 2):
        B.append(a)

B.sort(reverse=True)
ans = B[0] * B[1] if len(B) >= 2 else 0

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = Counter(map(int, input().split()))

L = sum([[a for _ in range(c // 2)] for a, c in cnt.items()], [])
L.sort()

ans = 0 if len(L) < 2 else L[-1] * L[-2]
print(ans)


Fixed submission:
from collections import Counter

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

cntA = Counter(A)
B = []
for a, c in cntA.items():
    if c // 2 == 0:
        continue
    for _ in range(c // 2):
        B.append(a)

B.sort(reverse=True)
ans = B[0] * B[1] if len(B) >= 2 else 0

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = Counter(map(int, input().split()))

L = sum([[a] * (c // 2) for a, c in cnt.items()], [])
L.sort()

ans = 0 if len(L) < 2 else L[-1] * L[-2]
print(ans)


Fixed submission:
from collections import Counter

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

cntA = Counter(A)
B = []
for a, c in cntA.items():
    if c // 2 == 0:
        continue
    for _ in range(c // 2):
        B.append(a)

B.sort(reverse=True)
ans = B[0] * B[1] if len(B) >= 2 else 0

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
cnt = Counter(map(int, input().split()))

L = sum([[a] * (c // 2) for a, c in cnt.items()], [])
L.sort()

ans = 0 if len(L) < 2 else L[-1] * L[-2]
print(ans)


Fixed submission:
from collections import Counter

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

cntA = Counter(A)
B = []
for a, c in cntA.items():
    if c // 2 == 0:
        continue
    for _ in range(c // 2):
        B.append(a)

B.sort(reverse=True)
ans = B[0] * B[1] if len(B) >= 2 else 0

print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print('No')
    exit()

if len(A.keys()) == 1:
    p = list(A.keys())[0]
    print('Yes' if 2 * p <= N or p == N - 1 else 'No')
    exit()

p1, p2 = sorted(A.keys())
1 / 0
print('Yes' if [p1, p2] == [1, 2] and A[1] == 1 else 'No')


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

minColor = min(A)
maxColor = max(A)

if minColor == maxColor:
    if minColor == N - 1:  # 各色の猫が1匹ずつ
        print('Yes')
    elif minColor * 2 <= N:  # 各色の猫が2匹以上
        print('Yes')
    else:
        print('No')
    exit()

if minColor + 1 != maxColor:
    print('No')
    exit()

minCnt = 0
maxCnt = 0

for a in A:
    if a == minColor:
        minCnt += 1
    elif a == maxColor:
        maxCnt += 1
    else:
        print('No')
        exit()

# minCnt : 1匹だけの色
# maxCnt : 2匹以上の色
# 全体でmaxColor個の色

if (maxColor - minCnt) * 2 <= maxCnt and maxColor >= minCnt + 1:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print('No')
    exit()

if len(A.keys()) == 1:
    print('Yes' if list(A.keys())[0] == N - 1 or list(A.keys())[0] == 2 else 'No')
    exit()

p1, p2 = A.keys()
1 / 0
print('Yes' if abs(p1 - p2) == 1 and max(p1, p2) <= N - 1 and A[min(p1, p2)] == 1 else 'No')


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

minColor = min(A)
maxColor = max(A)

if minColor == maxColor:
    if minColor == N - 1:  # 各色の猫が1匹ずつ
        print('Yes')
    elif minColor * 2 <= N:  # 各色の猫が2匹以上
        print('Yes')
    else:
        print('No')
    exit()

if minColor + 1 != maxColor:
    print('No')
    exit()

minCnt = 0
maxCnt = 0

for a in A:
    if a == minColor:
        minCnt += 1
    elif a == maxColor:
        maxCnt += 1
    else:
        print('No')
        exit()

# minCnt : 1匹だけの色
# maxCnt : 2匹以上の色
# 全体でmaxColor個の色

if (maxColor - minCnt) * 2 <= maxCnt and maxColor >= minCnt + 1:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = list(input())

ans = []
while S and S[-1] == '(':
    ans.append(')')
    S.pop()
ans = '(' * len(ans) + ''.join(ans)

cnt = 0
for s in S[::-1]:
    if s == ')':
        cnt += 1
    else:
        cnt -= 1

if cnt > 0:
    ans = '(' * cnt + ''.join(S) + ans
if cnt < 0:
    ans = S + ''.join(S) + ')' * cnt + ans

print(ans)


Fixed submission:
N = int(input())
S = input()

l = S.count('(')
r = S.count(')')
d = r - l

if d > 0:
    S = '(' * d + S
if d < 0:
    S = S + ')' * (-d)

l, r, d = 0, 0, 0
for s in S:
    if s == '(':
        l += 1
    if s == ')':
        r += 1
    d = max(d, r - l)

if d > 0:
    S = '(' * d + S + ')' * d

print(S)