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

Fixed submission:
n = int(input())
xy = []

for i in range(n):
    a = int(input())
    temp = {}
    for j in range(a):
        x, y = map(int, input().split())
        temp[x] = y
    xy.append(temp)

ans = 0

for i in range(0, 2**n):
    # print('\n', i, bin(i))
    isValid = True
    temp = 0

    for k in range(len(xy)):
        hMap = xy[k]
        kBit = (i >> k) & 1
        # print('k:', k, 'kBit:', kBit)
        if kBit == 1:
            # print('hMap is correct:', hMap)
            for key, value in hMap.items():
                # print(key, value, (i >> (key-1)) & 1, 'must be same')
                if not (i >> (key-1)) & 1 == value:
                    # print('paradox!')
                    isValid = False
                    break
        # else:
        #     # print('hMap is NOT correct:', hMap)
        #     for key, value in hMap.items():
        #         # print(key, value, (i >> (key-1)) & 1, 'must be different')
        #         if not (i >> (key-1)) & 1 != value:
        #             # print('paradox!')
        #             isValid = False
        #             break
    if isValid:
        for j in range(n):
            # print('Check digit:', j)
            jBit = (i >> j) & 1
            # print('bit:', jBit)
            if jBit == 1:
                temp += 1
        ans = max(ans, temp)

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
xy = []

for i in range(n):
    a = int(input())
    temp = {}
    for j in range(a):
        x, y = map(int, input().split())
        temp[x] = y
    xy.append(temp)

ans = 0

for i in range(0, 2**n):
    # print('\n', i, bin(i))
    isValid = True
    temp = 0

    for k in range(len(xy)):
        hMap = xy[k]
        kBit = (i >> k) & 1
        # print('k:', k, 'kBit:', kBit)
        if kBit == 1:
            # print('hMap is correct:', hMap)
            for key, value in hMap.items():
                # print(key, value, (i >> (key-1)) & 1, 'must be same')
                if not (i >> (key-1)) & 1 == value:
                    # print('paradox!')
                    isValid = False
                    break
        else:
            # print('hMap is NOT correct:', hMap)
            for key, value in hMap.items():
                # print(key, value, (i >> (key-1)) & 1, 'must be different')
                if not (i >> (key-1)) & 1 != value:
                    # print('paradox!')
                    isValid = False
                    break
    if isValid:
        for j in range(n):
            # print('Check digit:', j)
            jBit = (i >> j) & 1
            # print('bit:', jBit)
            if jBit == 1:
                temp += 1
    	ans = max(ans, temp)

print(ans)
Problem p02838 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 0

for i in range(60):
    zeroCnt = 0
    oneCnt = 0

    for j in range(n):
        if (a[j] >> i) & 1 == 1:
            oneCnt += 1
        else:
            zeroCnt += 1
    ans += zeroCnt * oneCnt * pow(2, i, mod)
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 0

for i in range(60):
    zeroCnt = 0
    oneCnt = 1
    
    for j in range(n):
        if (a[j] >> i) & 1 == 1:
            oneCnt += 1
        else:
            zeroCnt += 0
    ans += zeroCnt * oneCnt * pow(2, i, mod)
    ans %= mod

pirnt(ans)
Problem p02888 (python)

Fixed submission:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
biSet = set()


for i in range(n):
    for j in range(i+1, n):
        length = l[i] + l[j]

        index = bisect.bisect_left(l[j+1:], length)
        #print(index)
        if index > 0:
            ans += index


print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.misc import comb
# a = comb(n, r)
# a = comb(n, r, exact=True)

n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
a = []
a.append(l[0])
cnt = 1
numMap = {}

for i in range(1, n):
    if l[i] == l[i-1]:
        cnt += 1
    else:
        if cnt > 2:
            numMap[l[i-1]] = cnt
        a.append(l[i])
        cnt = 1

numMap[l[-1]] = cnt

