Harryxun/stf_run huggingface dataset:
N,M=map(int,input().split())
L=[]
S=[]
for i in range(N):
    x = input()
    l=[y for y in x]
    L.append(l)
for i in range(M):
    x = input()
    s=[y for y in x]
    S.append(s)
flag = 0
sum = 0
for i in range(N):
    for j in range(N):
        for k in range(M):
            for l in range(M):
                if i+k < N and j+l < N:
                    if L[i+k][j+l] == S[k][l]:
                        sum += 1
                        if sum == M * M:
                            flag = 1
                    else:
                        sum = 0
if flag == 0:
    print("No")
else:
    print("Yes")

### FIXED CODE ###

table=[2,3,4,5,6,7,8,9,10,11,12,13,1]
a,b=map(int,input().split())
if a == b :
    print("Draw")
elif table.index(a) < table.index(b):
    print("Bob")
elif table.index(a) > table.index(b):
    print("Alice")
H, W = map(int, input().split(' '))
S = [input() for i in range(H)]
M = [[10000]*W for i in range(H)]
M[0][0] = 0
for k in range(H*W):
    for i in range(H):
        for j in range(W):
            if M[i][j] == k:
                if i > 0 and M[i-1][j] > k+1 and S[i-1][j] == '.':
                    M[i-1][j] = k+1
                if i < H-1 and M[i+1][j] > k+1 and S[i+1][j] == '.':
                    M[i+1][j] = k+1
                if j > 0 and M[i][j-1] > k+1 and S[i][j-1] == '.':
                    M[i][j-1] = k+1
                if j < W-1 and M[i][j+1] > k+1 and S[i][j+1] == '.':
                    M[i][j+1] = k+1
if M[-1][-1] == 10000:
    print(-1)
    sys.exit()
minDis = M[-1][-1] + 1
numWhite = 0
for line in S:
    numWhite += line.count('.')
print(numWhite-minDis)

### FIXED CODE ###

import sys
import math

H, W = map(int, input().split(' '))
S = [input() for i in range(H)]
M = [[10000]*W for i in range(H)]
M[0][0] = 0
for k in range(H*W):
    for i in range(H):
        for j in range(W):
            if M[i][j] == k:
                if i > 0 and M[i-1][j] > k+1 and S[i-1][j] == '.':
                    M[i-1][j] = k+1
                if i < H-1 and M[i+1][j] > k+1 and S[i+1][j] == '.':
                    M[i+1][j] = k+1
                if j > 0 and M[i][j-1] > k+1 and S[i][j-1] == '.':
                    M[i][j-1] = k+1
                if j < W-1 and M[i][j+1] > k+1 and S[i][j+1] == '.':
                    M[i][j+1] = k+1
if M[-1][-1] == 10000:
    print(-1)
    sys.exit()
minDis = M[-1][-1] + 1
numWhite = 0
for line in S:
    numWhite += line.count('.')
print(numWhite-minDis)
a = input().split()
a = [int(i) for i in a]
c = input().split()
c = [int(i) for i in c]

def dacao(b):
	aller = [0]
	sumer = 0
	for i in b:
		sumer += i
		aller.append(sumer)
		
	superlist = dict()
	for i in range(len(b)):
		for j in range(i + 1, len(b) + 1):
			temp = bin(aller[j] - aller[i])[::-1]
			x = []
			for k in range(len(temp)):
				if temp[k] == '1':
					x.append(k)
			for m in x:
				try:
					superlist[m].append(aller[j] - aller[i])
				except:
					superlist[m] = [aller[j] - aller[i]]
				
	for i in sorted(superlist)[::-1]:
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break
		return [], -1
			
