Harryxun/sft_time huggingface dataset:
### 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, K = list(map(int, sys.stdin.buffer.readline().split()))
a = K
b = N - K
c = 0
d = min(a, b)
while not a == b == c:
    a, b, c = sorted([a, b, c])
    if a != b:
        d = b - a
    a += d
print(a + b + c)


### FIXED 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, K = list(map(int, sys.stdin.buffer.readline().split()))

a = K
b = N - K
c = 0
d = min(a, b)
while not a == b == c:
    a, b, c = sorted([a, b, c])
    if a != b:
        d = b - a
    cnt = (c - b) // d
    b += d * cnt
    a += d * cnt + d
print(a + b + c)
### BUGGY CODE ###

n = int(input())

ab = [list(map(int, input().split())) for _ in range(n)]

x = ab[0][0]
y = ab[0][1]
j = 1
for i in range(1,n):
    flg = True
    xx = ab[i][0]
    yy = ab[i][1]
    while flg:
        if x> j*xx or y > j*yy:
            j +=1
        else:
            x = xx *j
            y = yy *j
            flg =False
    j = 1

print(x+y)

### FIXED CODE ###

n = int(input())

ab = [list(map(int, input().split())) for _ in range(n)]

x = ab[0][0]
y = ab[0][1]
j = 1
flg = True
for i in range(1,n):
    xx = ab[i][0]
    yy = ab[i][1]
    j = max(x//xx,y//yy)
    while flg:
        if x> j*xx or y > j*yy:
            j +=1
        else:
            x = xx *j
            y = yy *j
            break

print(x+y)
### BUGGY CODE ###

s=str(input())

p1 = '01'
p2 = '10'
s = s[::-1]
c = 0
flag = True
while flag:
  flag = False
  for i in range(len(s)):
    if s[i:i+2] in [p1,p2]:
      s = s[:i] + s[i+2:]
      c += 2
      flag = True
      break
print(c)

### FIXED CODE ###

s=str(input())
c1 = 0
c0 = 0
for i in range(len(s)):
  if s[i] == '1':
    c1 += 1
  if s[i] == '0':
    c0 += 1
print(2*min(c1,c0))
### BUGGY CODE ###

n=int(input())
for i in range(1,3501):
  for j in range(1,3501):
    p=4*i*j-n*i-n*j
    if p>0:
      q=n*i*j
      if q%p==0:
        print(i,j,q//p)
        exit()

### FIXED CODE ###

n=int(input())
for i in range(1,3501):
  for j in range(1,3501):
    if (4*i*j-n*i-n*j)!=0:
      if (n*i*j)%(4*i*j-n*i-n*j)==0 and (n*i*j)//(4*i*j-n*i-n*j)>0:
        k=(n*i*j)//(4*i*j-n*i-n*j)
        break
  else:
    continue
  break
print(i,j,k)
### BUGGY CODE ###

N = int(input())
li = list(map(int,input().split()))
A = 1
if 0 in li:
  print(0)
else:
    for i in range (N):
      A = A * li[i]
      i += 1
    if A <= 1000000000000000000:
        print(A)
    else: 
        print (-1)

### FIXED CODE ###

N = int(input())
li = list(map(int,input().split()))
A = 1
i = 0
B = 0
if 0 in li:
  print(0)
else:
    for i in range (N):
      A = A * li[i]
      if (A > 10**18):
        break
      else: 
        B += 1
        i += 1
    if B == N:
      print (A)
    else:
      print (-1)
### BUGGY CODE ###

n = int(input())
d = list(map(int, input().split()))
ans = 0
for i in range(max(d)):
	a = sum(x > i for x in d)
	if a == (n/2):
		ans += 1
print(ans)


### FIXED CODE ###

n = int(input())
d = list(map(int, input().split()))
d.sort()
print(d[n//2]-d[(n//2)-1])
### BUGGY CODE ###

N, K = map(int, input().split())
*P, = map(int, input().split())
chk = 0
start_idx, end_idx = 0, 0
for i in range(N-K+1):
    if chk <= sum(P[i:i+K]):
        chk = sum(P[i:i+K])
        start_idx = i
        end_idx = i+K
ans = 0
for i in range(start_idx, end_idx):
    ans += (P[i] * (P[i] + 1) / 2) / P[i]
print(ans)


### FIXED CODE ###

N, K = map(int, input().split())
*P, = map(int, input().split())
cum = [0] * (N + 1)
for i in range(N):
    cum[i+1] = cum[i] + (P[i] * (P[i] + 1) / 2) / P[i]
ans = 0
for i in range(N-K+1):
    ans = max(ans, cum[i+K] - cum[i])
print(ans)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
dl=[]
dr=[]

for i in range(Q):
    line = list(map(int, input().split()))
    dl.append(line[0])
    dr.append(line[1])

s=[]
for i in range(N+1):
    s.append(S[:i].count('AC'))

ans=[]
for l, r in zip(dl, dr):
    ans.append(s[r]-s[l])

for i in range(Q):
    print(ans[i])

### FIXED CODE ###

##初期値設定##
N, Q = map(int, input().split())
S = input()
dl=[]
dr=[]
for i in range(Q):
    line = list(map(int, input().split()))
    dl.append(line[0])
    dr.append(line[1])

##ACの個数配列計算##
a=[]
for i in range(N-1):
    tmp_S = S[i:i+2]
    if tmp_S == "AC":
        a.append(1)
    else:
        a.append(0)

##累積和計算##
s = [0]
for i in range(len(a)):
    s.append(s[i]+a[i])

##解答算出##
ans=[]
for l, r in zip(dl, dr):
    ans.append(s[r-1]-s[l-1])

for i in range(Q):
    print(ans[i])
### BUGGY CODE ###

N, Q = map(int, input().split())
tree = list()
ope = list()
cnt = [0] * N
for i in range(N-1):
    tree.append(list(map(int, input().split())))
for i in range(Q):
    ope.append(list(map(int, input().split())))

#print(tree, ope)

for i in range(Q):
    vertex = ope[i][0]
    add = ope[i][1]
    parent = list()
    parent.append(vertex)
    for j in range(N-1):
        #print(j, tree[j][0], parent)
        if tree[j][0] in parent:
            parent.append(tree[j][1])
        if tree[j][1] in parent:
            cnt[j+1] += add
    if vertex == 1:
        cnt[0] += add
#print(tree)
for i in cnt:
  print(i, end =" ")


### FIXED CODE ###

N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N-1)]
px = [list(map(int, input().split())) for i in range(Q)]
cnt = [0] * N

for p, x in px:
    cnt[p-1] += x
for a, b in sorted(ab):
    cnt[b-1] += cnt[a-1]

print(*cnt)
### BUGGY CODE ###

# 上からk番目の数がxである。
# xとなる数はk個以上　かつ　x＋1となる数はk-1個以下
# xとなる数がk個以上となるxの最大値
from bisect import bisect_left, bisect_right
import sys

input = sys.stdin.buffer.readline


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

ng = -(10 ** 20)
ok = 10 ** 20


def count(x):
    cnt = 0
    for i in range(n):
        a = A[i]
        if a > 0:
            gendo = x // a
            pos = bisect_right(A, gendo)
            cnt += pos
            if pos >= i + 1:
                cnt -= 1
        elif a < 0:
            gendo = (-x + (-a - 1)) // (-a)
            pos = bisect_left(A, gendo)
            cnt += n - pos
            if pos <= i:
                cnt -= 1
        else:
            if x < 0:
                cnt += 0
            else:
                cnt += n - 1
    return cnt // 2


# x = mid 以上のものをカウントしたい
kouho = -(10 ** 10)
while ok - ng > 1:
    mid = (ok + ng) // 2
    cnt = count(mid)
    if cnt >= k:
        ok = mid
    else:
        ng = mid

print(ok)


### FIXED CODE ###

# 小さい方からk番目がx
# x以下がk個以上となる最小のx
# 負の数のところでバグってそう
from bisect import bisect_left, bisect_right

import sys

input = sys.stdin.buffer.readline

n, k = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
minusA = bisect_left(A, 0)
mA = A[:minusA]

zeroA = bisect_right(A, 0) - bisect_left(A, 0)
plusA = n - minusA - zeroA
pA = A[-plusA:]
minus = minusA * plusA
plus = plusA * (plusA - 1) // 2 + minusA * (minusA - 1) // 2
zero = zeroA * (zeroA - 1) // 2 + (n - zeroA) * zeroA


# x以下の数を数える
def calc(x):
    mmA = mA[::-1]
    res = 0
    if x > 0:
        lim = plusA - 1
        for i in range(plusA):
            while lim >= 0:
                if pA[i] * pA[lim] > x:
                    lim -= 1
                else:
                    break
            res += lim + 1
            if lim >= i:
                res -= 1
        lim = minusA - 1
        mmA = mA[::-1]
        for i in range(minusA):
            while lim >= 0:
                if mmA[i] * mmA[lim] > x:
                    lim -= 1
                else:
                    break
            res += lim + 1
            if lim >= i:
                res -= 1
        res //= 2
        res += minus + zero
    elif x == 0:
        res += minus + zero
    else:
        # 正の数、左絶対値小さい
        # 負の数、左絶対値大きい
        lim = minusA - 1
        for i in range(plusA):
            while lim >= 0:
                if pA[i] * mmA[lim] <= x:
                    lim -= 1
                else:
                    break
            res += minusA - (lim + 1)
    return res


ng = -(10 ** 20)
ok = 10 ** 20
while ok - ng > 1:
    mid = (ok + ng) // 2
    if calc(mid) >= k:
        ok = mid
    else:
        ng = mid

print(ok)
### BUGGY CODE ###

import bisect

N = int(input())
A = sorted([int(i) for i in input().split()])
B = sorted([int(i) for i in input().split()])
C = sorted([int(i) for i in input().split()])

cnt = 0
for i in range(N):
    x = bisect.bisect_right(B, A[i])
    for j in B[x:]:
        y = bisect.bisect_right(C, j)
        cnt += N - y
        
print(cnt)

### FIXED CODE ###

import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    ans += bisect.bisect_left(A, b) * (n - bisect.bisect_right(C, b))
    
print(ans)
### BUGGY CODE ###

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[(i-1)%N] - B[(i+1)%N]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    a = A[i]
    if B[i] == a: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < a: continue
    k = (B[i]-a) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    nb = B[i]
    if a <= nb - l - r:
        heapq.heappush(hq, (-nb,i))
    ll = B[(i-2)%N]
    rr = B[(i+2)%N]
    if A[(i+1)%N] <= r - nb - rr:
        heapq.heappush(hq, (-r,(i+1)%N))
    if A[(i-1)%N] <= l - nb - ll:
        heapq.heappush(hq, (-l,(i-1)%N))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

### FIXED CODE ###

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)
### BUGGY CODE ###

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

broken_step = []
for _ in range(M):
    broken_step.append(int(input()))
    
import sys
sys.setrecursionlimit(10000000)

mod = 1000000007

memo = [-1] * (N+1)

if 0 not in broken_step:
    memo[0] = 1
elif N == 0 and N in broken_step:
    memo[0] = 0

if 1 not in broken_step:
    memo[1] = 1
elif 1 in broken_step:
    memo[1] = 0

def step_memo(N):
    
    if N in broken_step:
        memo[N] = 0
        return 0
        
    if memo[N] != -1:
        return memo[N]
    
    memo[N] =  (step_memo(N-1) + step_memo(N-2)) % mod
    return (step_memo(N-1) + step_memo(N-2)) % mod
  
print(step_memo(N))


### FIXED CODE ###

N, M = map(int, input().split())
import sys
sys.setrecursionlimit(10000000)

mod = 1000000007

memo = [-1] * (N+1)

for _ in range(M):
    broken_step = int(input())
    memo[broken_step] = 0
    
memo[0] = 1
if memo[1] == -1:
    memo[1] = 1

def step_memo(N):
    
    if memo[N] != -1:
        return memo[N]
    
    memo[N] =  (step_memo(N-1) + step_memo(N-2)) % mod
    return (step_memo(N-1) + step_memo(N-2)) % mod
  
print(step_memo(N))
### BUGGY CODE ###

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

dic=dict()

from math import factorial

for i in range(N):
    dic[A[i]]=[i,-1]
for j in range(M):
    if B[j] in dic.keys():
        dic[B[j]]=[dic[B[j]][0],j]
    else:
        dic[B[j]]=[-1,j]

row=0
col=0
cells=0
ans=1
k = dic.keys()
kmin=min(k)

for i in range(N*M):
    n=N*M-i
    if n in k:
        d = dic[n]
        if d[0]>=0 and d[1]>=0:
            row+=1
            col+=1
            cells+=max(row+col-2,0)
        elif d[0]==-1:
            ans*=row
            cells+=max(row-1,0)
            col+=1
        else:
            ans*=col
            cells+=max(col-1,0)
            row+=1
    else:
        if n >= kmin:
            ans*=cells
            cells-=1
        else:
            ans*=n

if len(set(A))==N and len(set(B))==M:
    print(ans%(10**9+7))
else:
    print(0)




### FIXED CODE ###

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

l=[[0,0] for i in range(N*M+1)]

for a in A:
    if l[a][0]==1:
        print(0)
        exit()
    l[a][0]+=1
for b in B:
    if l[b][1]==1:
        print(0)
        exit()
    l[b][1]+=1

A=0
B=0
ans=1

for i in range(N*M):
    n=N*M-i
    if l[n]==[1,1]:
        A+=1
        B+=1
    elif l[n]==[1,0]:
        A+=1
        ans*=B
    elif l[n]==[0,1]:
        B+=1
        ans*=A
    else:
        ans*=A*B-(N*M-n)

    ans%=10**9+7

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

a = input().split()
n = int(a[0])
m = int(a[1])
q = int(a[2])

table = [[] for i in range(n)]
for i in range(m):
	a = input().split()
	l = int(a[0]) - 1
	r = int(a[1]) - 1
	table[l].append(r)

		
for i in range(q):
	output = 0
	a = input().split()
	l = int(a[0]) - 1
	r = int(a[1]) - 1
	for k in range(l, r + 1):
		for j in table[k]:
			if r >= j:
				output += 1
	print (output)
	

### FIXED CODE ###

import sys
s = sys.stdin.readlines()
 
N,M,Q = map(int,s[0].split())
 
Section = [[0]*N for _ in [0]*N]
for L,R in (map(int, e.split()) for e in s[1:M+1]):
    Section[L-1][R-1] += 1
 
Sum = [[0]*(N+1) for _ in [0]*(N+1)]
for i in range(N):
    for j in range(N):
        Sum[i+1][j+1] = Sum[i][j+1]+Sum[i+1][j]-Sum[i][j]+Section[i][j]
 
for p,q in (map(int, e.split()) for e in s[M+1:]):
    print(Sum[q][q]-Sum[p-1][q])
### BUGGY CODE ###

S = input()

mod = 10 ** 9 + 7

# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1

for i in range(len(S)):
    x = S[-(i + 1)]  # 後ろから一桁ずつみていく
    if x == "?":
        for k in range(10): # ?が0~9であった時、それぞれのケースについて調べる
            for j in range(13):
                dp[i + 1][(mul * k + j) % 13] += dp[i][j]
                dp[i + 1][(mul * k + j) % 13] %= mod
    else:
        k = int(x)
        for j in range(13):
            dp[i + 1][(mul * k + j) % 13] += dp[i][j]
            dp[i + 1][(mul * k + j) % 13] %= mod
    mul = mul * 10 % 13  # 計算量削減のためここで13で割っている

print(dp[len(S)][5])

### FIXED CODE ###

S = input()

mod = 10 ** 9 + 7

# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1

for i in range(len(S)):
    x = S[-(i + 1)]  # 後ろから一桁ずつみていく
    if x == "?":
        for k in range(10): # ?が0~9であった時、それぞれのケースについて調べる
            for j in range(13):
                dp[i + 1][(mul * k + j) % 13] += dp[i][j]
                dp[i + 1][(mul * k + j) % 13] %= mod
    else:
        k = int(x)
        for j in range(13):
            dp[i + 1][(mul * k + j) % 13] += dp[i][j]
            dp[i + 1][(mul * k + j) % 13] %= mod
    mul = mul * 10 % 13  # 計算量削減のためここで13で割っている

print(dp[len(S)][5])
### BUGGY CODE ###

import sys 
sys.setrecursionlimit(10**6)
s = input()
def dfs(t):
    if t==s:
        return True
    if t!=s[:len(t)]:
        return False
    for i in ['dream', 'dreamer','erase', 'eraser']:
            if dfs(t+i):
                return True
    else:
        return False
if dfs(''):
    print('YES')
else:
    print('NO')

### FIXED CODE ###

import sys 
sys.setrecursionlimit(10**6)
s = input()
def dfs(s):
    while True:
        f = True
        for i in ['erase', 'eraser', 'dream', 'dreamer']:
            if s[-len(i):]==i:
                s = s[:-len(i)]
                if s=='':
                    return True
                f = False
        if f: return False
    
if dfs(s):
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

N, S = map(int, input().split())
A = list(map(int, input().split()))
m = 0
var1 = [0] * (S + 1)
var1[0] = pow(2, N, 998244353)
var2 = pow(2, 998244353 - 2, 998244353)
for all in A:
    m = all+m
    funcvar1=min(S, m)
    for val in reversed(range(all,funcvar1+1)):
        var1[val] = (var1[val] + var1[val - all] * var2) % 998244353
print(var1[S])

### FIXED CODE ###

import numpy as np
def finalFunc(n,s,A):
    var=np.array([0 for i in range(min(3001,s+1))])
    var[0]=1
    for i in A:
        tmp=var[:-i].copy()
        var*=2
        var[i:]+=tmp
        var%=998244353
    return var[s]
n,s=map(int,input().split())
A=list(map(int,input().split()))
print(finalFunc(n,s,A))
### BUGGY CODE ###

from functools import lru_cache

N, A = map(int, input().split())
x = list(map(int, input().split()))
sum_x = sum(x)
X = max(x + [A])

answer = 0

@lru_cache(maxsize=None)
def solve(j, k, s):
    global x
    if j == 0 and k == 0 and s == 0:
        return 1
    elif j >= 1 and s < x[j - 1]:
        return solve(j-1, k, s)
    elif j >= 1 and k >= 1 and s >= x[j - 1]:
        return solve(j-1, k, s) + solve(j-1, k - 1, s - x[j - 1])
    else:
        return 0

for k in range(1, N + 1):
    answer += solve(N, k, k * A)
print(answer)


### FIXED CODE ###

from functools import lru_cache

N, A = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(lambda i: i - A, x))
X = max(max(x), A)

