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

import sys
F = sys.stdin
N, Q = map(int, F.readline().strip("\n").split())
s = F.readline().strip("\n")
O = [None] * Q
for i in range(Q):
    O[i] = list(map(str, F.readline().strip("\n").split()))

leftlow, lefthigh = 0, N
while lefthigh - leftlow > 1:
    mid = (lefthigh + leftlow)//2
    now = mid
    for place, move in O:
        if place == s[now]:
            if move == "L": now -= 1
            else: now += 1
            if now == -1 or now == N: break
    if now == -1:leftlow = mid
    else: lefthigh = mid

rightlow, righthigh = 0, N
while righthigh - rightlow > 1:
    mid = (righthigh + rightlow)//2
    now = mid
    for place, move in O:
        if place == s[now]:
            if move == "L":now -= 1
            else: now += 1
            if now == -1 or now == N: break
    if now == N:righthigh = mid
    else: rightlow = mid

print(righthigh - lefthigh)

### FIXED CODE ###

import sys
F = sys.stdin
N, Q = map(int, F.readline().strip("\n").split())
s = F.readline().strip("\n")
O = [None] * Q
for i in range(Q):
    O[i] = list(map(str, F.readline().strip("\n").split()))

leftlow, lefthigh = 0, N
while lefthigh - leftlow > 1:
    mid = (lefthigh + leftlow)//2
    now = mid
    for place, move in O:
        if place == s[now]:
            if move == "L": now -= 1
            else: now += 1
            if now == -1 or now == N: break
    if now == -1:leftlow = mid
    else: lefthigh = mid

rightlow, righthigh = 0, N
while righthigh - rightlow > 1:
    mid = (righthigh + rightlow)//2
    now = mid
    for place, move in O:
        if place == s[now]:
            if move == "L":now -= 1
            else: now += 1
            if now == -1 or now == N: break
    if now == N:righthigh = mid
    else: rightlow = mid

print(righthigh - lefthigh)
### BUGGY CODE ###

import math
l,r = list(map(int,input().split()))
MOD = 2019
res = 100000000000000000000000000000000
ans = []
for i in range(l,r+1):
    for j in range(i+1,r+1):
        if (i*j)%2019 < res:
            res = (i*j)%2019
print(res)


### FIXED CODE ###

import math
l,r = list(map(int,input().split()))
MOD = 2019
res = 100000000000000000000000000000000
ans = []
for i in range(l,r+1):
    if res == 0:
        break
    for j in range(i+1,r+1):
        if res == 0:
            break
        if (i*j)%2019 < res:
            res = (i*j)%2019
print(res)
### BUGGY CODE ###