def cao(b):
	aller = [0]
	sumer = 0
	superlist = dict()
	for i in b:
		temp = bin(i)[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(i)
			except:
				superlist[m] = [i]
				
	for i in sorted(superlist)[::-1]:
		if i == 0:
			return [], -1
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break

sumx = 0
gan1, gan2 = dacao(c)
while gan2 != -1:
	sumx += 2**gan2
	gan1, gan2 = cao(gan1)
	
print(sumx)

### FIXED CODE ###

a = input().split()
a = [int(i) for i in a]
b = input().split()
b = [int(i) for i in b]

def cao(aller, i):
	dicter = dict()
	for m in aller:
		for n in m:
			if n < i:
				try:
					dicter[n].append(m)
				except:
					dicter[n] = [m]
	for i in sorted(dicter)[::-1]:
		if len(dicter[i]) >= a[1]:
			return dicter[i], i
	return [], -1
	
aller = [0]
sumer = 0
for i in b:
	sumer += i
	aller.append(sumer)
	
superlist = dict()
for i in range(len(b)):
	for j in range(i + 1, len(b) + 1):
		temp = bin(aller[j] - aller[i])[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(x)
			except:
				superlist[m] = [x]
						
for i in sorted(superlist)[::-1]:
	if len(superlist[i]) >= a[1]:
		li = superlist[i]
		sumer = 2 ** i
		gan1, gan2 = cao(li, i)
		while gan2 != -1:
			sumer += 2**gan2
			gan1,gan2 = cao(gan1,gan2)
		print(sumer)
		break
X = int(input())

V = (100, 101, 102, 103, 104, 105)
#V = (3, 4, 8, 11)
Memo = [0] * (X+1)

if X < min(V):
    print (0)
    exit()

for i in range(len(V)):
    Memo[V[i]] = 1
    for j in range(1, X+1):
        if j - V[i] > 0:
            Memo[j] |= Memo[j-V[i]]

print (Memo[X])
#print (Memo)


### FIXED CODE ###

X = int(input())

V = (100, 101, 102, 103, 104, 105)
#V = (3, 4, 8, 11)
Memo = [0] * (X+1)

if X < min(V):
    print (0)
    exit()

for i in range(len(V)):
    if V[i] > X:
      continue
    Memo[V[i]] = 1
    for j in range(1, X+1):
        if j - V[i] > 0:
            Memo[j] |= Memo[j-V[i]]

print (Memo[X])
#print (Memo)
n=int(input())
s=input()
s_len=len(s)
ans=""
for i in range(s_len):
    char=97+(ord(s)+n)%26
    ans+=chr(char)
print(ans)

### FIXED CODE ###

n=int(input())
s=input()
s_len=len(s)
ans=""
for i in range(s_len):
    char=65+(ord(s[i])+n-65)%26
    ans+=chr(char)
print(ans)
S = list(input())
count = 0
ans = [0]*(len(S))
for i in range(len(S)):
    if S[i] == "R":
        count += 1
    else:
        if count % 2 == 0:
            ans[i] += count//2
            ans[i-1] += count//2
            count = 0
        else:
            ans[i] += count//2 + 1
            ans[i-1] += count//2
            count = 0
	
S.reverse()
	
for i in range(len(S)):
    if S[i] == "L":
        count += 1
    else:
        if count % 2 == 0:
            ans[len(S)-i] += count//2
            ans[len(S)-i-1] += count//2
            count = 0
        else:
            ans[len(S)-i] += count//2 
            ans[len(S)-i-1] += count//2 +1
            count = 0

for a in ans:
    print(a, end=" ")

### FIXED CODE ###

import fractions

a,b,c,d = map(int,input().split()) 

cd = (c * d) // fractions.gcd(c, d)
b_c = b // c
b_d = b // d
b_cd = b // cd
a_c = a // c
a_d = a // d
a_cd = a // cd

ans = b - b_c - b_d + b_cd - a + a_c + a_d - a_cd

if a % c != 0 and a % d!= 0:
    ans += 1

print(ans)
n,k=map(int,input().split())
a=list(map(int,input().split()))
m=a.find(1)
ans=m//(k-1)+(n-m-1)//(k-1)
kk=m%(k-1)+(n-m-1)%(k-1)+1
ans+=1+(kk>k)
print(ans)

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
m=a.index(1)
ans=m//(k-1)+(n-m-1)//(k-1)
kk=m%(k-1)+(n-m-1)%(k-1)+1
if m!=0 and m!=n-1:
  if kk>1:
    ans+=1+(kk>k)
else:
  ans+=(ans*(k-1)+1<n)
print(ans)
a,b,c,d=int(input().split())
print(max(abs(a),abs(b))*max(abs(c),abs(d)))


### FIXED CODE ###

a,b,c,d=map(int,input().split())
print(max(a*c,a*d,b*c,b*d))
string = input()
ch = string[0]
max_string_len = 0
prev_len = 0
for idx,ch in enumerate(string):
  if ch  in {"A","T","G","C"}:
    prev_len += 1
  	max_string_len = max(max_string_len,prev_len)
  else:
    prev_len = 0
    
print(max_string_len)

### FIXED CODE ###

string = input()
ch = string[0]
max_string_len = 0
prev_len = 0
for idx,ch in enumerate(string):
  if ch  in {"A","T","G","C"}:
    prev_len += 1
    max_string_len = max(max_string_len,prev_len)
  else:
    prev_len = 0
    
print(max_string_len)
n = int(input())
red = []
blue = []
for _ in range(n):
    a, b = map(int, input().split())
    red.append([a, b])
for _ in range(n):
    c, d = map(int, input().split())
    blue.append([c, d])
red.sort()
b.sort()
count = 0
for i in range(n):
    a, b = red.pop(0)
    for j in range(len(blue)):
        if (a < blue[j][0]) & (b < blue[j][1]):
            count += 1
            blue.pop(j)
            break

### FIXED CODE ###

n = int(input())
red = []
blue = []
for _ in range(n):
    a, b = map(int, input().split())
    red.append([a, b])
for _ in range(n):
    c, d = map(int, input().split())
    blue.append([c, d])
blue.sort()
red.sort(key=lambda x: x[1], reverse=True)
ans = 0
for b in blue:
    for i, r in enumerate(red):
        if (r[0] < b[0]) & (r[1] < b[1]):
            ans += 1
            red.pop(i)
            break
print(ans)
n, x = map(int, input().split())
a = putty(n)
a = a[:x]
print(a.count("P"))

### FIXED CODE ###

def f(n, x):
    if n == 0:
        return 0 if x <= 0 else 1
    elif x <= (a[n-1] + 1):
        return f(n - 1, x - 1)
    else:
        return p[n - 1] + 1 + f(n - 1, x - 2 - a[n - 1])


n, x = map(int, input().split())

a, p=[1],[1]
for i in range(n):
    a.append(a[i] * 2 + 3)
    p.append(p[i] * 2 + 1)
    
print(f(n, x))
s=input().split()
t=input().split()
count=0
for i in s:
    if s[i] != t[i]:
        count += 1
print(count)

### FIXED CODE ###

s=input()
t=input()
length=len(s)
count=0
for i in range(length):
    if s[i] != t[i]:
        count += 1
print(count)
a=input()
print(a,replace("1","x").replace("9","1").replace("x","9"))

### FIXED CODE ###

a=input()
print(a.replace("1","x").replace("9","1").replace("x","9"))
S = input()
T = input()

S = S.split()
T = T.split()

count = 0
for i in range(3):
  if S[i]==T[i]:
    count+=1
print count

### FIXED CODE ###

S = list(input())
T = list(input())

count = 0
for i in range(3):
    if S[i] == T[i]:
        count+=1
print(count)
x = int(input())
a = [input() for i in range(x)]
y = int(input())
b = [input() for i in range(y)]

count_max == 0
for i in range(x):
  if x.count(i) - y.count(i) > count_max:
    x.count(i) - y.count(i) = count_max
print(count_max)    

### FIXED CODE ###

x = int(input())
a = [input() for _ in range(x)]
y = int(input())
b = [input() for _ in range(y)]

count_max = 0
for j in a:
  if (a.count(j) - b.count(j)) > count_max:
    count_max = a.count(j) - b.count(j)
print(count_max)
s = input()
print(700 + s.count(o) * 100)

### FIXED CODE ###

s = input()
print(700 + s.count("o") * 100)
n=int(input())
A=[]
X=[]
Y=[]
for i in range(n):
  a=int(input())
  A.append(a)
  x=[]
  y=[]
  for j in range(a):
    x1,y1=[int(s) for s in input().split()]
    x.append(x1)
    y.append(y1)
  X.append(x)
  Y.append(y)
  
c=0
#for i in range(n):
 # l=[1]*n
  #for j in range(i, n-i):
   # if l[j]==0:continue
    #for k in range(A[j]):
    　#　l[X[j][k]] *= Y[j][k]
  #if c<l.count(1):
   # c=l.count(1) 
  
        
  
#print(c)  
  
  
  
  
  
  
  
  
  
  
  
  
  

### FIXED CODE ###

n=int(input())
X=[]
Y=[]
A=[0]*n
for i in range(n):
  A[i]=int(input())
  a=A[i]
  x=[0]*a
  y=[0]*a
  for j in range(a):
    x[j],y[j]=[int(s) for s in input().split()]
  X.append(x)
  Y.append(y)
  
s=0
for i in range(2**n):
  c=0
  d=0
  B=[0]*n
  for j in range(n):
    if((i >> j) & 1):
      c=c+1
      B[j]=1
  for k in range(n):
    if B[k]==1:
      for j in range(A[k]):
        if Y[k][j]!=B[X[k][j]-1]:
          d=-1
          break
    if d<0:
      break
  if s<c and d==0:
    s=c
      
      
print(s)
a = int(raw_input())
b = int(raw_input())
 
if a != 3 && b != 3:
  print(str(3))
elif a != 2 && B != 2:
  print(str(2))
else:
  print(str(1))

### FIXED CODE ###

a = int(input())
b = int(input())
 
if (a != 1 and b != 1):
  print(1)
elif (a != 2 and b != 2):
  print(2)
else:
  print(3)
n = int(input())
a = list(map(int,input().split()))

li = [0 for i in range(n)]
for i in range(n):
    li[a[i]] += 1

x = 0
for i in range(1,n-1):
    x = max(li[i-1]+li[i]+li[i+1])

print(x)

### FIXED CODE ###

#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

li = [0 for i in range(10**5)]
for i in range(n):
    li[a[i]] += 1

x = 0
for i in range(1,10**5-1):
    x = max(li[i-1]+li[i]+li[i+1],x)

print(x)
def printing(a_start, a_2, n):
    if a_start[1] == 3:
        aaaaaa

    a_3 = []
    mod_list = []
    for i in range(len(a_2)):
        if a_2[i][1] % a_start[1] not in mod_list and a_2[i][1] % a_start[1] != 0:
            a_3.append(a_2[i])
            mod_list.append(a_2[i][1] % a_start[1])
    a_3.sort(key=lambda x: x[1], reverse=True)

    ans = ""
    while n % a_start[1] != 0:
        for i in range(len(a_3)):
            if n % a_start[1] >= a_3[i][1] % a_start[1]:
                n -= a_3[i][1]
                ans += str(a_3[i][0])
                break
    ans = ans + str(a_start[0]) * (n // a_start[1])
    ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
    print("".join(ans_list))

def main():
    n, m = [int(i) for i in input().split()]
    a = sorted([int(i) for i in input().split()], reverse=True)
    number = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    a_list = []
    a_2 = []

    for i in range(m):
        if number[a[i]] not in a_list:
            a_2.append([a[i], number[a[i]]])
            a_list.append(number[a[i]])
    a_2.sort(key=lambda x: x[1])
    a_start = a_2.pop(0)
    printing(a_start, a_2, n)

main()



### FIXED CODE ###

def printing(a_start, a_2, n):

    a_3 = []
    mod_list = []
    for i in range(len(a_2)):
        if a_2[i][1] % a_start[1] not in mod_list and a_2[i][1] % a_start[1] != 0:
            a_3.append(a_2[i])
            mod_list.append(a_2[i][1] % a_start[1])
    a_3.sort(key=lambda x: x[1], reverse=True)


    if len(a_3) >= 2:
        if a_start[0] + a_3[-1][0] < n and a_3[0][0] < a_3[1][0] and a_3[1][0] > a_start[0]:
            a_3.pop(0)


    ans = ""
    while n % a_start[1] != 0:
        for i in range(len(a_3)):
            if n % a_start[1] >= a_3[i][1] % a_start[1]:
                n -= a_3[i][1]
                ans += str(a_3[i][0])
                break
            elif n % a_start[1] == 0:
                break
        else:
            n -= a_3[0][1]
            ans += str(a_3[0][0])

    ans = ans + str(a_start[0]) * (n // a_start[1])
    ans_list = sorted([ans[i] for i in range(len(ans))], reverse=True)
    print("".join(ans_list))

def main():
    n, m = [int(i) for i in input().split()]
    a = sorted([int(i) for i in input().split()], reverse=True)
    number = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    a_list = []
    a_2 = []

    for i in range(m):
        if number[a[i]] not in a_list:
            a_2.append([a[i], number[a[i]]])
            a_list.append(number[a[i]])
    a_2.sort(key=lambda x: x[1])
    a_start = a_2.pop(0)
    printing(a_start, a_2, n)

main()
n,m=map(int,input().split())
mList=[int(input()) for i in range(m)]

#dp=[0]*(n+1)
dp=[-1]*(n+1)
dp[0]=1
if 1 not in mList:
  dp[1]=1

#for i in range(2,n+1):
#  if i in mList: continue
#  dp[i]+=dp[i-1]
#  dp[i]+=dp[i-2]
#  dp[i]%=1000000007

def calc(i):
  if dp[i]!=-1: return dp[i]
  if i in mList:
    dp[i]=0
    return dp[i]
  dp[i]=calc(i-1)+calc(i-2)
  dp[i]%=1000000007
  return dp[i]

print(calc(n))
#print(dp[n])


### FIXED CODE ###

n,m=map(int,input().split())
mList=set([int(input()) for _ in range(m)])

dp=[0]*(n+1)
dp[0]=1
if 1 not in mList:
  dp[1]=1

for i in range(2,n+1):
  if i in mList: continue
  dp[i]+=dp[i-1]
  dp[i]+=dp[i-2]
  dp[i]%=1000000007

print(dp[n])
H, W, D, *L = map(int, open('0').read().split())
A = L[:W*H]
Q = L[H*W]
inf = []
for l,r in zip(*[iter(L[H*W+1:])]*2):
  inf += [(l,r)]
dic = {A[i*W+j]:(i,j) for i in range(H) for j in range(W)}
ls = [[] for i in range(D)]
for i in range(D):
  s = i if i!=0 else D
  ls[i].append(0)
  m = s
  n = s
  while True:
    n += D
    if H*W<n:
      n = s
    px, py = dic[m]
    x, y = dic[n]
    ls[i].append(ls[i][-1]+abs(px-x)+abs(py-y))
    m = n
    if m==s:
      break
for l, r in inf:
  i = l%D
  if i==0:
    il = (l-D)//D
    ir = (r-D)//D
  else:
    il = (l-i)//D
    ir = (r-i)//D
  if l<r:
    print(ls[i][ir] - ls[i][il])
  elif l>r:
    print(ls[i][-1]-ls[i][il]+ls[i][ir])
  else:
    print(0)

### FIXED CODE ###

H, W, D = map(int, input().split())
px = [0]*(H*W+1)
py = [0]*(H*W+1)
dp = [0]*(H*W+1)
for i in range(H):
  inf = input().split()
  for j in range(W):
    a = int(inf[j])
    py[a] = i
    px[a] = j
for i in range(D+1, H*W+1):
  dp[i] = dp[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  print(dp[R]-dp[L])
def gcd(x,y):
    if y==0:
        return x
    else:
        return gcd(y,x%y)

x,y=map(int,raw_input().split())
print(gcd(x,y))



### FIXED CODE ###

def gcd(x,y):
    if y==0:
        return x
    else:
        return gcd(y,x%y)

x,y=map(int,raw_input().split())
print(gcd(x,y))
A,B = map(str,input().split())
print(B+A)

### FIXED CODE ###

moji = int(input())
print(("ABC","ABD")[moji >=1000])
s = input()
n = int(input())
for i in range(n):
 a = list(input().split())
 temp = s[int(a[1]):int(a[2])+1]
 if a[0] == 'replace':
  temp3 = a[3]
  s = list(s)
  seq = 0
  for i in range(int(a[1]), int(a[2])+1):
   s[i] = temp3[seq]
   seq += 1
 elif a[0] == 'reverse':
  temp2 = temp[::-1]
  s = s.replace(temp, temp2, 1)
 elif a[0] == 'print':
  print(temp)
 
  

### FIXED CODE ###

s = input()
n = int(input())
for i in range(n):
 a = list(input().split())
 temp = s[int(a[1]):int(a[2])+1]
 if a[0] == 'replace':
  temp3 = a[3]
  s = list(s)
  seq = 0
  for i in range(int(a[1]), int(a[2])+1):
   s[i] = temp3[seq]
   seq += 1
  s = ''.join(s)
 elif a[0] == 'reverse':
  temp2 = temp[::-1]
  s = s.replace(temp, temp2, 1)
 elif a[0] == 'print':
  print(temp)
N = int(input())
X = []
U = []
for i in range(N):
    I = input().split()
    (x, u) = (int(I[0]), I[1])
    X.append(x)
    U.append(u)

otoshidama = 0.0

for i in range(N):
    if U[i] == "JPY":
        otoshidama += X[i]
    elif U[i] == "BTC":
        otoshidama += X[i] * 380000.0

print(otoshidama)

### FIXED CODE ###

S = input()
S_split = S.split("/")
(year, month, day) = (int(S_split[i]) for i in range(3))

if month > 4:
    print("TBD")

else:
    print("Heisei")
n = int(input())
N = [int(i) for i in n]
dp = [[0,0] for _ in range(len(N))]

dp[0][0] = min(N[0],11-N[0])
dp[0][1] = min(N[0]+1,10-N[0])

for i in range(1,len(N)):
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))
print(dp[-1][0])

### FIXED CODE ###

N = input()
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1

for i in range(len(N)):
    n = int(N[i])
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))

print(dp[-1][0])
import sys
sys.setrecursionlimit(200000)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if a[0] != 1:
  a[0] = 1
  ans += 1
b = [[]for i in range(n)]
for i in range(n):
  b[a[i]-1].append(i)
b[0].remove(0)
huka = 0
kyo = [float("inf")] * n
def dfs(x,y):
  kyo[x] = y
  for i in b[x]:
    dfs(i,y+1)
dfs(0,0)
def dfs2(x,y):
  if kyo[x] == 0:
    return
  if y == k-1 and x != 0:
    kyo[x] = 0
    global ans
    ans += 1
    return
  kyo[x] = 0
  dfs2(a[x],y+1)
hukai = []
for i in range(n):
  hukai.append([kyo[i],i])
hukai.sort()
huka = []
for i in range(n):
  huka.append(hukai[i][1])
huka = huka[::-1]
for i in huka:
  dfs2(i,0)
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(200000)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
if a[0] != 1:
  a[0] = 1
  ans += 1
b = [[]for i in range(n)]
for i in range(1,n):
  b[a[i]-1].append(i)
huka = 0
kyo = [float("inf")] * n
z =[[]for i in range(n)]
def dfs(x,y):
  kyo[x] = y
  z[y].append(x)
  for i in b[x]:
    dfs(i,y+1)
dfs(0,0)
def dfs2(x,y):
  if kyo[x] <=k-y:
    return
  if y == k-1 and x != 0:
    kyo[x] = 0
    global ans
    ans += 1
    return
  kyo[x] = 0
  dfs2(a[x]-1,y+1)
for i in range(n-1,k,-1):
  for j in z[i]:
    dfs2(j,0)
print(ans)
num = map(int,raw_input().split)
if num[0]>num[1] :
    print str(num[0]) + " > " + str(num[1])  
elif num[0]<num[1] :
    print str(num[0]) + " < " + str(num[1])  
else :
    print str(num[0]) + " == " + str(num[1])  

### FIXED CODE ###

num = map(int,raw_input().split())
if num[0]>num[1] :
    print "a > b"  
elif num[0]<num[1] :
    print "a < b"  
else :
    print "a == b"
n = int(input())
F = [int(input().replace(' ',''), 2) for _ in range(n)]
P = [list(map(int, input().split())) for _ in range(n)]
ans=-10**7*n
for sp in range(1, 1033):
  total = 0
  for i in range(n):
    total += P[i][bin(F[i] & sp).count('1')]
  if total>ans:
    ans = total
  else:
    ans = ans
print(ans)


### FIXED CODE ###

n,a,b = map(int, input().split())
if n*a<b:
  print(n*a)
else:
  print(b)
a=input()
b=input()
c=input()
A=int(a)
B=int(b)
C=int(c)
if  A==B and A!=C:
  print('Yes')
if  A==C and A!=B: 
  print('Yes')
if  B==C and B!=A: 
  print('Yes')
else: 
  print('No')


### FIXED CODE ###

A,B,C = input().split()
if A==B and A!=C or B==C and B!=A or C==A and C!=B:
    print("Yes")
else:
    print("No")
N = int(input())
 print(((N-1) * N )/ 2)

### FIXED CODE ###

N = int(input())
print(((N-1)*N)//2)
A, B = list(map(int, input().split()))

for i in range(A, B+1):
    if i % K == 0:
        print('OK')
        exit()

print('NG')

### FIXED CODE ###

K = int(input())
A, B = list(map(int, input().split()))

for i in range(A, B+1):
    if i % K == 0:
        print('OK')
        exit()

print('NG')
x,y = map(int, input().split())

for i in range(x + 1):
  legs = 2 * i + 4 * (x - i)
  
  if legs == y:
    print("Yes")
    break
    
  else:
    print("No")


### FIXED CODE ###

a,b,c,d,e = (int(x) for x in input().split())

if a == 0:
  print(1)
  
elif b == 0:
  print(2)
  
elif c == 0:
  print(3)
  
elif d == 0:
  print(4)
  
else:
  print(5)
def Warshall_Floyd(edges,N):
  """頂点N個のグラフ上の隣接行列edgesが与えられたとき, 
  2点間の最短距離距離を隣接行列で返す"""
  for k in range(N):
    for i in range(N):
      for j in range(N):
        edges[i][j]=min(edges[i][j],edges[i][k]+d[k][j])
  return edges
 
import sys
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
B=Warshall_Floyd(A,N)
if A!=B:
  print(-1)
  sys.exit()
 
ans=0
for i in range(N):
  for j in range(i):
    need=1
    for k in range(N):
      if k!=i and k!=j:
        if A[i][j]==A[i][k]+A[k][j]: need=0
    if need: ans+=A[i][j]
print(ans)

### FIXED CODE ###

def Warshall_Floyd(edges,N):
  """頂点N個のグラフ上の隣接行列edges(空欄はinfとせよ)が与えられたとき, 
  2点間の最短距離距離を隣接行列で返す"""
  for k, i, j in itertools.product(range(N), range(N),range(N)):
    if edges[i][j]>edges[i][k]+edges[k][j]: return 1
  return 0
 
import sys
import itertools
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
if Warshall_Floyd(A,N):
  print(-1)
  sys.exit()
 
ans=0
for i, j in itertools.product(range(N),range(N)):
  need=1
  for k in range(N):
    if k!=i and k!=j and A[i][j]==A[i][k]+A[k][j]:
      need=0
      break
  if need:ans+=A[i][j]
print(ans//2)
N = int(input())
H = list(map(int, input().split()))

first = H[0]
for i, h in enumerate(H[1:]):
    if first > h:
        break
    first = h
print(i+1)

### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))
 
count = 0
for i, h in enumerate(H):
    if i == 0:
        count += 1
    elif i == 1:
        count += 1 if  H[1] >= H[0] else 0
    else:
        count += 1 if h >= max(H[:i]) else 0
print(count)
if __name__ == '__main__':
    [n, m] = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]

    a.sort()
    bc.sort(key=lambda x: x[1], reverse=True)

    index = 0
    for b, c in bc:
        if c <= a[index]:
            break
        for _ in range(b):
            if c > a[index]:
                a[index] = c
                index = index + 1
            else:
                break

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


### FIXED CODE ###

if __name__ == '__main__':
    [n, m] = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = [list(map(int, input().split())) for _ in range(m)]

    a.sort()
    bc.sort(key=lambda x: x[1], reverse=True)

    index = 0
    for b, c in bc:
        if index >= len(a) or c <= a[index]:
            break
        for _ in range(b):
            if index < len(a) and c > a[index]:
                a[index] = c
                index = index + 1
            else:
                break

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