Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
a=1;c={"R":0,"G":0,"B":0}
m=998244353
for i in range(1,int(input())+1):
	a*=i%m
for i in input():
	s=sorted(c.values())
	c[i]+=1
	if c[i]>s[2]:pass
	elif c[i]>s[1]:a*=s[2]-s[1]
	else:a*=s[1]-s[0]
	a%=m
print(a)

Fixed submission:
import math
n=int(input())
c={"R":0,"G":0,"B":0}
a=1
mod=998244353
for i in input():
	s=sorted((c["R"],c["G"],c["B"]))
	c[i]+=1
	if c[i]>s[2]:
		pass
	elif c[i]>s[1]:
		a*=s[2]-s[1]
	else:
		a*=s[1]-s[0]
	a%=mod
print(math.factorial(n)*a%mod)

Buggy python submission (verdict: Time Limit Exceeded):
a=1;c={"R":0,"G":0,"B":0}
for i in range(int(input())):
	a*=-~i
for i in input():
	s=sorted(c.values())
	c[i]+=1
	if c[i]>s[2]:
		pass
	elif c[i]>s[1]:
		a*=s[2]-s[1]
	else:
		a*=s[1]-s[0]
print(a%998244353)

Fixed submission:
import math
n=int(input())
c={"R":0,"G":0,"B":0}
a=1
mod=998244353
for i in input():
	s=sorted((c["R"],c["G"],c["B"]))
	c[i]+=1
	if c[i]>s[2]:
		pass
	elif c[i]>s[1]:
		a*=s[2]-s[1]
	else:
		a*=s[1]-s[0]
	a%=mod