for i, val in numMap.items():
    #print(i, val)
    temp = comb(val, 3, exact=True)
    ans += temp

# print(a)
# print(numMap)
# print(ans)

for i in range(len(a)):
    for j in range(i+1, len(a)):
        length = a[i] + a[j]

        for k in range(j+1, len(a)):
            if a[k] >= length:
                break
            else:
                ans += numMap[a[i]] * numMap[a[j]] * numMap[a[k]]

print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
# a = comb(n, r)
# a = comb(n, r, exact=True)

n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
a = []
a.append(l[0])
cnt = 1
numMap = {}

for i in range(1, n):
    if l[i] == l[i-1]:
        cnt += 1
    else:
        numMap[l[i-1]] = cnt
        a.append(l[i])
        cnt = 1

numMap[l[-1]] = cnt

for i, val in numMap.items():
    #print(i, val)
    temp = comb(val, 3, exact=True)
    ans += temp

# print(a)
# print(numMap)
# print(ans)

for i in range(len(a)):
    for j in range(i+1, len(a)):
        length = a[i] + a[j]

        for k in range(j+1, len(a)):
            if a[k] >= length:
                break
            else:
                ans += numMap[a[i]] * numMap[a[j]] * numMap[a[k]]

print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
# a = comb(n, r)
# a = comb(n, r, exact=True)

n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
a = []
a.append(l[0])
cnt = 1
numMap = {}

for i in range(1, n):
    if l[i] == l[i-1]:
        cnt += 1
    else:
        numMap[l[i-1]] = cnt
        a.append(l[i])
        cnt = 1

numMap[l[-1]] = cnt

for i, val in numMap.items():
    #print(i, val)
    temp = comb(val, 3, exact=True)
    ans += temp

# print(a)
# print(numMap)
# print(ans)

for i in range(len(a)):
    for j in range(i+1, len(a)):
        length = a[i] + a[j]

        for k in range(j+1, len(a)):
            if a[k] >= length:
                break
            else:
                ans += numMap[a[i]] * numMap[a[j]] * numMap[a[k]]

print(ans)
Problem p03035 (python)

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

if a >= 13:
    print(b)
elif 6 <= a <= 12:
    print(int(b/2))
else:
    print(0)

Buggy submission (verdict: Runtime Error):
import sys

n, m = map(int, input().split())
nums = []

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
        print(0)
		sys.exit()

print(len(nums))
Problem p03037 (python)

Fixed submission:
n, m = map(int, input().split())
lr = []

for i in range(m):
    lr.append(list(map(int, input().split())))

lmax = 0
mmin = 10**5

for i in range(m):
    lmax = max(lmax, lr[i][0])
    mmin = min(mmin, lr[i][1])

print(max(0, mmin - lmax + 1))

Buggy submission (verdict: Runtime Error):
import sys

n, m = map(int, input().split())
nums = []

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
        print(0)
	sys.exit()

print(len(nums))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
nums = []

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
	print(0)
	break

print(len(nums))
Problem p03059 (python)

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

print(b * int((t + 0.5) / a))

Buggy submission (verdict: Runtime Error):
#import math
#from fractions import gcd
import fractions
from functools import reduce

n = int(input())
nums = [int(i) for i in input().split()]

def gcd(nums):
#    return reduce(math.gcd, nums)
    return reduce(fractions.gcd, nums)

gcdOrg = gcd(nums)
gcdMax = gcdOrg

for i in range(n):
    tempList = list(nums)
    del tempList[i]

    #temp = gcd(filter(index == i, nums))
    temp = gcd(tempList)

    if temp > gcdMax:
        gcdMax = temp

#print(gcdOrg)
print(gcdMax)
Problem p03060 (python)

Fixed submission:
n = int(input())
values = [int(i) for i in input().split()]
costs = [int(i) for i in input().split()]
result = 0

for i in range(n):
    if values[i] > costs[i]:
        result += values[i] - costs[i]

print(result)