@lru_cache(maxsize=None)
def solve(j, t):
    global y
    if j == 0 and t == 0:
        return 1
    elif j >= 1 and (t < y[j - 1] - N * X or t > y[j - 1] + N * X):
        return solve(j - 1, t)
    elif j >= 1 and t >= y[j - 1] - N * X and t <= y[j - 1] + N * X:
        return solve(j - 1, t) + solve(j - 1, t - y[j - 1])
    else:
        return 0

print(solve(N, 0) - 1)
### BUGGY CODE ###

mod = 998244353
N,K = map(int,input().split())
move = set()
for _ in range(K):
    x,y = map(int,input().split())
    for i in range(x,y+1):
        move.add(i)

memo = [0]*(N+1)
memo[1] = 1
for i in range(2,N+1):
    for j in move:
        if 0<i-j:
            memo[i] += memo[i-j]
    memo[i] = memo[i]%mod
print(memo[-1]%mod)

### FIXED CODE ###

mod = 998244353
N,K = map(int,input().split())
move = [list(map(int,input().split())) for _ in range(K)]

dp = [0]*(N+1)
dp_sum = [0]*(N+1)
dp[1] = dp_sum[1] = 1
for i in range(2,N+1):
    for l,r in move:
        if 0<i-l:
            dp[i] += dp_sum[i-l]
            if 0<i-r-1:
                dp[i] -= dp_sum[i-r-1]
        dp[i] %= mod
    dp_sum[i] = dp_sum[i-1] + dp[i]
            
