Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from heapq import *
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
F = list(map(int, input().split()))
F.sort(reverse=True)
data = []
for a, f in zip(A, F):
    heappush(data, (-a*f, a, f))
for _ in range(K):
    x, a, f = heappop(data)
    if a > 0:
        a -= 1
    heappush(data, (-a*f, a, f))

print(-heappop(data)[0])

### FIXED CODE ###

import sys
input = sys.stdin.readline

def binary_search_int(ok, ng, test):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if test(mid):
            ok = mid
        else:
            ng = mid
    return ok


N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
F = list(map(int, input().split()))
F.sort(reverse=True)

def max2(x,y):
    return x if x > y else y

def test(x):
    temp = K
    for a, f in zip(A, F):
        temp -= max2((a - x//f), 0)
        if temp < 0:
            return False
    return True

print(binary_search_int(A[-1]**2 + 100, -1,test))
### BUGGY CODE ###

N, M = map(int, input().split())
abl1 = []
abl2 = []
for _ in range(M):
    ab = list(map(int,input().split()))
    if ab[0] == 1:
        abl1.append(ab)
    elif ab[1] == N:
        abl2.append(ab)

for i in abl1:
    if [i[1], N] in abl2:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')


### FIXED CODE ###

N, M = map(int, input().split())

abl1 = set()
abl2 = set()
for _ in range(M):
    ab = list(map(int,input().split()))
    if ab[0] == 1:
        abl1.add(ab[1])
    elif ab[1] == N:
        abl2.add(ab[0])


for i in abl1:
   if i in abl2:
       print("POSSIBLE")
       exit()
print("IMPOSSIBLE")
### BUGGY CODE ###

a, b, c, k = map(int, input().split())
A = [1 for _ in range(a)]
B = [0 for _ in range(b)]
C = [-1 for _ in range(c)]
D = A + B + C
print(sum(D[:k]))

### FIXED CODE ###

a, b, c, k = map(int, input().split())
if k <= a:
    print(k)
if a < k <= a + b:
    print(a)
if a + b < k:
    d = 2*a + b - k
    print(d)
### BUGGY CODE ###

k,s = map(int, input().split())
count = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                count += 1
print(count)

### FIXED CODE ###

k,s = map(int, input().split())
count = 0
for x in range(k+1):
    for y in range(k+1):
        z = s-x-y
        if z >= 0 and z <= k:
            count += 1
print(count)
### BUGGY CODE ###

a,b,k = map(int,input().split())
if (a + b) <= k:
  print("0 0")
else:
  for i in range (k):
    if a > 0:
      a -= 1
    elif b > 0:
      b -= 1
    else:
      break
  print(str(a) + " " + str(b))

### FIXED CODE ###

a,b,k = map(int,input().split())
if (a + b) <= k:
  a = 0
  b = 0
elif a >= k:
  a = a - k
else:
  b = b - (k - a)
  a = 0 
print(str(a) + " " + str(b))
### BUGGY CODE ###

N, X, Y = map(int, input().split())
ans = []
for i in range(1, N+1):
    for j in range(i+1, N+1):
        # 使わない場合
        a = abs(j - i)
        # 使う場合
        b = abs(i - X) + abs(j - Y) + 1
        ans.append(min(a, b))
for i in range(1, N):
    print(ans.count(i))


### FIXED CODE ###

N, X, Y = map(int, input().split())
ans = [0]*(N-1)
for i in range(1, N+1):
    for j in range(i+1, N+1):
        # 使わない場合
        a = abs(j - i)
        # 使う場合
        b = abs(i - X) + abs(j - Y) + 1
        ans[(min(a, b))-1] += 1
for i in ans:
    print(i)
### BUGGY CODE ###

N = int(input())
for n in range(1, N + 1):
    ans = 0
    for x in range(1, int(pow(n, 0.5))):
        if x ** 2 + 2 * x > n:
            break
        for y in range(1, int(pow(n, 0.5))):
            if x ** 2 + y ** 2 + x * y + y + x > n:
                break
            for z in range(1, int(pow(n, 0.5))):
                if x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x > n:
                    break
                if x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x == n:
                    ans += 1
    print(ans)

### FIXED CODE ###

N = int(input())
A = [0] * 10001
for x in range(1, int(pow(N, 0.5))):
    for y in range(1, int(pow(N, 0.5))):
        for z in range(1, int(pow(N, 0.5))):
            value = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
            if value > 10000:
                break
            A[value] += 1
for a in A[1:N+1]:
    print(a)
### BUGGY CODE ###

import functools
import operator

func = functools.partial(functools.reduce, operator.mul)

n = int(input())
a = list(map(int, input().split()))
res = func(a)
if res > (10**18):
    print(-1)
else:
    print(res)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
res = 1
A.sort()
for i in range(N):
    res *= A[i]
    if res > (10 ** 18):
        print(-1)
        exit()
    elif res == 0:
        print(0)
        exit()
print(res)
### BUGGY CODE ###

import numpy as np
import math
N,M = map(int,input().split())
ls = []
for n in range(N):
  ls.append(list(map(int,input().split())))
ls = np.array(ls)
idx = np.argsort(ls[:,1])[::-1]
#print (ls[idx])
days = list(range(1,M+1))
days = {k: 0 for k in days}
#print (days)
sum = 0
skip_d = -1
flag = False
for work in ls[idx]:
  d = work[0]
  if d==skip_d:
    pass
  else:
    while(d<=M):
      if d in days:
        sum+=work[1]
        days.pop(d)
        d=M+1
        skip_d = -1
        flag = True
      else:
        d+=1
    if not flag:
      skip_d = work[0]
      flag = False
print (sum)
  
    


### FIXED CODE ###

import heapq
N,M = map(int,input().split())
ls = []
h = []
ans = 0
for n in range(N):
    ls.append(list(map(int,input().split())))
ls = sorted(ls,key=lambda ls:ls[0],reverse=False)
n = 0
for m in range(1,M+1):
    #print (m)
    while(n<N and ls[n][0]<=m ):
        heapq.heappush(h, -ls[n][1])
        n+=1
    if len(h)>0:
        ans -= heapq.heappop(h)
        # print (m,ans)
print (ans)
### BUGGY CODE ###

N=int(input())
a=[]
for i in range(N):
  a.append(int(input()))
flag=True
step=[]
pivot=0
for i in range(N):
  step.append(pivot)
  if a[pivot]==2:
    break
  pivot=a[pivot]-1
  if pivot in step:
    flag=False
    break
if flag:
  print(len(step))
else:
  print(-1)

### FIXED CODE ###

N=int(input())
a=[]
for i in range(N):
  a.append(int(input())-1)
flag=True
step=[]
pivot=0
for i in range(N+1):
  step.append(pivot)
  if a[pivot]==1:
    break
  pivot=a[pivot]

if len(step)>N:
  print(-1)
else:
  print(len(step))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


N = readint()
S = readstr()
Q = readint()
K = readints()

if 'D' not in S:
    printrows([0]*Q)
    exit()

ans = []
for k in K:
    m = 0
    c = 0
    a = [0]
    d = S.find('D')
    e = S.rfind('D')
    for i in range(d+1,min(N,d+k)):
        if S[i]=='M':
            m += 1
        if S[i]=='C':
            c += 1
            a[-1] += m
    a.append(a[-1])
    for i in range(d+1,N):
        if i+k-1<N:
            if S[i+k-1]=='M':
                m += 1
            elif S[i+k-1]=='C':
                c += 1
                a[-1] += m
        if S[i-1]=='M':
            m -= 1
            a[-1] -= c
        elif S[i-1]=='C':
            c -= 1
        if S[i]=='D':
            if i==e:
                break
            else:
                a.append(a[-1])
    ans.append(sum(a))
printrows(ans)




### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


N = readint()
S = readstr()
Q = readint()
K = readints()

if 'D' not in S:
    printrows([0]*Q)
    exit()
d = S.find('D')
e = S.rfind('D')
ans = []
for k in K:
    m = 0
    c = 0
    a = 0
    for i in range(d+1,min(N,d+k)):
        if S[i]=='M':
            m += 1
        if S[i]=='C':
            c += 1
            a += m
    b = a
    for i in range(d+1,N):
        if i+k-1<N:
            if S[i+k-1]=='M':
                m += 1
            elif S[i+k-1]=='C':
                c += 1
                a += m
        if S[i-1]=='M':
            m -= 1
            a -= c
        elif S[i-1]=='C':
            c -= 1
        if S[i]=='D':
            b += a
            if i==e:
                break
    ans.append(b)
printrows(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 6)

n = int(input())
edges = [list(map(int, input().split())) for _ in range(n - 1)]
bw = [-1 for i in range(n + 1)]
bw[1] = 0

def dfs(frm):
	tmp = []
	for e in edges[:]:
		if e[0] == frm:
			tmp.append([e[1], e[2]])
			edges.remove(e)
		elif e[1] == frm:
			tmp.append([e[0], e[2]])
			edges.remove(e)

	for t in tmp:
		if bw[t[0]] == -1:
			if t[1] % 2 == 0:
				bw[t[0]] = bw[frm]
			else:
				bw[t[0]] = abs(bw[frm] - 1)
			dfs(t[0])

dfs(1)

for i in range(1, n + 1):
	print(bw[i])

### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)

n = int(input())
edges = [list(map(int, input().split())) for _ in range(n - 1)]
bw = [-1 for i in range(n + 1)]
bw[1] = 0

new_edges = [[] for _ in range(n + 1)]

for e in edges:
	new_edges[e[0]].append([e[1], e[2]]) 
	new_edges[e[1]].append([e[0], e[2]]) 

def dfs(frm, now):
	for ne in new_edges[now]:
		if ne[0] == frm:
			continue
		else:
			if ne[1] % 2 == 0:
				bw[ne[0]] = bw[now]
			else:
				bw[ne[0]] = abs(1 - bw[now])
			dfs(now, ne[0])

dfs(0, 1)

for i in range(1, n + 1):
	print(bw[i])
### BUGGY CODE ###

import sys

hs = list(map(int, sys.stdin.read().strip().splitlines()[1].split(" ")))
if len(hs) == 1:
  print("Yes")
  sys.exit(0)

stack = [(hs[0], 1), (hs[0]-1, 1)]
while stack:
  pre, i = stack.pop()
  for v in (hs[i]-1, hs[i]):
    if v >= pre:
      if i == len(hs)-1:
        print("Yes")
        sys.exit(0)

      stack.append((v, i+1))
print("No")


### FIXED CODE ###

import sys

hs = list(map(int, sys.stdin.read().strip().splitlines()[1].split(" ")))
if len(hs) == 1:
  print("Yes")
  sys.exit(0)
if hs[0] > hs[1]:
  hs[0] -= 1

for i in range(1, len(hs)):
  pre = hs[i-1]
  diff = hs[i] - pre
  if diff >= 1:
    hs[i] -= 1
  elif diff < 0:
    print("No")
    sys.exit(0)
print("Yes")
### BUGGY CODE ###

n,a = map(int,input().split())
x = [int(i) for i in input().split()]
dp = [[[0]*(n+1) for _ in range(n*a+1)] for _ in range(n+1)] 
for k in range(n+1): dp[0][0][k] = 1

for i in range(1,n+1):
    for j in range(1,n*a+1):
        for k in range(1,n+1):
            dp[i][j][k] = dp[i][j][k-1]
            if j - x[k-1] >= 0:
                dp[i][j][k] += dp[i-1][j-x[k-1]][k-1]
ans = 0
for i in range(1,n+1): ans += dp[i][a*i][n]
print(ans)

### FIXED CODE ###

n,a = map(int,input().split())
x = [int(i) for i in input().split()]
dp = [[[0]*(n+1) for _ in range(n*a+1)] for _ in range(n+1)] 
for k in range(n+1): dp[0][0][k] = 1

for i in range(1,n+1):
    for j in range(1,n*a+1):
        for k in range(1,n+1):
            dp[i][j][k] = dp[i][j][k-1]
            if j - x[k-1] >= 0:
                dp[i][j][k] += dp[i-1][j-x[k-1]][k-1]
ans = 0
for i in range(1,n+1): ans += dp[i][a*i][n]
print(ans)
### BUGGY CODE ###

n = input()
S = list(map(int,input().split()))
n = input()
T = list(map(int,input().split()))
count =  0

def binarySearch(a,b):
    if a == b[-1]:
        return 1
    elif len(b) == 1:
        return 0
    elif a < b[int(len(b)/2)]:
        return binarySearch(a,  b[:int(len(b)/2)])
    elif a >= b[int(len(b)/2)]:
        return binarySearch(a,  b[int(len(b)/2):])

for i in T:
    count += binarySearch(i,S)
print(count)

### FIXED CODE ###

input()
S = list(map(int,input().split()))
input()
T = list(map(int,input().split()))

def binarySearch(k,a):
    left = 0
    right = len(a)
    while left < right:
        middle = (left + right) // 2
        if a[middle] == k:
            return 1
        elif a[middle] < k:
            left = middle+1
        elif a[middle] > k:
            right = middle 
    return 0


count = 0
for t in T:
    count += binarySearch(t,S)
print(count)
### BUGGY CODE ###

from operator import itemgetter

s=input()
k=int(input())

S=[]
for i in range(len(s)):
    S.append([s[i:len(s)],0,len(s)-i])

S.sort()

S[0][1]=S[0][2]
for i in range(len(S)-1):
    t=0
    while t<len(S[i][0]) and t<len(S[i+1][0]) and S[i][0][t]==S[i+1][0][t]:
        t+=1
    S[i+1][1]=S[i+1][2]-t

#print(S)




k-=1
i=0
while k>=S[i][1] :
    k-=S[i][1]
    i+=1

ans=S[i][0][0:(S[i][2]-S[i][1]+k+1)]

#print(i)

print(ans)


### FIXED CODE ###

s = input()
n = len(s)
k = int(input())

A = []
for i in range(n):
    A.append(s[i:])

A.sort()
# print(A)

B = []

for i in range(min(k, len(A))):
    for j in range(1, len(A[i])+1):
        B.append(A[i][:j])

B.sort()

# print(B)
co = 1
i = 1
mae = B[0]
while co < k:
    #print(i)
    if mae != B[i]:
        co += 1
        mae = B[i]
    i += 1
print(mae)
### BUGGY CODE ###

from collections import deque

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

G = dict()
for i in range(1, N + 1):
    G[i] = dict()

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

parents = [0] * (N + 1)
values = [0] * (N + 1)
values_sum = [-1] * (N + 1)

for _ in range(Q):
    p, x = map(int, input().split())
    values[p] += x

# solve
values_sum[1] = values[1]

queue = deque([1])
while len(queue) > 0:
    p = queue.popleft()
    for q in G[p].keys():
        if G[p][q] == 1:
            if values_sum[q] == -1:
                values_sum[q] = values_sum[p] + values[q]
                G[q][p] = 0
                queue.append(q)

print(*values_sum[1:])


### FIXED CODE ###

from collections import defaultdict

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

G = defaultdict(list)

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

parents = [0] * (N + 1)
values = [0] * (N + 1)
values_sum = [-1] * (N + 1)

for _ in range(Q):
    p, x = map(int, input().split())
    values[p] += x

# solve
values_sum[1] = values[1]

queue = [1]
while len(queue) > 0:
    p = queue.pop()
    for q in G[p]:
        if values_sum[q] == -1:
            values_sum[q] = values_sum[p] + values[q]
            queue.append(q)

print(*values_sum[1:])
### BUGGY CODE ###

class MyClass():
    def main(self):
        self.n = input()
        self.A = map(int,raw_input().split())
        self.q = input()
        self.M = map(int,raw_input().split())
        
        W = map(self.solve,self.M)
        for w in W:
            print w

    def solve(self,m):
        w = self.rec(m,self.A)
        if w:
            return "yes"
        else:
            return "no"

    def rec(self,m,A):
        if len(A) == 1:
            r1 = False
        else:
            r1 = self.rec(m,A[1:])
        
        m_new = m - A[0]
        if m_new < 0:
            r2 = False
        elif m_new == 0:
            r2 = True
        elif len(A) == 1:
            r2 = False
        else:
            r2 = self.rec(m_new,A[1:])
        
        if r1 or r2:
            return True
        else:
            return False

if __name__ == "__main__":
    MC = MyClass()
    MC.main()

### FIXED CODE ###

from collections import deque

def main():
    n = input()
    A = deque(map(int,raw_input().split()))
    q = input()
    M = map(int,raw_input().split())

    p = rec([0],A)

    for m in M:
        if m in p:
            print "yes"
        else:
            print "no"

def rec(a,A):
    if len(A) == 0:
        return a
    else:
        y = A.popleft()
        a = a+[x+y for x in a]
        return rec(a,A)

if __name__ == "__main__":
    main()
### BUGGY CODE ###

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

a = []
for _ in range(m):
  a.append(list(map(int,input().split())))
a.sort()

bridge = n
ans = 0
for i in range(m):
    count = True
    for j in range(a[m - i - 1][0],a[m - i - 1][1]):
        if j >= bridge:
            count = False
            break
            
    if count:
        ans += 1
        bridge = a[m - i - 1][0]

print(ans)

### FIXED CODE ###

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

a = []
for _ in range(m):
  a.append(list(map(int,input().split())))
a.sort()

bridge = n
ans = 0
for i in range(m):
    count = True
    if a[m - i - 1][1] > bridge:
        count = False
            
    if count:
        ans += 1
        bridge = a[m - i - 1][0]

print(ans)
### BUGGY CODE ###

MOD = 10**9 + 7
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
dp = [0] * (1 << N)
dp[0] = 1
prev = {0}
for i in range(N):
    temp = set([])
    for j in range(N):
        if A[i][j] != 1: continue
        bit = 1 << j
        for s in prev:
            if s & bit == 0:
                new_s = s | bit
                dp[new_s] += dp[s]
                dp[new_s] %= MOD
                temp |= {new_s}
    prev = temp
print(dp[(1 << N) - 1])

### FIXED CODE ###

MOD = 10**9 + 7
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
dp = [0] * (1 << N)
dp[0] = 1
bits = [1 << i for i in range(N)]
cands = [[] for _ in range(N+1)]
for s in range(1 << N):
    temp = 0
    for n in range(N):
        if s & (1 << n): temp += 1
    cands[temp].append(s)
for i, a in enumerate(A):
    for j in [k for k, v in enumerate(a) if v == 1]:
        bit = bits[j]
        for s in cands[i]:
            if s & bit == 0:
                new_s = s | bit
                dp[new_s] += dp[s]
                dp[new_s] %= MOD
print(dp[-1])
### BUGGY CODE ###

# alter altar
# WRとなっては行けない
# all W or all R 
# RRRRRWWWWWW 左にR、右にWを集める
length = int(input())
stone = input()
mini = 200000
for sep in range(length+1):
	red = 0
	white = 0
	for left in range(0, sep):
		# print(left, end="")
		if(stone[left] == "W"):
			white += 1
	for right in range(sep, length):
		# print(right, end="")
		if(stone[right] == "R"):
			red += 1
	# print(white)
	# print(red)
	trial = max(white, red)
	mini = min(mini, trial)
print(mini)

### FIXED CODE ###

# alter altar
length = int(input())
stone = input()
red = [0] * (length + 1)
white = [0] * (length + 1)
[red_num, white_num] = [0, 0]
for i in range(1, length + 1):
	if(stone[i-1] == "W"):
		white_num += 1
	if(stone[length-i] == "R"):
		red_num += 1
	white[i] = white_num
	red[length - i] = red_num
# print(white)
# print(red)
mini = 200000
for i in range(0, length+1):
	trial = max(white[i], red[i])
	mini = min(mini, trial)
print(mini)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = []
for i in range(1,N + 1):
    x = A.index(i) + 1
    ans.append(x)

print(*ans)




### FIXED CODE ###

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

for i in range(N):
    ans[A[i] - 1] = i + 1
print(*ans)
### BUGGY CODE ###

X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

AB = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)

AB = AB[:K]
C.sort(reverse=True)
ABC = []
for ab in AB:
    for c in C:
        ABC.append(ab+c)

ABC.sort(reverse=True)

for i in range(K): print(ABC[i])

### FIXED CODE ###

X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            if (i+1)*(j+1)*(k+1) <= K: ans.append(A[i]+B[j]+C[k])
            else: break
ans.sort(reverse=True)
for i in range(K): print(ans[i])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10000000)
# N = 4
# ARR = [10, 30, 40, 20]

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

