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

Fixed submission:
A, B = map(int, input().split())
if (1 <= A <= 9) & (1 <= B <= 9):
    s = A*B
    print(s)

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N = int(input())
s = N-1
a = N
b = 1
for i in range(int(N**0.5)+1, 1, -1):
        if N%i == 0:
            a = i
            b = N//i
            s = a + b -2
            break

print(s)

Buggy submission (verdict: Runtime Error):
N = int(input())
s = N-1
for i in range(1, int(N**0.5) + 1):
    for j in range(1, int(N**0.5) + 1):
        if N == i * j:
            flag = i + j - 2

            if flag <= s:
                s = flag

print(s)

Buggy submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if (1 <= A <= 9) & (1 <= B <= 9):
    s = A*B
    print(s)

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
# 問題文
# 高橋君は九九を覚えたので、1以上 9以下の 2つの整数の積を計算することができます。それ以外の計算はできません。
# 2つの整数 A, Bが与えられます。
# 高橋君が A×Bを計算できるならその結果を、できないなら代わりに -1 を出力してください。
# 制約
# 1≤A≤20
# 1≤B≤20
# 入力中のすべての値は整数である。
A = int(input())
B = int(input())
if (1 <= A <= 9) & (1 <= B <= 9):
    s = A*B
    print(s)

else:
    print(-1)
Problem p02881 (python)

Fixed submission:

N = int(input())
s = N-1
a = N
b = 1
for i in range(int(N**0.5)+1, 1, -1):
        if N%i == 0:
            a = i
            b = N//i
            s = a + b -2
            break

print(s)

Buggy submission (verdict: Runtime Error):
 N = int(input())
s = N-1
for i in range(1, int(N**0.5) + 1):
    for j in range(1, int(N**0.5) + 1):
        if N == i * j:
            flag = i + j - 2

            if flag <= s:
                s = flag

print(s)
Problem p02676 (python)

Fixed submission:
n=int(input())
s=input()
if len(s)<=n:
  print(s)
else:
  print(s[:n],'...',sep='')

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
if len(s)<=n:
  print(s)
else:
  print(s[:n,'...',sep=''])
Problem p02951 (python)

Fixed submission:
a, b, c= map(int, input().split())
if c - (a - b) >= 0:
  print(c - (a - b))
else:
  print(0)

Buggy submission (verdict: Runtime Error):
a,b,c = map(int, input().split)
if c - (a -b) >= 0:
  print(c - (a -b))
else:
  print(0)
Problem p02970 (python)