print(dp[-1]%mod)
### BUGGY CODE ###

import sys
input =sys.stdin.readline 
number, weight = input().rstrip().split()
number = int(number)
weight = int(weight)

items = [list(map(int,input().rstrip().split())) for _ in range(number)]
#print(items)
dp = [[0]*(weight+1) for _ in range(number+1)]

for i in  range(number):
    for j in range(weight+1):
        if j < items[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-items[i][0]] + items[i][1])

print(dp[number][weight])


### FIXED CODE ###

import sys
 
input = sys.stdin.readline
 
 
def main():
    N, W = map(int, input().split())
    wv = [None] * N
    for i in range(N):
        wv[i] = tuple(map(int, input().split()))
 
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i, (w, v) in enumerate(wv, start=1):
        dp_i = dp[i]
        dp_im = dp[i - 1]
        for j in range(1, W + 1):
            if j < w:
                dp_i[j] = dp_im[j]
            else:
                a = dp_im[j - w] + v
                b = dp_im[j]
                if a > b:
                    dp_i[j] = a
                else:
                    dp_i[j] = b
 
    ans = dp[-1][-1]
    print(ans)
 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

N,M=map(int,input().split())
list1=list(map(int,input().split()))
list2=[]
for _ in range(M):
  B,C=map(int,input().split())
  if C>min(list1):
    list2+=[C]*B  