def calculate(n, arr):
    result = aaa(0, n - 1, arr)
    print(result)


def aaa(currentPosition, endPosition, arr, cost=0):
    if currentPosition == endPosition:
        return cost
    if currentPosition == (endPosition - 1):
        return aaa(endPosition, endPosition, arr, cost + abs(arr[endPosition] - arr[currentPosition]))

    a1 = abs(arr[currentPosition + 1] - arr[currentPosition])
    a2 = abs(arr[currentPosition + 2] - arr[currentPosition])

    s1 = aaa(currentPosition + 1, endPosition, arr, cost + a1)
    s2 = aaa(currentPosition + 2, endPosition, arr, cost + a2)
    return min(s1, s2)





calculate(N, ARR)


### FIXED CODE ###

# N = 4
# HRR = [10, 30, 40, 20]
# N = 2
# HRR = [10, 10]

N = int(input())
HRR = list(map(int,input().split()))
result = []
for i in range(N):
    if i == 0:
        result.append(0)
    elif i == 1:
        s1 = result[i - 1] + abs(HRR[i] - HRR[i - 1])
        result.append(s1)
    else:
        s1 = result[i - 1] + abs(HRR[i] - HRR[i - 1])
        s2 = result[i - 2] + abs(HRR[i] - HRR[i - 2])
        result.append(min(s1, s2))

print(result[-1])
### BUGGY CODE ###

import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [set() for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a].add(b)
    graph[b].add(a)



# @debug
def dfs(v, pc=None, seen=None):
    seen = seen or set()

    if v in seen:
        return True
    seen.add(v)

    if pc is None or pc == S[v]:
        ok = False
        for u in graph[v]:
            if S[u] != S[v]:
                ok |= dfs(u, S[v], seen)
        if not ok:
            seen.remove(v)
            return False

    if pc is None or pc != S[v]:
        ok = False
        for u in graph[v]:
            if S[u] == S[v]:
                ok |= dfs(u, S[v], seen)
        if not ok:
            seen.remove(v)
            return False
    return True


ok = False
for v in range(N):
    ok |= dfs(v)
if ok:
    print('Yes')
else:
    print('No')


### FIXED CODE ###

import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')