Fixed submission:
n, d = map(int, input().split())
i = (d * 2) + 1
if n % i == 0 :
  print(n // i)
else :
  print((n // i) + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
d = int(input())
i = (d * 2) + 1
if n % i == 0 :
  print(n // i)
else :
  print((n // i) + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
d = int(input())
i = (d * 2) + 1
if n % i == 0 :
  print(n / i)
else :
  print((n // i) + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
d = int(input())
i = (d * 2) + 1
if n % i == 0 :
  print(n / i)
else :
  print(n // i) + 1
Problem p02987 (python)

Fixed submission:
s = input()
s = sorted(s)
cnt = 0
for i in range(4 - 1) :
  if s[i] != s[i + 1] :
    cnt += 1
if cnt == 1 :
  print("Yes")
else :
  print("No")

Buggy submission (verdict: Runtime Error):
s = input()
cnt = 0
for i in renge(4 - 1) :
  if s[i] != s[i + 1] :
    cnt += 1
if cnt != 0 && cnt <= 2 :
  print("Yes")
else :
  print("No")
Problem p03011 (python)

Fixed submission:
l = list(map(int, input().split()))
sum = 0
max = 0
for i in range(3) :
  sum += l[i]
  if max <= l[i] :
    max = l[i]
print(sum - max)

Buggy submission (verdict: Runtime Error):
p, q, r = (int(i) for i in input().split())
if p >= q & p >= r :
  max = p
if q >= p & q >= r :
  max = q
if r >= p & r >= q :
  max = r
print(p + q + r - max)
Problem p02689 (python)

Fixed submission:
import sys
def input():
  return list(map(int, sys.stdin.readline().strip().split()))

n, m = input()
h = input()
tmp = [[] for i in range(n)]
for i in range(m):
  a, b = input()
  tmp[b - 1].append(a - 1)
  tmp[a - 1].append(b - 1)
ans = 0
for i in range(n):
  flag = True
  for ele in tmp[i]:
    if(h[i] <= h[ele]): flag = False
  if(flag): ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
def input():
  return list(map(int, sys.stdin.readline().strip().split()))

n, m = input()
h = input()
tmp = [[] for i in range(n)]
for i in range(m):
  a, b = input()
  tmp[b - 1].append(a - 1)
  tmp[a - 1].append(b - 1)
ans = 0
for i in range(n):
  flag = True
  for ele in tmp[i]:
    if(h[i] <= h[ele]):
  		flag = False
  if(flag): ans += 1
print(ans)
Problem p02699 (python)

Fixed submission:
s, w = input().split()

if int(w) >= int(s):
  print('unsafe')
else:
  print('safe')

Buggy submission (verdict: Runtime Error):
s, w = int(input().split)

if w >= s:
  print('unsafe')
else:
  print('safe')

Buggy submission (verdict: Runtime Error):
s, w = int(input().split)

if w <= s:
  print('unsafe')
else:
  print('safe')
Problem p02860 (python)

Fixed submission:
r = int(input())
s = input()
 
i = int(r/2)
before = s[i:]
after = s[:i]
 
if before == after:
	print('Yes')
else:
  	print('No')

Buggy submission (verdict: Runtime Error):
r = int(input())
s = input()
 
if i < 2:
  return

i = int(r/2)
before = s[i:]
after = s[:i]
 
if before == after:
	print('Yes')
else:
  	print('No')
Problem p02688 (python)

Fixed submission:
N, K = map(int, input().split())
list_A = [1]*N
for i in range(K):
    d = int(input())
    l = list(map(int, input().split()))
    for j in range(d):
        if list_A[l[j]-1]  == 1:
            list_A[l[j]-1]  = 0

print(list_A.count(1))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
list_A = [1]*N
for i in range(K):
    d = int(input())
    l = list(map(int, input().split()))
    for J range(d):
        if list_A[l[j]-1]  == 1:
            list_A[l[j]-1]  = 0

print(list_A.count(1))
Problem p02693 (python)

Fixed submission:
K = int(input()) 
A, B = map(int, input().split())
import math
n = math.floor(B / K) 
if n >= A / K:
    print('OK')
else:
    print('NG')

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split()) 

ans = []
if M % 2 != 0:
    for i in range((M+1)//2):
        ans.append((1 + i,M - i))
        
    for j in range((M - 1)//2):
        ans.append((M + 1 + j, 2*M + 1 - j))
        
else:
    for i in range(M//2):
        ans.append((1 + i, M + 1 - i))
        ans.append((M + 2 + j, 2*M + 1 - j))
              
for i in range(M):
    print(*ans[i])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split()) 
ans = []
if M % 2 != 0:
    for i in range((M+1)//2):
        ans.append((1 + i,M - i))
        
    for j in range((M - 1)//2):
        ans.append((M + 1 + j, 2*M + 1 - j))
else:
    for i in range(M//2):
        ans.append((1 + i, M + 1 - i))
        ans.append((M + 2 + j, 2*M + 1 - j))
              
for i in range(M):
    print(*ans[i])
Problem p02759 (python)

Fixed submission:
import math
n = int(input())
print(math.ceil(n/2))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
l = [list(input().split()) for i in range(m)]
for num in range(1000):
    if m == 0:
        if n == 1:
            print(0)
            exit()
        else:
            print(10**(n-1))
            exit()    
    for i in range(m):
        if len(str(num)) < n:
            break
        if str(num)[int(l[i][0])-1] != l[i][1]:
            break
    else:
        print(num)
        exit()
print(-1)
Problem p02836 (python)

Fixed submission:
#B
from collections import deque
s = deque(list(input()))
cnt = 0
while 1 < len(s):
    r = s.pop()
    l = s.popleft()
    if r != l:
        cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
#B
from collections import deque
s = deque(list(input()))
cnt = 0
while 1 <= len(s):
    r = s.pop()
    l = s.popleft()
    if r != l:
        cnt += 1
print(cnt)
Problem p02676 (python)

Fixed submission:
K=int(input(""))
S=input("")
lenS=len(S)
if lenS<=K:
  print(S)
else:
  print(S[:K]+"...")

Buggy submission (verdict: Runtime Error):
K=int(intput(""))
S=input("")
lens=len(s)
if lens<=K:
  print(S)
else:
  S2=S[:K-1]
  print(S2)
Problem p02831 (python)

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

def gcd(m, n):
  x = max(m, n)
  y = min(m, n)
  
  if x % y == 0:
    return y
  
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z
      
def lcm(m, n):
  return int(m * n / gcd(m, n))

print(lcm(A, B))

Buggy submission (verdict: Runtime Error):
import math

A, B = map(int, input().split)
min1 = math.gcd(A, B)

max1 = max(A, B)

for i in range(100000000):
  s = min1 * i
  if max1 < s:
    break
    
print(s)
Problem p02832 (python)

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

c = 1
cp = 0

for i in range(N):
  ai = an[i]
  if ai != c:
    cp += 1
    
  elif ai == c:
    c += 1
    
if N == cp:
    cp = -1

print(cp)

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

c = 1
cp = 0

for i in N:
  ai = an[i]
  if ai != c:
    cp += 1
    
  elif ai == c:
    c += 1
    
print(cp)
Problem p02836 (python)

Fixed submission:
s = input()

l = len(s)
sen = l // 2
c = 0

for i in range(sen):
  s1 = s[i]
  s2 = s[l-i-1]
  
  if s1 != s2:
    c += 1
print(c)

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

l = len(s)
sen = l // 2
c = 0

for i in range(sen):
  s1 = s[i]
  s2 = s[l-i]
  
  if s1 != s2:
    c += 1
print(c)
Problem p02948 (python)

Fixed submission:
import heapq

N, M = map(int,input().split())
L = [list(map(int,input().split())) for i in range(N)]

D = {}
for i in range(N) :
    k,v = L[i]
    if k not in D :
        D[k] = [v]
    else :
        D[k].append(v)

class Heapq :
    def __init__(self, arr, desc=False) :
        if desc :
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)
    
    def pop(self) :
        return heapq.heappop(self.hq) * self.sign
    
    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)
 
    def top(self):
        return self.hq[0] * self.sign
 
    def size(self):
        return len(self.hq)

q = Heapq([], True)
ans = 0
for k in range(1, M+1) :
    if k in D :
        for v in D[k] :
            q.push(v)
    if q.size() :
        ans += q.pop()
print(ans)

Buggy submission (verdict: Runtime Error):
import heapq

N,M = map(int,input().split())
L = [list(map(int,input().split())) for i in range(N)]
ans = 0
A = []

L = sorted(L, key=lambda x: x[1],reverse=True)
L = sorted(L, key=lambda x: x[0],reverse=True)

cnt = 0

for i in range(N) :
    if i >= 1 and L[i][0] != L[i-1][0] :
        cnt = 0
    
    if (M-L[i][0]+1)-cnt <= 0 :
        continue
    if i < M :
        heapq.heappush(A,L[i][1])
        cnt += 1
    else :
        minv = heapq.heappop(A)
        if minv >= L[i][1] :
            heapq.heappush(A, minv)
        else :
            heapq.heappush(A,L[i][1])
            cnt += 1

print(sum(A))

Buggy submission (verdict: Runtime Error):
import heapq

N,M = map(int,input().split())
L = [list(map(int,input().split())) for i in range(N)]
ans = 0
A = []

L = sorted(L, key=lambda x: x[1],reverse=True)
L = sorted(L, key=lambda x: x[0],reverse=True)

minv = float("inf")
cnt = 0

for i in range(N) :
    if i >= 1 and L[i][0] != L[i-1][0] :
        cnt = 0
    
    if (M-L[i][0]+1)-cnt <= 0 :
        continue
    if i < M :
        heapq.heappush(A,L[i][1])
        cnt += 1
    else :
        minv = heapq.heappop(A)
        heapq.heappush(A,max(L[i][1], minv))
        cnt += 1

print(sum(A))
Problem p03011 (python)

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

print(a+b+c-max(a,b,c))

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

q = deque([])

H,W = map(int,input().split())
L = []

for i in range(H) :
    S = input()
    for j in range(W) :
        if S[j] == "." :
            q.append((i,j))
    L.append(list(S))

ans = 0

while q :
    y,x = q.popleft()
    
    cnt = 1
    
    d = [1,-1]
    
    for dy in d :
        ny = y
        
        while True :
            ny += dy
            
            if (ny < 0) or (ny >= H) :
                break
            
            if L[ny][x] == "#" :
                break
            
            cnt += 1
    
    for dx in d :
        nx = x
        
        while True :
            nx += dx
            
            if (nx < 0) or (nx >= W) :
                break
            
            if L[y][nx] == "#" :
                break
            
            cnt += 1
    
    ans = max(ans,cnt)

print(ans)
Problem p03017 (python)

Fixed submission:
N,a,b,c,d = map(int,input().split())
S = input()
a -= 1
b -= 1
c -= 1
d -= 1

if c < d :
    if (S[b:d+1].count("##") == 0) and (S[a:c+1].count("##") == 0) :
        ans = "Yes"
    else :
        ans = "No"
else :
    if (S[b:d+1].count("##") == 0) and (S[a:c+1].count("##") == 0) :
        flg1 = 1
    else :
        flg1 = 0
    

    if S[a:d+1].count("...") >= 1 :
        flg2 = 1
    else :
        flg2 = 0
        
    for i in range(b,d+1) :
        if (S[i-1]+S[i]+S[i+1] == "...") :
            flg3 = 1
            break
    else :
        flg3 = 0
    
    
    if flg1*flg3 :
        ans = "Yes"
    else :
        ans = "No"
print(ans)

Buggy submission (verdict: Runtime Error):
N,A,B,C,D = map(int,input().split())
S = input()
A -= 1
B -= 1
C -= 1
D -= 1

if S[A:max(C,D)+1].count("##") == 0 :
    elif C < D :
        print("Yes")
    else :
        if S[B-1:D+2].count("...") :
            print("Yes")
        else :
            print("No")
else :
    print("No")
Problem p03038 (python)

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

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(M) :
    b,c = map(int,input().split())
    L.append(b,c)

L = sorted(L,key=lambda x: x[1], reverse=True)
for i in range(len(L)) :
    b,c = L[i]
    b = min(b,10**9-len(A))
    A.extend([c]*b)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(M) :
    b,c = map(int,input().split())
    L.append(b,c)

L = sorted(L,key=x: x[1], reverse=True)
for i in range(len(L)) :
    b,c = L[i]
    b = min(b,10**9-len(A))
    A.extend([c]*b)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)
Problem p03137 (python)

Fixed submission:
N,M = map(int,input().split())
L = list(map(int,input().split()))
if N >= M :
    ans = 0
else :
    L = sorted(L)
    A = []
    for i in range(M-1) :
        A.append(L[i+1]-L[i])
    A = sorted(A)
    ans = sum(A[:M-N])
print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
L = list(map(int,input().split()))
if N >= M :
    ans = 0
else :
    L = sorted(L)
    A = []
    for i in range(M-1) :
        A.append(L[i+1]-L[i])
A = sorted(A)
ans = A[:M-N]
print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
left = ""
right = ""

if N >= M :
    ans = 0
else :
    for i in range(N) :
        if i == 0 :
            left = L[0]
            L = L[1:]
        elif i == 1 :
            right = L[-1]
            L = L[:-1]
        else :
            if abs(left-L[0]) < abs(right-L[-1]) :
                right = L[-1]
                L = L[:-1]
            else :
                left = L[0]
                L = L[1:]
    #print(L)
    ans = 0

    while len(L) :
        #print(abs(left-L[0]),abs(right-L[-1]))
        if abs(left-L[0]) < abs(right-L[-1]) :
            ans += abs(left-L[0])
            left = L[0]
            L = L[1:]
        else :
            ans += abs(right-L[-1])
            right = L[-1]
            L = L[:-1]

print(ans)
#print(L)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
L = list(map(int,input().split()))
L = sorted(L)
left = ""
right = ""

for i in range(N) :
    if i >= M :
        break
    if i == 0 :
        left = L[0]
        L = L[1:]
    elif i == 1 :
        right = L[-1]
        L = L[:-1]
    else :
        if abs(left-L[0]) < abs(right-L[-1]) :
            right = L[-1]
            L = L[:-1]
        else :
            left = L[0]
            L = L[1:]
#print(L)
ans = 0

while len(L) :
    #print(abs(left-L[0]),abs(right-L[-1]))
    if abs(left-L[0]) < abs(right-L[-1]) :
        ans += abs(left-L[0])
        left = L[0]
        L = L[1:]
    else :
        ans += abs(right-L[-1])
        right = L[-1]
        L = L[:-1]

print(ans)
#print(L)
Problem p03221 (python)

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

C = []
for i in range(N+1) :
    C.append([])

for i, (p,y) in enumerate(L) :
    C[p].append([y,i])

for p in range(N+1) :
    for i,(y,_) in enumerate(sorted(C[p])) :
        ans[_] = str(p).zfill(6) + str(i+1).zfill(6)
for i in range(M) :
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
L = []
ans = []
for i in range(M) :
    L.append(list(map(int,input().split())))
    ans.append([None])

C = []
for i in range(N+1) :
    C.append([])

for i, (p,y) in enumerate(L) :
    C[p].append([y,i])

for p in range(N+1) :
    for i,(y,_) in enumerate(sorted(C[p])) :
        ans[_] = str(p).zfill(6) + str(i+1).zfill(6)
for i in range(N+1) :
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
L = []
ans = []
for i in range(M) :
    L.append(list(map(int,input().split())))
    ans.append([])

C = []
for i in range(N+1) :
    C.append([])

for i, (p,y) in enumerate(L) :
    C[p].append([y,i])

for p in range(N+1) :
    for i,(y,_) in enumerate(sorted(C[p])) :
        ans[_] = str(p).zfill(6) + str(i+1).zfill(6)
for i in range(N+1) :
    print(ans[i])
Problem p03634 (python)

Fixed submission:
from collections import deque

N = int(input())

L = [[] for i in range(N)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    L[a].append([b,c])
    L[b].append([a,c])
    

Q,K = map(int,input().split())

K -= 1

DISTANCE = [float("inf") for i in range(N)]

DISTANCE[K] = 0

q = deque([K])

while q :
    from_ = q.popleft()
    for to_, weight in L[from_] :
        dist = DISTANCE[from_] + weight
        if dist < DISTANCE[to_] :
            DISTANCE[to_] = dist
            q.append(to_)
            
ans = []

for i in range(Q) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    calc = DISTANCE[x] + DISTANCE[y]
    ans.append(calc)

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

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

input = sys.stdin.readline

N = int(input())
T = [[] for _ in range(N+1)]

for _ in range(N-1) :
    a,b,c = map(int,input().split())
    T[a].append([b,c])
    T[b].append([a,c])

Q,K = map(int,input().split())

dist = [None]*(N+1)

def dfs(now,d) :
    dist[now] = d
    for to,c in T[now] :
        if dist[to] is not None :
            continue
        dfs(to,d+c)

dfs(K,0)

ans = [0]*Q

for _ in range(Q) :
    u,v = map(int,input().split())
    ans[i] = dist[u]+dist[v]

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

Buggy submission (verdict: Runtime Error):
N = int(input())
T = [[] for i in range(N+1)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    T[a].append([b,c])
    T[b].append([a,c])

Q,K = map(int,input().split())

dist = [None]*(N+1)

def dfs(now,d) :
    dist[now] = d
    for to,c in T[now] :
        if dist[to] is not None :
            continue
        dfs(to,d+c)

dfs(K,0)

ans = []

for i in range(Q) :
    u,v = map(int,input().split())
    ans.append(dist[u]+dist[v])

print(*ans,sep="\n")
Problem p02819 (python)

Fixed submission:
x = int(input())
if x == 2:
  print(2)
  
else:
  var = x
  while True:
    for _ in range(2,var):
      if var%_ != 0:
        ind = True
      else:
        ind = False
        break
    if ind == True:
      break  
    else:
      var+=1  
  print(var)

Buggy submission (verdict: Runtime Error):
x = int(input())
if x == 2:
  print(2)
  
else:
  var = x
  while True:
    for _ in range(2,var):
      if var%_ != 0:
        ind = True
      else:
        ind = False
        break
    if ind == True:
      break  
    else:
      var+=1  
print(var)
Problem p02897 (python)

Fixed submission:
def main():
    n, = map(int, open(0).read().split())

    ans = ((n+1) // 2) / n
    print(ans)
    return()

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    a, b = map(int, open(0).read().split())

    ans = max(0, a - b * 2)
    print(ans)
    return()

if __name__ == '__main__':
    main()
Problem p03038 (python)

Fixed submission:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

Buggy submission (verdict: Runtime Error):
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  change += [c]*b_up
  if b_up != b:
    break
#print(change)

ans = 0
for i in range(n):
  #print(ans)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)