list3=list1+list2
list3.sort(reverse=True)
list5=list3[:N]
SUM=sum(list5)
print(SUM)

### FIXED CODE ###

N,M = map(int, input().split())
listA = list(map(int, input().split()))
listA.sort()
 
listBC = [list(map(int, input().split())) for i in range(M)]
listBC = sorted(listBC, key=lambda x: x[1], reverse=True)
countB = 0
 
for i in range(M):
  B, C = listBC[i][0], listBC[i][1]
  listA += [C]*B
  countB += B
  if countB >= N:
    break

listA.sort(reverse=True)
print(sum(listA[:N]))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    x = list(map(int, input().split()))
    ans = 1
    for i in range(len(x)):
        ans *= x[i]
    if ans > 10**18:
        print('-1')
    else:
        print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    x = list(map(int, input().split()))
    ans = 1
    if 0 in x:
        print('0')
        return
    for i in range(len(x)):
        ans *= x[i]
        if ans > 10**18:
            print('-1')
            return
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)
### BUGGY CODE ###

X, Y, A, B, C = input().split(" ")
Xa = input().split(" ")
Ya = input().split(" ")
Notra = input().split(" ")
def toint(a):
	for i in range(len(a)):
		a[i]=int(a[i])
toint(Xa)
toint(Ya)
toint(Notra)
Notrmax = 0
Notra.sort(reverse=True)
Xa.sort(reverse=True)
Ya.sort(reverse=True)
Xa = Xa[0:int(X)]
Ya = Ya[0:int(Y)]
def change():
	answer = 0
	while True:
		Notra.sort(reverse=True)
		Xa.sort()
		Ya.sort()
		if Xa[0] > Ya[0]:
			try:
				if Ya[0] < Notra[0]:
					Ya[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] < Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] == Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
	for i in Xa:
		answer += i
	for i in Ya:
		answer += i
	print(answer)
change()

### FIXED CODE ###

x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
# print(p,q)
p = sorted(p, reverse=True)
q = sorted(q, reverse=True)
r = sorted(r, reverse=True)
p = [int(1e10)]+p[:x]
q = [int(1e10)]+q[:y]
k = 0
i, j = x, y
# print(p, q)
while 1:
   if k >= c:
      break
   if min(p[i], q[j]) >= r[k] :
      break
   if p[i] < q[j]:
      p[i] = r[k]
      i -= 1
   else :
      q[j] = r[k]
      j -=1
   k += 1
print(sum(p[1:])+sum(q[1:]))
### BUGGY CODE ###

s=list(input())
a=0
f=True
while f:
  for i in range(1,len(s)):
    if s[i]=='W' and s[i-1]=='B':
      a+=1
      s[i]='B'
      s[i-1]='W'
      break
  else:
    f=False
    
print(a)

### FIXED CODE ###

s=list(input())
cnt=s.count('W')
a=[i for i,x in enumerate(s) if x=='W']
ans=0
for i in range(cnt):
  ans+=a[i]-i
print(ans)