def main():
    n=int(input())
    a=1
    m=int((2*n)**0.5)
    if m*(m+1)==n*2:
        a=m+1
        print("Yes")
    else:
        print("No")
        exit()
    print(a)
    s=set(range(1,1+n))
    l=[0]*(n+1)
    ans=[set()]

    for i in range(a):
        ll=set()
        for j in ans[:i+1]:
            for p in s&j:
                if p not in ll:
                    ll.add(p)
                    if l[p]==1:
                        s.remove(p)
                    else:
                        l[p]+=1
                    break

        for p in s:
            if len(ll)>=n*2//a:
                break
            else:
                if l[p]==0:
                    ll.add(p)
                    l[p]+=1

        ans.append(ll)
    for i in ans[1:]:
        print(n*2//a,*i)


if __name__ == '__main__':
    main()








### FIXED CODE ###

def main():
    n=int(input())
    m=int((2*n)**0.5)
    if m*(m+1)==n*2:
        print("Yes")
        m+=1
        print(m)
    else:
        print("No")
        exit()
    
    ans=[[m-1] for i in range(m)]
    c=1
    for i in range(m):
        for j in range(i+1,m):
            ans[i].append(c)
            ans[j].append(c)
            c+=1
    for i in ans:
        print(*i)


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

import itertools
import math
n = int(input())

lst = [_ for _ in range(1, n+1)]
lst = list(itertools.product(lst, repeat=3))

ans = sum([math.gcd(math.gcd(_[0],_[1]),_[2]) for _ in lst])

print(ans)

### FIXED CODE ###

import itertools
import math
n = int(input())

lst = [_ for _ in range(1, n+1)]
lst2 = list(itertools.product(lst, repeat=2))

lst3 = [math.gcd(_[0], _[1]) for _ in lst2]

lst4 = list(itertools.product(lst3, lst))

lst5 = [math.gcd(_[0], _[1]) for _ in lst4]

print(sum(lst5))
### BUGGY CODE ###

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

for x in range(0, M):
  index = A.index(max(A))
  A[index] = A[index] // 2

print(sum(A))

### FIXED CODE ###

import heapq

N, M = map(int, input().split())
A = [-int(A) for A in input().split()]
heapq.heapify(A)

for x in range(M):
  most = -heapq.heappop(A)
  most //= 2
  heapq.heappush(A, -most)

print(-sum(A))
### BUGGY CODE ###

Row = int(input())
List = list(input() for i in range(Row))
List.sort()
a=Row-1
while a>0:
 if List[a] == List[a-1]:
  List.remove(List[a])
  a-=1
 else:a-=1
else:print(len(List)) 

### FIXED CODE ###

Row = int(input())
List = list(input() for i in range(Row))
print(len(set(List)))
### BUGGY CODE ###

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

nums = A[:]
import fractions

gcd_queue = [
    (nums[0], True),
    (nums[1], True),
    (fractions.gcd(nums[0], nums[1]), False),
]
for n in nums[2:]:
    tmp_queue = []
    for q in gcd_queue:
        if q[1] == True:
            tmp_queue.append((fractions.gcd(q[0], n), True))
        else:
            tmp_queue.append((fractions.gcd(q[0], n), False))
            tmp_queue.append((q[0], True))
    gcd_queue = tmp_queue[:]
#print(gcd_queue)

ans = 0
for q in gcd_queue:
    ans = max(ans, q[0])
print(ans)

### FIXED CODE ###

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

nums = A[:]
import fractions

gcd_queue = [
    (nums[0], True),
    (nums[1], True),
    (fractions.gcd(nums[0], nums[1]), False),
]
for n in nums[2:]:
    tmp_queue = []
    for q in gcd_queue:
        if q[1] == True:
            tmp_queue.append((fractions.gcd(q[0], n), True))
        else:
            tmp_queue.append((fractions.gcd(q[0], n), False))
            tmp_queue.append((q[0], True))
    gcd_queue = set(tmp_queue[:])

ans = 0
for q in gcd_queue:
    ans = max(ans, q[0])
print(ans)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
S=input()
 
sum=0
judge=True

i=0
while i<len(S)-1:
    if S[i:i+2]=="BW":
        S=S[:i]+"WB"+S[i+2:]
        sum+=1
        i-=2
    i=max(0,i+1)
    
print(sum)


### FIXED CODE ###

# coding: utf-8
# Your code here!
S=input()
N=len(S)

sum=0
k=0

for i in range(N):
    if S[i]=="W":
        sum+=(i-k)
        k+=1

print(sum)
### BUGGY CODE ###

def inverse(a,mod): #aのmodを法にした逆元を返す
    return pow(a,mod-2,mod)

N = int(input())

A = list(map(int,input().split()))
A.sort()

mod = 10**9 + 7

s = [2]
now = 3
dic = {}

while now ** 2 <= 10 ** 6:

    flag = True
    for i in s:
        if now % i == 0:
            flag = False
            break

    if flag:
        s.append(now)
    now += 2

for i in s:
    dic[i] = 0

for i in range(N):
    if i > 0 and A[i] == A[i-1]:
        continue
    a = A[i]

    for j in s:
        cnt = 0
        while a % j == 0:
            cnt += 1
            a //= j

        dic[j] = max(dic[j],cnt)

    if a != 1:
        s.append(a)
        dic[a] = 1

lcm = 1

for i in s:
    for j in range(dic[i]):
        lcm *= i

ans = 0
g = 0
for i in A:
    g += inverse(i,mod)
    g %= mod

ans += lcm * g
ans %= mod
print (ans)

### FIXED CODE ###

import fractions

def lcm(x,y):
  return x // fractions.gcd(x,y) * y


mod = 10**9+7


N = int(input())

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

B = 0
for i in A:
    B += pow(i,mod-2,mod)
    B %= mod

g = 1

for i in range(N):
    g = lcm(g,A[i])

print (g * B % mod)
### BUGGY CODE ###

from itertools import combinations as comb

n, k = map(int, input().split())
p_list = []
for i in range(n):
    p = int(input())
    p_list.append(p)

comb_list = list(comb(p_list, k))
min = 10000000000000000
for c in comb_list:
    c = sorted(list(c))
    if min > c[-1]-c[0]:
        min = c[-1]-c[0]

print(min)

### FIXED CODE ###

n, k = map(int, input().split())
h_list = []
for i in range(n):
    h_list.append(int(input()))
h_list.sort()
min = h_list[-1]
for j in range(n-k+1):
    if min > h_list[j+k-1] - h_list[j]:
        min = h_list[j+k-1] - h_list[j]

print(min)
### BUGGY CODE ###

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

ans = 0

for i in range(N):
    for j in range(N-i-1):
        ans += As[i]*As[j+i+1]
        if ans >= 10**9+7:
            ans = ans%(10**9+7)

print(ans)


### FIXED CODE ###

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

sum = 0
for i in range(N):
    sum += As[i]
    sum %= 10**9+7

ans = 0
for i in range(N):
    sum -= As[i]
    ans += As[i]*sum
    ans %= 10**9+7

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

import collections
s = input()
p = 0
dp = [0]
for i in range(len(s)):
    p = (p + (int(s[-1 - i]) * (10 ** (i)))) % 2019
    dp.append(p % 2019)
t = collections.Counter(dp)
ans = 0
for i in t.values():
    ans += i * (i - 1) / 2
print(int(ans))

### FIXED CODE ###

s = input()[::-1]
p = 0
dp = [0] * 2019
dp[0] = 1
d = 1
for i in s:    
    p = (int(i) * d + (p)) % 2019
    dp[p] += 1
    d *= 10
    d = d % 2019
ans = 0
for i in dp:
    ans += i * (i - 1) // 2
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
count = 0
for i in range(0,1000):
  if i <= 9:
    j = "00"+str(i)
  elif i <= 99:
    j = "0"+str(i)
  else:
    j = str(i)
  check = False
  for k in range(N-2):
    if S[k] == j[0]:
      for n in range(k+1,N-1):
        if S[n] == j[1]:
          for m in range(n+1,N):
            if S[m] == j[2]:
              count += 1
              check = True
              break
          if check:
            break
      if check:
        break
print(count)   

### FIXED CODE ###

N = int(input())
S = input()
count = 0
for i in range(0,1000):
  if i >= 100:
    t = str(i)
  elif i >= 10:
    t = "0"+str(i)
  else:
    t = "00"+str(i)
  ti = 0
  for j in range(N):
    if S[j] == t[ti]:
      ti += 1
      if ti == 3:
        count += 1
        break
print(count)
### BUGGY CODE ###

N = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(N):
    ans *= a[i]

if ans > 10**18:
    ans = -1
print(ans)

### FIXED CODE ###

N = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(N):
    if a[i] == 0:
        ans = 0
if ans != 0:
    for i in range(N):
        ans *= a[i]
        if ans > 10**18:
            ans = -1
            break

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

import collections
def cal(n):
    ans = int(n*(n-1)/2)
    return ans 
n = int(input())
a = list(map(int,input().split()))

temp_list = []
for i in range(1,n+1):
    temp = a[:i-1]+a[i:]
    # print(temp)
    temp_c = collections.Counter(temp)
    temp_k = temp_c.values()
    temp_cal = list(map(cal,temp_k))
    print(sum(temp_cal))

### FIXED CODE ###

import collections
def cal(n):
    ans = int(n*(n-1)/2)
    return ans 
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
temp = list(c.values())
t_val = list(map(cal,c.values()))
t_key = list(c.keys())
t_cal = sum(t_val)
# print(t_key)
# print(t_cal)
# print(temp)
for i in range(n):
    ans1 = c[a[i]]-1
    print(t_cal - ans1)
### BUGGY CODE ###

S=input()
Q=int(input())
sousa=[list(map(str,input().split())) for i in range(Q)]

for i in sousa:
    if i[0]=='1':
        S=S[::-1]
    if i[0]=='2':
        if i[1]=='1':
            S=i[2]+S
        if i[1]=='2':
            S=S+i[2]
print(S)

### FIXED CODE ###

S=input()
Q=int(input())
sousa=[list(map(str,input().split())) for i in range(Q)]
counter=0
up=''
down=''
for i in sousa:
    if i[0]=='1':
#         S=S[::-1]
        counter+=1
    if i[0]=='2':
        if counter%2==0:
            if i[1]=='1':
                up=i[2]+up
            if i[1]=='2':
                down=down+i[2]
        else:
            if i[1]=='1':
                down=down+i[2]
            if i[1]=='2':
                up=i[2]+up   
                
S=up+S+down
if counter%2==1:
    S=S[::-1]

print(S)
### BUGGY CODE ###

import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    a_list = MI()

    c = Counter(a_list)
    n_key = len(c)
    n_rewrite = 0
    while n_key > k:
        n_rewrite += c.pop(c.most_common()[-1][0])
        n_key -= 1

    print(n_rewrite)
    pass


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    a_list = MI()

    c = Counter(a_list)
    sum = 0
    if len(c) <= k:
        print("0")
        exit()

    for l in c.most_common()[k:]:
        sum += l[1]
    print(sum)
    pass


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

import copy
import math
n,m=map(int,input().split())

a=list(map(int,input().split()))
for i in range(m):
  a.sort(reverse=True)
  a[0]=a[0]/2
total=0  
for i in range(n):
  total+=math.floor(a[i])
print (total)



### FIXED CODE ###

import copy
import heapq
import math
n,m=map(int,input().split())
a=[0]*n
atmp=list(map(int,input().split()))
for i in range(n):
  a[i]=-atmp[i]

heapq.heapify(a)
for i in range(m):
  tmp=heapq.heappop(a)
  heapq.heappush(a,tmp/2)

total=0  
for i in range(n):
  total+=math.ceil(a[i])
print (-total)
### BUGGY CODE ###

N = int(input())
S = input()
count = 0
def findlet(let,ind,checker):
    add = S[ind:].count(let)
    if add > 0:
        if ind + checker < N:
            if S[ind+checker] == let:
                add -= 1
        global count
        count += add
for i in range(N-2):
    st = S[i:]
    if st[0] == "R":
        for j in [x for x, y in enumerate(st) if y == 'G']:
            findlet("B",i+j,j)
        for j in [x for x, y in enumerate(st) if y == 'B']:
            findlet("G",i+j,j)
    elif st[0] == "G":
        for j in [x for x, y in enumerate(st) if y == 'B']:
            findlet("R",i+j,j)
        for j in [x for x, y in enumerate(st) if y == 'R']:
            findlet("B",i+j,j)
    else:
        for j in [x for x, y in enumerate(st) if y == 'R']:
            findlet("G",i+j,j)
        for j in [x for x, y in enumerate(st) if y == 'G']:
            findlet("R",i+j,j)
print(count)

### FIXED CODE ###

N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
count = r * g * b
for i in range(N):
    for j in range(i+1,N-1):
        if 2*j - i <= N-1:
            l = set([S[i],S[j],S[2*j-i]])
            if len(l) == 3:
                count -= 1
print(count)
### BUGGY CODE ###

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

def func(a,b):
    for i in range(21):
        if (a>>i)&(b>>i): #AND回路
            return False
    return True

ans = 0
i = 0
j = 1

while i < n:
    host = lst1[i]
    while j < n:
        if not func(host,lst1[j]):
            break
        host = host^lst1[j]
        j += 1
        ans += 1
    i += 1
    j = i+1

ans += n

print(ans)

### FIXED CODE ###

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

def func(a,b):
    for i in range(21):
        if (a>>i)&(b>>i): #AND回路
            return False
    return True

def add_point(length):
    res = 0
    for i in range(1,length):
        res += i
    return res

a,b,count,ans = 0,1,0,0
host = lst1[0]
#尺取法
while True:
    if b >= n and a >= n:
        break
    while  b<n and func(host,lst1[b]):
        host = host^lst1[b]
        b += 1
    host = host^lst1[a] #使わなくなる部分を引く
    if b -a > 1:
        ans += b-a-1
    a += 1

ans += n
print(ans)
### BUGGY CODE ###

num = input()
data = input().rstrip().split(" ")
ans = 1
for i in data:
    ans = ans*int(i)
if ans > 1000000000000000000:
    ans = -1
print(ans)

### FIXED CODE ###

num = input()
data = input().rstrip().split(" ")
ans = 1
if "0" in data:
    ans = 0
else:
    for i in data:
        ans = ans*int(i)
        if ans > 1000000000000000000:
            ans = -1
            break
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
dp = {}
si = set()
for i in range(N-2):
  if i in si:
    continue
  si.add(i)
  for j in range(i+1, N-1):
    if dp.get(S[i] + S[j], 0) > 0:
      continue
    dp[S[i] + S[j]] = len(set([S[k] for k in range(j+1, N)]))
print(sum(dp.values()))

### FIXED CODE ###

N = int(input())
S = input()
dp = {}
si = set()
for i in range(N-2):
  if S[i] in si:
    continue
  si.add(S[i])
  for j in range(i+1, N-1):
    if dp.get(S[i] + S[j], 0) > 0:
      continue
    dp[S[i] + S[j]] = len(set(S[j+1:]))
print(sum(dp.values()))
### BUGGY CODE ###

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

s, = read_line(str)

s = [int(c) for c in s]

c = 0
while True:
    i = 0
    while i < len(s) - 1:
        if s[i] != s[i + 1]:
            s[i] = s[i + 1] = -1
            i += 2
            c += 2
        i += 1
    s = [c for c in s if c >= 0]
    l = len([c for c in s if c == 0])
    if l == 0 or l == len(s):
        break

print(c)


### FIXED CODE ###

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

s, = read_line(str)

s = [int(c) for c in s]

c = 0

ss = 0

for i in range(0, len(s)):
    if s[i] == 1:
        if ss < 0:
            c += 2
        ss += 1
    else:
        if ss > 0:
            c += 2
        ss -= 1
print(c)
### BUGGY CODE ###

def partition(A, p, r):
    x = int(A[r-1][1])
    i = p-1

    for j in range(p,r-1):
        if int(A[j][1]) <= x:
            i = i + 1
            A[i],A[j] = A[j],A[i]
    A[i+1],A[r-1] = A[r-1],A[i+1]
    return i + 1

def qsort(A,p,r):
    if p < r:
        q = partition(A,p,r)
        qsort(A,p,q)
        qsort(A,q+1,r)
    return A

def isStable(a, b):
    length = int(len(a))
    for i in range(length):
        for j in range(i+1,length-1) :
            for x in range(length):
                for y in range(x+1,length-1) :
                    if a[i][1]==a[j][1] and a[i] == b[y] and a[j] == b[x]:
                        return False
                        break
    return True

if __name__ == '__main__':
    cnt = int(input())
    A = []
    for i in range(cnt):
        A.append(input().split())
    A2 = A[:]
    p = 0
    r = len(A)
    B = qsort(A,p,r)
    if isStable(A2,B):
        print("Stable")
    else:
        print("Not stable")
    for i in B:
        print ("{0} {1}".format(i[0],i[1]))

### FIXED CODE ###

def partition(A, p, r):
    x = int(A[r-1][1])
    i = p-1

    for j in range(p,r-1):
        if int(A[j][1]) <= x:
            i = i + 1
            A[i],A[j] = A[j],A[i]
    A[i+1],A[r-1] = A[r-1],A[i+1]
    return i + 1

def qsort(A,p,r):
    if p < r:
        q = partition(A,p,r)
        qsort(A,p,q)
        qsort(A,q+1,r)
    return A

def isStable(a):
    length = int(len(a))
    for i in range(length):
        for j in range(i+1,length-1) :
            if a[i][1] == a[j][1] and a[i][2] > a[j][2]:
                return False
                break
    return True

if __name__ == '__main__':
    cnt = int(input())
    A = []
    for i in range(cnt):
        N = input().split()
        N.append(i)
        A.append(N)
    p = 0
    r = len(A)
    B = qsort(A,p,r)
    if isStable(B):
        print("Stable")
    else:
        print("Not stable")
    for i in B:
        print ("{0} {1}".format(i[0],i[1]))
### BUGGY CODE ###



n = int(input())

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

count = 0
for i in range(1, n + 1):
    count += i * len(make_divisors(i))
print(count)

### FIXED CODE ###

n = int(input())
def div(n):
    a = 0
    for i in range(1, n+1):
        y = int(n/i)
        a += y* (y+1)*i/2
    return int(a)
print(div(n))
### BUGGY CODE ###

class BIT(object):
    def __init__(self, N, init=0, mod=1):
        self.N = N
        self.bit = [init for _ in range(N)]
        self.mod = mod

    def add(self, k, w):
        x = k
        while x < self.N:
            self.bit[x] += w % self.mod
            self.bit[x] %= self.mod
            x |= x + 1

    def sum(self, k):
        s = 0
        x = k - 1
        while x >= 0:
            s += self.bit[x]
            s %= self.mod
            x = (x & (x + 1)) - 1
        return s

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

bit = BIT(k + 1, mod=MOD)
bit.add(0, 1)

for ai in a:
    for i in range(k, 0, -1):
        if ai >= i:
            x = bit.sum(i)
        else:
            x = bit.sum(i) - bit.sum(i - ai)
        bit.add(i, x % MOD)
print(bit.sum(k + 1) - bit.sum(k))


### FIXED CODE ###

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

dp = [0 for _ in range(k + 1)]
dp[0] = 1
b = [1 for _ in range(k + 2)]
b[0] = 0

for ai in a:
    for i in range(k, -1, -1):
        dp[i] += (b[i] - b[i - min(ai, i)]) % MOD
        dp[i] %= MOD
    for i in range(1, k + 2):
        b[i] = (b[i - 1] + dp[i - 1]) % MOD
        b[i] %= MOD
print(dp[-1])
### BUGGY CODE ###

import numpy as np
from fractions import gcd

MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))