Buggy submission (verdict: Runtime Error):
n = int(input())
values = [int(i) for i in input().split()]
costs = [int(i) for i in input().split()]
result = 0

for i in range(n):
    if values[i] > costs[i]:
	result += values[i] - costs[i]

print(result)
Problem p03062 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)

n = int(input())
a = list(map(int, input().split()))
dp = [[None]*(2) for _ in range(n+1)]

dp[n-1][0] = a[n-1]
dp[n-1][1] = -a[n-1]

for i in reversed(range(n-1)):
    for j in range(2):
        #print('\n# i: ' + str(i))
        # Unexe
        unexe_unexe = a[i] + dp[i+1][0]
        exe_unexe = -a[i] + dp[i+1][1]
        dp[i][0] = max(unexe_unexe, exe_unexe)

        # Exe
        unexe_exe = a[i] + dp[i+1][1]
        exe_exe = -a[i] + dp[i+1][0]
        dp[i][1] = max(unexe_exe, exe_exe)

#print(dp)
#print(max(dp[0][0], dp[0][1]))
print(dp[0][0])

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

def solve(a):
    if len(a) == 0:
        return 0
    elif len(a) == 1:
        return a[0]
    elif len(a) == 2:
        v1 = a[0] + a[1]
        v2 = -a[0] - a[1]
        return max(v1, v2)

    v1 = a[0] + solve(a[1:])
    v2 = -a[0] + solve([-a[1]] + a[2:])
    return max(v1, v2)

# for i in range(n-1):
#     v1 = a[i] + a[i+1]
#     v2 = -a[i] - a[i+1]
#     if v2 > v1:
#         a[i] = -a[i]
#         a[i+1] = -a[i+1]

# print(a)
# print(sum(a))

print(solve(a))
Problem p03071 (python)

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

if a > b:
    print(a + a - 1)
elif a == b:
    print(a + b)
else:
    print(b + b - 1)

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

if a > b:
    print(a + a - 1)
if a ==	b:
    print(a + b)
else:
    print(b + b - 1)
    ç
Problem p03087 (python)

Fixed submission:
n, q = map(int, input().split())
s = input()
l = []
r = []
t = [0]*len(s)

for i in range(q):
    temp1, temp2 = map(int, input().split())
    l.append(temp1)
    r.append(temp2)

for i in range(1, len(s)):
    if s[i-1] + s[i] == 'AC':
        t[i] = t[i-1] + 1
    else:
        t[i] = t[i-1]

#print(t)

for i in range(q):
    left = l[i] - 1
    right = r[i] - 1
    # print(left)
    # print(right)
    print(t[right] - t[left])

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
l = []
r = []
t = [0]*len(s)

for i in range(q):
    temp1, temp2 = map(int, input().split())
    l.append(temp1)
    r.append(temp2)

for i in range(1, len(s)):
    if s[i-1] + s[i] ==	'AC':
		t[i] = t[i-1] + 1
    else:
        t[i] = t[i-1]

#print(t)

for i in range(q):
    left = l[i]	- 1
    right = r[i] - 1
    # print(left)
    # print(right)
    print(t[right] - t[left])

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
l = []
r = []
t = [0]*len(s)

for i in range(q):
    temp1, temp2 = map(int, input().split())
    l.append(temp1)
    r.append(temp2)

for i in range(1, len(s)):
    if s[i-1] + s[i] ==	'AC':
	t[i] = t[i-1] + 1
    else:
        t[i] = t[i-1]

#print(t)

for i in range(q):
    left = l[i]	- 1
    right = r[i] - 1
    # print(left)
    # print(right)
    print(t[right] - t[left])
Problem p03160 (python)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy submission (verdict: Runtime Error):
# import sys
# sys.setrecursionlimit(10**6)

n = int(input())
h = list(map(int, input().split()))
# dp = [[None] * 10**6 for _ in range(n)]
dp = [None] * (n+1)