print(math.factorial(n)*a%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
from math import factorial
n = int(input())
R,B,G = [],[],[]
m,M,mid = [],[],[]
ans = factorial(n)
for i,j in enumerate(input()):
	if j=="R":
		R += [i]
	elif j=="B":
		B += [i]
	else:
		G += [i]
for r,b,g in zip(R,B,G):
	f,s,t = sorted([r,b,g])
	m += [f]
	M += [t]
	mid += [s]
for i,j in enumerate(mid):
	i = bisect(m,j) - i
	if i > 1:
		ans *= i
for i,j in enumerate(mid[::-1]):
	i = n - i - bisect(M,j)
	if i > 1:
		ans *= i
print(ans)

Fixed submission:
import math
n=int(input())
c={"R":0,"G":0,"B":0}
a=1
mod=998244353
for i in input():
	s=sorted((c["R"],c["G"],c["B"]))
	c[i]+=1
	if c[i]>s[2]:
		pass
	elif c[i]>s[1]:
		a*=s[2]-s[1]
	else:
		a*=s[1]-s[0]
	a%=mod
print(math.factorial(n)*a%mod)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
mod = 10**9+7
dp = [0]*13
dp[0] = 1
for i,d in enumerate(s):
	_dp=[0]*13
	if d=="?":
		for x in range(10):
			for y in range(13):
				_dp[(y*10+x)%13] += dp[y]
	else:
		d = int(d)
		for y in range(13):
			_dp[(y*10+d)%13] += dp[y]
	for j in range(13):
		_dp[j] %= mod
	dp=_dp
print(dp[5])

Fixed submission:
s = input()
n = len(s)
mod = 10**9+7
dp = [[0]*13 for _ in range(n+1)]
dp[0][0] = 1
for i,d in enumerate(s):
	if d=="?":
		for x in range(10):
			for y in range(13):
				dp[i+1][(y*10+x)%13] += dp[i][y]
	else:
		d = int(d)
		for y in range(13):
			dp[i+1][(y*10+d)%13] += dp[i][y]
	for j in range(13):
		dp[i+1][j] %= mod
print(dp[n][5])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
mod = 10**9+7
dp = [[0]*13 for _ in range(n+1)]
dp[0][0] = 1
for i,d in enumerate(s):
	if d=="?":
		for x in range(10):
			for y in range(13):
				dp[i+1][(y*10+x)%13] += dp[i][y]
	else:
		d = int(d)
		for y in range(13):
			dp[i+1][(y*10+d)%13] += dp[i][y]
	for j in range(13):
		dp[i+1][j] %= mod
print(dp[n][5])

Fixed submission:
s = input()
n = len(s)
mod = 10**9+7
dp = [[0]*13 for _ in range(n+1)]
dp[0][0] = 1
for i,d in enumerate(s):
	if d=="?":
		for x in range(10):
			for y in range(13):
				dp[i+1][(y*10+x)%13] += dp[i][y]
	else:
		d = int(d)
		for y in range(13):
			dp[i+1][(y*10+d)%13] += dp[i][y]
	for j in range(13):
		dp[i+1][j] %= mod
print(dp[n][5])
Buggy python submission (verdict: Time Limit Exceeded):
s=int(input())
flag=False
if s<=10**9:
  li=[0,0,1,0,0,s]
else:
  a=b=int(s**(1/2))
  while True:
    c = s-a*b
    if c == 0:
      d=0;break
    sc = int(c**(1/2))
    for i in range(sc,sc+100):
      if c%i==0 and a+c<=10**9:
        d=i;c=c//i
        flag = True
        break
    if flag:
      break
    a -= 1
  li = [c,0,a+c,d,0,b]
for num in li:
  print(num,end=" ")

Fixed submission:
s = int(input())
if s==10**18:
  print(0,0,10**9,0,0,10**9)
else:
  print(0,0,s//10**9+1,1,10**9-s%10**9,10**9)

Buggy python submission (verdict: Runtime Error):
s=int(input())
flag=False
if s<=10**9:
  a=1;b=s;c=0;d=1
else:
  a=b=int(s**(1/2))
  while True:
    c = s-a*b
    sc = int(c**(1/2))
    for i in range(sc,sc+100):
      if c%i==0:
        d=i;c=c//i
        flag = True
        break
    if flag or c==0:
      break
    a -= 1
li = [c,0,a+c,d,0,b]
print(a*b+c*d)
for num in li:
  print(num,end=" ")

Fixed submission:
s = int(input())
if s==10**18:
  print(0,0,10**9,0,0,10**9)
else:
  print(0,0,s//10**9+1,1,10**9-s%10**9,10**9)

Buggy python submission (verdict: Runtime Error):
s=int(input())
flag=False
if s<=10**9:
  a=1;b=s;c=0;d=1
else:
  a=b=int(s**(1/2))
  while True:
    c = s-a*b
    sc = int(c**(1/2))
    for i in range(sc,sc+100):
      if c%i==0:
        d=i;c=c//i
        flag = True
        break
    if flag or c==0:
      break
    a -= 1
li = [c,0,a,d,0,b]
print(a*b+c*d)
for num in li:
  print(num,end=" ")

Fixed submission:
s = int(input())
if s==10**18:
  print(0,0,10**9,0,0,10**9)
else:
  print(0,0,s//10**9+1,1,10**9-s%10**9,10**9)

Buggy python submission (verdict: Runtime Error):
s=int(input())
flag=True
if s<=10**9:
  a=1;b=s;c=0
else:
  a=b=int(s**(1/2))
  c = s-a**2
  d = 1
  if c>10**9:
    sc=int(c**(1/2))
    for i in range(sc,c):
      if c%i==0:
        d=i;c=c//i
        break
li = [0,0,a,d,c,b]
for num in li:
  print(num,end=" ")

Fixed submission:
s = int(input())
if s==10**18:
  print(0,0,10**9,0,0,10**9)
else:
  print(0,0,s//10**9+1,1,10**9-s%10**9,10**9)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
de = deque([int(input()) for _ in range(n)])
for i in range(n):
  f = de[0]
  del de[0]
  print(max(de))
  de.append(f)

Fixed submission:
from collections import deque
n = int(input())
li = [int(input()) for _ in range(n)]
m = max(li)
ind = li.index(m)
li.sort()
mm= li[n-2]
for i in range(n):
  if i == ind:
    print(mm)
  else:
    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
li = [int(input()) for _ in range(n)]
for i in range(n):
  if i == 0:
    print(max(li[1:]))
  elif i == n:
    print(max(li[:-1]))
  else:
    print(max(li[:i]+li[i+1:]))

Fixed submission:
from collections import deque
n = int(input())
li = [int(input()) for _ in range(n)]
m = max(li)
ind = li.index(m)
li.sort()
mm= li[n-2]
for i in range(n):
  if i == ind:
    print(mm)
  else:
    print(m)
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import*
N,*A=map(int,open(0))
L=[A[0]]
for x in A[1:]:
	i=bisect_left(L,x)
	if i==0:L=[x]+L
	else:L[i-1]=x
print(len(L))

Fixed submission:
from collections import*
from bisect import*
N,*A=map(int,open(0))
q=deque([A[0]])
for x in A[1:]:
	i=bisect_left(q,x)
	if i==0:q.appendleft(x)
	else:q[i-1]=x
print(len(q))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(200000)
n = int(input())
li = list(map(int,input().split()))
a = li[0]
del li[0]

def f(num1,num2,l):
  t = num1^num2
  if len(l) == 1 and t in l:
    return True
  else:
    if t in l:
      l.remove(t)
      return f(num2,t,l)
    else:
      return False

for i in li:
  if not a^i in li:
    continue
  else:
    if f(i,a,li[:]):
      print("Yes")
      exit()
    else:continue
print("No")

Fixed submission:
n=int(input())
*a,=map(int,input().split())
s=list(set(a))
s.sort()
ans="No"
if len(s)==1:
  if 0 in s:
    ans="Yes"
elif len(s)==2:
  if a.count(0)*2==a.count(s[1]):
    ans="Yes"
elif len(s)==3 and a.count(s[0])==a.count(s[1])==a.count(s[2]) and s[0]^s[1]^s[2]==0:
  ans="Yes"
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
n = int(input())
li = list(map(int,input().split()))
a = li[0]
del li[0]

def f(num1,num2,l):
  t = num1^num2
  if len(l) == 1 and t in l:
    return True
  else:
    if t in l:
      l.remove(t)
      return f(num2,t,l)
    else:
      return False

for i in li:
  if not a^i in li:
    continue
  else:
    if f(i,a,li[:]):
      print("Yes")
      exit()
    else:continue
print("No")

Fixed submission:
n=int(input())
*a,=map(int,input().split())
s=list(set(a))
s.sort()
ans="No"
if len(s)==1:
  if 0 in s:
    ans="Yes"
elif len(s)==2:
  if a.count(0)*2==a.count(s[1]):
    ans="Yes"
elif len(s)==3 and a.count(s[0])==a.count(s[1])==a.count(s[2]) and s[0]^s[1]^s[2]==0:
  ans="Yes"
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
n = int(input())
li = list(map(int,input().split()))
a = li[0]
del li[0]

def f(num1,num2,l):
  if l == []:
    return True
  else:
    if num1^num2 in l:
      l.remove(num1^num2)
      return f(num2,num1^num2,l)
    else:
      return False

for i in li:
  if f(a,i,li[1:]):
    print("Yes")
    exit()
  else:continue
print("No")

Fixed submission:
n=int(input())
*a,=map(int,input().split())
s=list(set(a))
s.sort()
ans="No"
if len(s)==1:
  if 0 in s:
    ans="Yes"
elif len(s)==2:
  if a.count(0)*2==a.count(s[1]):
    ans="Yes"
elif len(s)==3 and a.count(s[0])==a.count(s[1])==a.count(s[2]) and s[0]^s[1]^s[2]==0:
  ans="Yes"
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
li = list(map(int,input().split()))
a = li[0]
del li[0]
def f(num1,num2,l):
  if l == []:
    return True
  else:
    if num1^num2 in l:
      return f(num2,num1^num2,l[1:])
    else:
      return False
for i in li:
  if f(a,i,li[1:]):
    print("Yes")
    exit()
  else:continue
print("No")

Fixed submission:
n=int(input())
*a,=map(int,input().split())
s=list(set(a))
s.sort()
ans="No"
if len(s)==1:
  if 0 in s:
    ans="Yes"
elif len(s)==2:
  if a.count(0)*2==a.count(s[1]):
    ans="Yes"
elif len(s)==3 and a.count(s[0])==a.count(s[1])==a.count(s[2]) and s[0]^s[1]^s[2]==0:
  ans="Yes"
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n = int(input())
A = [[0]*n for _ in range(n)]
for i in range(n-1):
  A[i][i]=A[i][i+1]=1
A[n-1][0]=A[n-1][n-1]=1
b = list(map(lambda x:int(x)*2,input().split()))
A = np.array(A)
b = np.array(b)
x = np.linalg.solve(A,b)
for i in x:
  print(int(i),end=" ")

Fixed submission:
n = int(input())
li = list(map(lambda x:int(x)*2,input().split()))
num = 0
for i,b in enumerate(li[1:]):
  if i%2==0:
    num += b
  else:
    num -= b
x = (li[0]-num)//2
print(x,end=" ")
for i in range(n-1):
  x = li[i]-x
  print(x,end=" ")
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

mod = 10**9 + 7
n,k = map(int,input().split())

def cmb(n,r,mod):
  if r==0 or n==r:return 1
  r = min(n-r,r)
  ue = reduce(mul, range(n, n-r, -1))
  shita = reduce(mul, range(1,r+1))
  return (ue//shita)%mod

for i in range(1,k+1):
  try:print(cmb(n-k+1,i,mod)*cmb(k-1,k-i,mod)%mod)
  except:print(0)

Fixed submission:
n = int(input())
dli = list(map(int,input().split()))
dli.sort()
print(dli[n//2]-dli[(n-1)//2])
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

mod = 10**9 + 7
n,k = map(int,input().split())

def cmb(n,r,mod):
  if r==0 or n==r:return 1
  r = min(n-r,r)
  ue = reduce(mul, range(n, n-r, -1))
  shita = reduce(mul, range(1,r+1))
  return (ue//shita)%mod

for i in range(1,k+1):
  print(cmb(n-k+1,i,mod)*cmb(k-1,k-i,mod)%mod)

Fixed submission:
from operator import mul
from functools import reduce

mod = 10**9 + 7
n,k = map(int,input().split())

def cmb(n,r,mod):
  if r==0 or n==r:return 1
  r = min(n-r,r)
  ue = reduce(mul, range(n, n-r, -1))
  shita = reduce(mul, range(1,r+1))
  return (ue//shita)%mod

for i in range(1,k+1):
  try:print(cmb(n-k+1,i,mod)*cmb(k-1,k-i,mod)%mod)
  except:print(0)

Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

mod = 10**9 + 7
n,k = map(int,input().split())

def cmb(n,r,mod):
  if r==0 or n==r:return 1
  r = min(n-r,r)
  ue = reduce(mul, range(n, n-r, -1))
  shita = reduce(mul, range(1,r+1))
  return (ue//shita)%mod

for i in range(1,k+1):
  print(cmb(n-k+1,i,mod)*cmb(k-1,k-i,mod))

Fixed submission:
from operator import mul
from functools import reduce

mod = 10**9 + 7
n,k = map(int,input().split())

def cmb(n,r,mod):
  if r==0 or n==r:return 1
  r = min(n-r,r)
  ue = reduce(mul, range(n, n-r, -1))
  shita = reduce(mul, range(1,r+1))
  return (ue//shita)%mod

for i in range(1,k+1):
  try:print(cmb(n-k+1,i,mod)*cmb(k-1,k-i,mod)%mod)
  except:print(0)
Buggy python submission (verdict: Runtime Error):
from collections import deque
N,M,*L,S,T = map(int,open(0).read().split())
G = [[] for _ in range(N)]
step = [0 for _ in range(N)]
step[(S-1)*3] = 0
for u,v in zip(*[iter(L)]*2):
	G[u-1].append(v-1)
q = deque([(S-1,0)])
while q:
	cur,d = q.popleft()
	for nxt in G[cur]:
		if nxt==T-1 and (d+1)%3==0:
			print((d+1)//3)
			exit()
		if not step[nxt] & 1<<((d+1)%3):
			step[nxt] |= 1<<(d+1)%3
			q.append((nxt,d+1))
print(-1)

Fixed submission:
from collections import deque
N,M,*L,S,T = map(int,open(0).read().split())
G = [[] for _ in range(N)]
step = [[-1]*3 for _ in range(N)]
step[S-1][0] = 0
for u,v in zip(*[iter(L)]*2):
	G[u-1].append(v-1)
q = deque([(S-1,0)])
while q:
	cur,d = q.popleft()
	for nxt in G[cur]:
		if nxt==T-1 and (d+1)%3==0:
			print((d+1)//3)
			exit()
		if step[nxt][(d+1)%3] < 0:
			step[nxt][(d+1)%3] = d+1
			q.append((nxt,d+1))
print(-1)
Buggy python submission (verdict: Runtime Error):
from itertools import accumulate
n,k = map(int,input().split())
a = list(map(int,input().split()))
cnt = 0
leng = n
for i in range(n):
  aaa = accumulate(a[i:])
  for num,j in enumerate(aaa):
    if j >= k:
      cnt += leng - num
      break
  leng -= 1
print(cnt)

Fixed submission:
x,a = map(int,input().split())
if x < a:
  print(0)
else:
  print(10)

Buggy python submission (verdict: Runtime Error):
x,a = map(int,input().sprit())
if x < a:
  print(0)
else:
  print(10)

Fixed submission:
x,a = map(int,input().split())
if x < a:
  print(0)
else:
  print(10)
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
s=input().split()
t=input().split()
dp=[[1]*(m+1)]+[[1]+[0]*m for _ in range(n)]
for i in range(n):
	for j in range(m):
		dp[i+1][j+1]=(dp[i+1][j]+dp[i][j+1]-dp[i][j]*(s[i]!=t[j]))%(10**9+7)
print(dp[-1][-1])

Fixed submission:
import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7

def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m])

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7

def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m])

if __name__ == '__main__':
	main()

Fixed submission:
import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7

def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m])

if __name__ == '__main__':
	main()
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = [int(input()) for _ in range(m)]
mod = 10**9+7
dp1=0;dp2=1
for i in range(n):
	dp1,dp2=dp2,(dp1+dp2)*(not i+1 in a)
	if dp1==dp2==0:
		print(0);exit()
print(dp2%mod)

Fixed submission:
p,q,r = map(int,input().split())
print(min([p+q,q+r,r+p]))

Buggy python submission (verdict: Runtime Error):
p,q,r = map(int,input().split())
print(min[p+q,q+r,r+p])

Fixed submission:
p,q,r = map(int,input().split())
print(min([p+q,q+r,r+p]))
Buggy python submission (verdict: Runtime Error):
n=input()
w=sorted(list(map(int,input().split())))
s=sum(w)//2
for i,j in enumerate(w):
  s-=j
  if s<0:
    print(j-w[i-1]);break
  elif s==0:
    print(min(j-w[i-1],w[i+1]-j);break

Fixed submission:
n = int(input())
wli = list(map(int,input().split()))
cand = []
for i in range(n-1):
  cand.append(abs(sum(wli[:i+1])-sum(wli[i+1:])))
print(min(cand))
Buggy python submission (verdict: Runtime Error):
n, *L = map(int, open(0).read().split())
ab = L[:2 * n - 2]
c = sorted(L[2 * n - 2:])
con = [[] for _ in range(n)]
ans = [0] * n
tot = sum(c) - max(c)
for a, b in zip(*[iter(ab)] * 2):
	con[a - 1].append(b - 1)
	con[b - 1].append(a - 1)
vis = [0] * n
q = [0]
vis[0] = 1
while q:
	cur = q.pop()
	ans[cur] = c.pop()
	for nxt in con[cur]:
		if vis[nxt]:
			continue
		q.append(nxt)
print(tot)
print(*ans)

Fixed submission:
n, *L = map(int, open(0).read().split())
ab = L[:2 * n - 2]
c = sorted(L[2 * n - 2:])
con = [[] for _ in range(n)]
ans = [0] * n
tot = sum(c) - max(c)
for a, b in zip(*[iter(ab)] * 2):
	con[a - 1].append(b - 1)
	con[b - 1].append(a - 1)
vis = [0] * n
q = [0]
vis[0] = 1
while q:
	cur = q.pop()
	ans[cur] = c.pop()
	for nxt in con[cur]:
		if vis[nxt]:
			continue
		q.append(nxt)
		vis[nxt] = 1
print(tot)
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
li=[list(map(lambda x:int(x)+1,input().split())) for _ in range(m)]
s=set(range(li[0][0],li[0][1]+1))
for i in li:
  s = s & set(range(i[0],i[1]+1))
print(len(s))

Fixed submission:
n,m = map(int,input().split())
s = [list(map(int,input().split())) for i in range(m)]
l = s[0][0]
r = s[0][1]
for item in s:
  if item[0] > l:
    l = item[0]
  if item[1] < r:
    r = item[1]
if r-l+1 > 0:
  print(r-l+1)
else:
  print(0)
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
*a,=map(int,input().split())
l=[list(map(int,input().split()))[::-1] for _ in range(m)]
a.sort()
d=[]
for c,b in sorted(l)[::-1]:
	d+=[c]*b
for i,(x,y) in enumerate(zip(a,d)):
	if x<y:a[i]=y
print(sum(a))

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n
		break
cut = cnt
for i,(x,y) in enumerate(zip(a,cmp[:min(n,cnt+1)])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
  b,c = input().split()
  for j in range(int(b)):
    if a[j] < int(c):
      a[j] = int(c)
    else:
      break
  a.sort()
print(sum(a))

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n
		break
cut = cnt
for i,(x,y) in enumerate(zip(a,cmp[:min(n,cnt+1)])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
  b,c = map(int,input().split())
  for j in range(b):
    if a[j] < c:
      a[j] = c
    else:
      break
  a.sort()
print(sum(a))

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n
		break
cut = cnt
for i,(x,y) in enumerate(zip(a,cmp[:min(n,cnt+1)])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
  b,c = map(int,input().split())
  for j in range(b):
    if a[j] < c:
      a[j] = c
    else:
      break
  a.sort()
print(sum(a))

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n
		break
cut = cnt
for i,(x,y) in enumerate(zip(a,cmp[:min(n,cnt+1)])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))
Buggy python submission (verdict: Runtime Error):
from functools import lru_cache
import sys
sys.setrecursionlimit(200000)

n,m,*l=map(int,open(0).read().split())
graph=[[]*n for _ in range(n)]
unvis=[True]*n

@lru_cache(maxsize=None)
def dfs(x):
	for y in graph[x]:
		if unvis[y]:
			unvis[y]=False
			dfs(y)
			
for i,j in zip(l[::3],l[1::3]):
	graph[i-1].append(j-1)
	graph[j-1].append(i-1)

ans=0
for x in range(n):
	if unvis[x]:
		unvis[x]=False
		dfs(x)
		ans+=1
print(ans)

Fixed submission:
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.root = [-1]*(n+1)
		self.rank = [0]*(n+1)

	def find(self, x):
		if(self.root[x] < 0):
			return x
		else:
			self.root[x] = self.find(self.root[x])
			return self.root[x]
			
	def unite(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if(x == y):
			return 0
		elif(self.rank[x] > self.rank[y]):
			self.root[x] += self.root[y]
			self.root[y] = x
		else:
			self.root[y] += self.root[x]
			self.root[x] = y
			if(self.rank[x] == self.rank[y]):
				self.rank[y] += 1
	
	def f(self, x):
		return self.root[x] < 0
		
n,m,*l=map(int,open(0).read().split())
uf=UnionFind(n)
for i,j in zip(l[::3],l[1::3]):
	uf.unite(i,j)
print(sum(uf.f(i) for i in range(1,n+1)))

Buggy python submission (verdict: Runtime Error):
def input(): return sys.stdin.readline().strip()
n,m = map(int,input().split())
s = [input().split() for i in range(m)]
li = [[s[0][0],s[0][1]]]
del s[0]
for item in s:
  num_1 = item[0]
  num_2 = item[1]
  for group in li:
    if num_1 in group and num_2 in group:
      flag=0
      pass
      break
    elif num_1 in group:
      group.append(num_2)
      flag=0
      break
    elif num_2 in group:
      group.append(num_1)
      flag=0
      break
    else:
      flag = 1
  if flag:
    li.append([num_1,num_2])
count = 0
for j in li:
  count += len(j)-1
print(n-count)

Fixed submission:
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.root = [-1]*(n+1)
		self.rank = [0]*(n+1)

	def find(self, x):
		if(self.root[x] < 0):
			return x
		else:
			self.root[x] = self.find(self.root[x])
			return self.root[x]
			
	def unite(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if(x == y):
			return 0
		elif(self.rank[x] > self.rank[y]):
			self.root[x] += self.root[y]
			self.root[y] = x
		else:
			self.root[y] += self.root[x]
			self.root[x] = y
			if(self.rank[x] == self.rank[y]):
				self.rank[y] += 1
	
	def f(self, x):
		return self.root[x] < 0
		
n,m,*l=map(int,open(0).read().split())
uf=UnionFind(n)
for i,j in zip(l[::3],l[1::3]):
	uf.unite(i,j)
print(sum(uf.f(i) for i in range(1,n+1)))