x = 1
ans = 0
for a in A:
    tmp = gcd(x, a)
    # ans += (i) * A[i] // tmp + A[i] * x //tmp  
    x = (x * a) // tmp
    
b = 0
for a in A:
    b += x // a
    
print(b%MOD)


### FIXED CODE ###

import fractions 
#from functools import reduce
import functools

MOD = 10**9+7


def main():
    # N = int(input())
    # A = list(map(int, input().split()))
    N, *A = map(int, open(0).read().split())
            
    x = 1
    for a in A:
        x *= a //  fractions.gcd(x, a)
        
    b = sum(x//a for a in A)
    print(b%MOD)
    
if __name__== "__main__":
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
D = list(map(int, input().split()))
UB = [list(map(int, input().split())) for _ in range(M)]

LL = [[-1] * 2 for _ in range(N)]
for i in range(M):
  a, b = UB[i]
  a -= 1
  b -= 1
  if D[a] < D[b]:
    LL[b] = [a, i]
  elif D[a] > D[b]:
    LL[a] = [b, i]
  else:
    LL[a] = [b, i]
    LL[b] = [a, i]

DD = [[0] * 2 for _ in range(N)]
for i in range(N):
  DD[i][0] = D[i]
  DD[i][1] = i
DD.sort()
L = [0] * N
H = [0] * M

for i in range(N):
  h, x = DD[i]
  if L[x] == 0:
    if LL[x][0] == -1:
      print(-1)
      exit()
    else:
      n, m = LL[x]
      H[m] = h
      if L[n] == 1:
        L[n] = 1
        L[x] = 2
      else:
        L[n] = 2
        L[x] = 1

t = ""
for i in range(N):
  if L[i] == 1:
    t += "B"
  else:
    t += "W"
print(t)
for i in range(M):
  if H[i] == 0:
    print(10 ** 9)
  else:
    print(H[i])


### FIXED CODE ###

import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
D = list(map(int, input().split()))
UB = [list(map(int, input().split())) for _ in range(M)]

LL = [[-1] * 2 for _ in range(N)]
for i in range(M):
  a, b = UB[i]
  a -= 1
  b -= 1
  if D[a] < D[b]:
    LL[b] = [a, i]
  elif D[a] > D[b]:
    LL[a] = [b, i]
  else:
    LL[a] = [b, i]
    LL[b] = [a, i]

DD = [[0] * 2 for _ in range(N)]
for i in range(N):
  DD[i][0] = D[i]
  DD[i][1] = i
DD.sort()
L = [0] * N
H = [0] * M

for i in range(N):
  h, x = DD[i]
  if L[x] == 0:
    if LL[x][0] == -1:
      print(-1)
      exit()
    else:
      n, m = LL[x]
      H[m] = h
      if L[n] == 1:
        L[n] = 1
        L[x] = 2
      else:
        L[n] = 2
        L[x] = 1

for i in range(N):
  if L[i] == 1:
    L[i] = "B"
  else:
    L[i] = "W"
print("".join(list(map(str, L))))
for i in range(M):
  if H[i] == 0:
    print(10 ** 9)
  else:
    print(H[i])