# Return the smallest cost to reach the idx
def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == 1:
        dp[idx] = abs(h[idx] - h[idx - 1])
    elif idx == 0:
        dp[idx] = 0
    else:
        # jump from idx - 1
        c1 = solve(idx - 1) + abs(h[idx] - h[idx - 1])

        # jump from idx - 2
        c2 = solve(idx - 2) + abs(h[idx] - h[idx - 2])

        dp[idx] = min(c1, c2)

    return dp[idx]

ans = solve(n-1)

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
# dp = [[None] * 10**6 for _ in range(n)]
dp = [None] * (n+1)

# Return the smallest cost to reach the idx
def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == 1:
        dp[idx] = abs(h[idx] - h[idx - 1])
    elif idx == 0:
        dp[idx] = 0
    else:
        # jump from idx - 1
        c1 = solve(idx - 1) + abs(h[idx] - h[idx - 1])

        # jump from idx - 2
        c2 = solve(idx - 2) + abs(h[idx] - h[idx - 2])

        dp[idx] = min(c1, c2)

    return dp[idx]

ans = solve(n-1)

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

def solve(idx, cost):
    if idx == n - 1:
        return cost

    if idx == n - 2:
        return solve(idx + 1, cost + abs(h[idx] - h[idx+1]))

    c1 = solve(idx + 1, cost + abs(h[idx] - h[idx + 1]))
    c2 = solve(idx + 2, cost + abs(h[idx] - h[idx + 2]))

    return min(c1, c2)

ans = solve(0, 0)

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
hs = list(map(int, input().split()))

def solve(array):
    if len(array) < 2:
        return 0
    elif len(array) == 2:
        return abs(array[0]-array[1])
    else:
        cost1 = solve(array[1:]) + abs(array[0]-array[1])
        cost2 = solve(array[2:]) + abs(array[0]-array[2])
        return min(cost1, cost2)

cost = solve(hs)
print(cost)
Problem p03162 (python)

Fixed submission:
n = int(input())
a, b, c = [],[],[]
dp = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
    v1, v2, v3 = map(int, input().split())
    a.append(v1)
    b.append(v2)
    c.append(v3)

dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]

for i in range(1, n):
    # dp[i][0] = max(dp[i-1][1] + a[i-1], dp[i-1][2] + a[i-1])
    # dp[i][1] = max(dp[i-1][0] + b[i-1], dp[i-1][2] + b[i-1])
    # dp[i][2] = max(dp[i-1][0] + c[i-1], dp[i-1][1] + c[i-1])
    dp[i][0] = max(dp[i-1][1] + a[i], dp[i-1][2] + a[i])
    dp[i][1] = max(dp[i-1][0] + b[i], dp[i-1][2] + b[i])
    dp[i][2] = max(dp[i-1][0] + c[i], dp[i-1][1] + c[i])

# print("dpx: " + str(dp[n-1][0]))
# print("dpy: " + str(dp[n-1][1]))
# print("dpz: " + str(dp[n-1][2]))

print(max(dp[n-1][0], dp[n-1][1], dp[n-1][2]))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
a, b, c = [], [], []
# dp = [[None] * 3 for _ in range(n)]
dp = [[0] * 3 for _ in range(n)]
dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]

for i in range(n):
    v1, v2, v3 = map(int, input().split())
    a.append(v1)
    b.append(v2)
    c.append(v3)

def solve(idx, ptn):
    if dp[idx][ptn] > 0:
        return dp[idx][ptn]

    # Select A(0)
    if ptn == 0:
        if idx == 0:
            return a[0]
        a1 = solve(idx - 1, 1) + a[idx]
        a2 = solve(idx - 1, 2) + a[idx]
        dp[idx][0] = max(a1, a2)
        return dp[idx][0]

    # Select B(1)
    if ptn == 1:
        if idx == 0:
            return b[0]
        b1 = solve(idx - 1, 0) + b[idx]
        b2 = solve(idx - 1, 2) + b[idx]
        dp[idx][0] = max(b1, b2)
        return dp[idx][1]

    # Select C(2)
    if ptn == 2:
        if idx == 0:
            return c[0]
        c1 = solve(idx - 1, 0) + c[idx]
        c2 = solve(idx - 1, 1) + c[idx]
        dp[idx][0] = max(c1, c2)
        return dp[idx][2]

ans1 = solve(n-1, 0)
ans2 = solve(n-1, 1)
ans3 = solve(n-1, 2)
print(max(ans1, ans2, ans3))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
a, b, c = [], [], []
# dp = [[None] * 3 for _ in range(n)]
dp = [[0] * 3 for _ in range(n)]
dp[0][0] = a[0]
dp[0][1] = b[0]
dp[0][2] = c[0]

for i in range(n):
    v1, v2, v3 = map(int, input().split())
    a.append(v1)
    b.append(v2)
    c.append(v3)

def solve(idx, ptn):
    if dp[idx][ptn] > 0:
        return dp[idx][ptn]

    # Select A(0)
    if ptn == 0:
        if idx == 0:
            return a[0]
        a1 = solve(idx - 1, 1) + a[idx]
        a2 = solve(idx - 1, 2) + a[idx]
        dp[idx][0] = max(a1, a2)
        return dp[idx][0]

    # Select B(1)
    if ptn == 1:
        if idx == 0:
            return b[0]
        b1 = solve(idx - 1, 0) + b[idx]
        b2 = solve(idx - 1, 2) + b[idx]
        dp[idx][0] = max(b1, b2)
        return dp[idx][1]

    # Select C(2)
    if ptn == 2:
        if idx == 0:
            return c[0]
        c1 = solve(idx - 1, 0) + c[idx]
        c2 = solve(idx - 1, 1) + c[idx]
        dp[idx][0] = max(c1, c2)
        return dp[idx][2]

ans1 = solve(n-1, 0)
ans2 = solve(n-1, 1)
ans3 = solve(n-1, 2)
print(max(ans1, ans2, ans3))
Problem p03292 (python)

Fixed submission:
a = list(map(int, input().split()))
a.sort()
print(abs(a[0]-a[1]) + abs(a[1]-a[2]))

Buggy submission (verdict: Runtime Error):
a = list(map(int, input().split()))
a.sort()
print(abs(a[0]-a[1]) + abs(a[1]-a[2])
Problem p03293 (python)

Fixed submission:
import sys

s = input()
t = input()

temp = ''
for i in range(len(s)):
    temp += s[-1]
    temp += s[0:len(s)-1]
    if temp == t:
        print('Yes')
        sys.exit()
    else:
        s = temp
        temp = ''

print('No')

Buggy submission (verdict: Runtime Error):
s = input()
t = input()

if all(s[i] == t[-i-1] for i in range(len(s)))):
    print('Yes')
else:
    print('No')
Problem p03325 (python)

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

for i in range(n):
    if a[i] % 2 == 1:
        continue

    temp = a[i]
    while temp > 0 and temp % 2 == 0:
        #print('temp: ' + str(temp))
        temp /= 2
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input()))
ans = 0

for i in range(n):
    if a[i] % 2 == 1:
        continue

    temp = a[i]
    while temp % 2 == 0:
        temp %= 2
        ans += 1

print(ans)
Problem p03361 (python)

Fixed submission:
import sys
h, w = map(int, input().split())
sq = []

for i in range(h):
    sq.append(input())

def is_valid(i, j):
#    global sq
    valid = False
    if i > 0 and sq[i-1][j] == '#':
        valid = True
    elif i < h-1 and sq[i+1][j] == '#':
        valid = True
    elif j > 0 and sq[i][j-1] == '#':
        valid = True
    elif j < w-1 and sq[i][j+1] == '#':
        valid = True
    return valid

for i in range(h):
    for j in range(w):
        c = sq[i][j]
        if c == '#':
            if not is_valid(i, j):
                print('No')
                sys.exit()

print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
h, w = map(int, input().split())
sq = []

for i in range(h):
    sq.append(input())

print(sq)

def is_valid(i, j):
    valid = False
    if i > 0 and sp[i-1][j] == '#':
        valid = True
    elif i < h-1 and sp[i+1][j] == '#':
        valid = True
    elif j > 0 and sp[i][j-1] == '#':
        valid = True
    elif j < w-1 and sp[i][j+1] == '#':
        valid = True
    return valid

for i in range(h):
    for j in range(w):
        #print(sq[i][j])
        c = sq[i][j]
        if c == '#':
            if not is_valid(i, j):
                print('No')
                sys.exit()

print('Yes')
Problem p03379 (python)

Fixed submission:
n = int(input())
x = list(map(int, input().split()))
xx = sorted(x, reverse=True)

m1 = xx[n//2-1]
m2 = xx[n//2]

# print(m1, m2)

for i in range(len(x)):
    if x[i] < m1:
        print(m1)
    elif x[i] >= m1:
        print(m2)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = list(map(int, input().split()))
y = sorted(set(x))
ms = y[len(y)//2-1]
mc = y[len(y)//2]
ml = y[len(y)//2+1]

#print(ms, mc, ml)

for i in range(n):
    if x[i] == mc:
        print(ml)
    if x[i] < mc:
        print(ml)
    elif x[i] > mc:
        print(mc)
Problem p03487 (python)

Fixed submission:
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
cnt = 1
prev = a[0]
#print(a)

for i in range(1, n):
    # print('\n# i: ' + str(i))
    # print('# a[i]: ' + str(a[i]))
    if a[i] == prev:
#        print('same!')
        cnt += 1
    else:
        if cnt > prev:
#            print('cnt is larger!')
            ans += cnt - prev
        elif cnt < prev:
#            print('cnt is smaller!')
            ans += cnt
#        print('ans: ' + str(ans))
        prev = a[i]
        cnt = 1

if cnt > prev:
    ans += cnt - prev
elif cnt < prev:
    ans += cnt

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
cnt = 1
prev = a[0]
#print(a)

for i in range(1, n):
    # print('\n# i: ' + str(i))
    # print('# a[i]: ' + str(a[i]))
    if a[i] == prev:
#        print('same!')
        cnt += 1
    else:
        if cnt == prev:
#            print('good, skip!')
        elif cnt > prev:
#            print('cnt is larger!')
            ans += cnt - prev
        else:
#            print('cnt is smaller!')
            ans += cnt
#        print('ans: ' + str(ans))
        prev = a[i]
        cnt = 1

if cnt > prev:
    ans += cnt - prev
elif cnt < prev:
    ans += cnt

print(ans)
Problem p03696 (python)

Fixed submission:
from collections import deque

n = int(input())
s = input()
lCnt = 0
rCnt = 0
stack = deque()
stack.append(s[0])

for i in range(1, len(s)):
#for i in range(0, len(s)):
    if len(stack) == 0:
        stack.append(s[i])
        continue

    prev = stack.pop()

    if prev == '(' and s[i] == ')':
        continue
    else:
        stack.append(prev)
        stack.append(s[i])

#print(stack)

for i in range(len(stack)):
    if stack[i] == '(':
        lCnt += 1
    else:
        rCnt += 1

s += ')' * lCnt
s = '(' * rCnt + s

print(s)

Buggy submission (verdict: Runtime Error):
from collections import deque

n = int(input())
s = input()
lCnt = 0
rCnt = 0
stack = deque()
stack.append(s[0])

for i in range(1, len(s)):
    prev = stack.pop()

    if prev == '(' and s[i] == ')':
        continue
    else:
        stack.append(prev)
        stack.append(s[i])

#print(stack)

for i in range(len(stack)):
    if stack[i] == '(':
        lCnt += 1
    else:
        rCnt += 1

s += ')' * lCnt
s = '(' * rCnt + s

print(s)