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

s = input()
q = int(input())
query = [input().split() for _ in range(q)]

for i in query:
    if len(i) == 1:
        s = s[::-1]
    else:
        if i[1] == '1':
            s = i[2] + s
        if i[1] == '2':
            s = s + i[2]

print(s)

### FIXED CODE ###

from collections import deque

s = deque(input())
q = int(input())
query = [input().split() for _ in range(q)]

reverse = False
for i in query:
    if len(i) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if reverse:
            if i[1] == '1':
                s.append(i[2])
            else:
                s.appendleft(i[2])
        else:
            if i[1] == '1':
                s.appendleft(i[2])
            else:
                s.append(i[2])
if reverse:
    s = reversed(s)
    s = ''.join(s)
    print(s)
else:
    s = ''.join(s)
    print(s)
### BUGGY CODE ###

N = int(input())
s = [input() for i in range(5)]
s = [int(i) for i in  s]
time = 0
road = [0 for i in range(6)]
road[0] = N
while(True):
    for i in range(5)[::-1]:
        moving_people_num = len(set(range(road[i])) & set(range(s[i]))) 
        road[i] -= moving_people_num
        road[i+1] += moving_people_num
    time += 1
    if road[5] == N:
        break
print(time)

### FIXED CODE ###

import math
N = int(input())
s = [input() for i in range(5)]
s = [int(i) for i in s]
s_min = min(s)
time = math.ceil(N/s_min) + 4
print(time)
### BUGGY CODE ###

import itertools
import os
import sys

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

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, M, D = list(map(int, sys.stdin.readline().split()))


# 数列の 1 つ目の要素が決まれば数も決まりそう
# 差が D となる数が上にだけある・下にだけある・上にも下にもある・どっちにもない
# 1 <= a <= N-D までは上にある
# D < a <= N までは下にある

def solve(n, m, d):
    arr = list(itertools.product(range(n), repeat=m))
    ans = 0
    for a in arr:
        for i in range(m - 1):
            if abs(a[i] - a[i + 1]) == d:
                ans += 1
    return ans


# 隣り合う要素を 2 つ決めたときに差が D となる組み合わせの数
if D == 0:
    c = N
else:
    c = (N - D) * 2

# 全部の合計
# pow(N, M - 2): ある 2 つの要素を決めたときにその位置にそれらを使う数列の数
s = c * pow(N, M - 2) * (M - 1)
# 個々の美しさがわからなくても合計がわかれば組み合わせ数で割れば平均になる
ans = s / pow(N, M)
print(ans)


### FIXED CODE ###

import itertools
import os
import sys

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

sys.setrecursionlimit(2147483647)
INF = float("inf")

N, M, D = list(map(int, sys.stdin.readline().split()))


# 数列の 1 つ目の要素が決まれば数も決まりそう
# 差が D となる数が上にだけある・下にだけある・上にも下にもある・どっちにもない
# 1 <= a <= N-D までは上にある
# D < a <= N までは下にある

def solve(n, m, d):
    arr = list(itertools.product(range(n), repeat=m))
    ans = 0
    for a in arr:
        for i in range(m - 1):
            if abs(a[i] - a[i + 1]) == d:
                ans += 1
    return ans


# 隣り合う要素を 2 つ決めたときに差が D となる組み合わせの数
if D == 0:
    c = N
else:
    c = (N - D) * 2

# # 全部の合計
# # pow(N, M - 2): ある 2 つの要素を決めたときにその位置にそれらを使う数列の数
# s = c * pow(N, M - 2) * (M - 1)
# # 個々の美しさがわからなくても合計がわかれば組み合わせ数で割れば平均になる
# ans = s / pow(N, M)

# ans = c * (M - 1) * pow(N, M - 2) / pow(N, M)
ans = c * (M - 1) / N ** 2
print(ans)
### BUGGY CODE ###

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

p = a
t = sorted(list(set(a)))
for i in range(n):
  x = p[i]
  p[i] = 0
  s = 0
  for j in t:
    m = p.count(j)
    for k in range(m):
      s += k
  print(s)
  p[i] = x

### FIXED CODE ###

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

nums = [0]*(n+1)
for i in a:
  nums[i] += 1
  
t = 0
for j in nums:
  t += j*(j-1)//2

for k in a:
  print(t+1-nums[k])
### BUGGY CODE ###

import bisect
import sys

input=sys.stdin.readline

def main(args):
    n, d, a = map(int,input().split())
    
    monster = [0]*n
    pos = [0]*n
    for i in range(n):
        x, h = map(int,input().split())
        monster[i] = (x, h)
        pos[i] = x
    
    pos.sort()
    monster.sort()
    
    #print(pos)
    
    limit = [0]*n
    #そのモンスターを攻撃したとき巻き添えにできるやつらの中で一番右のヤツのindex
    for i in range(n):
        limit[i] = bisect.bisect_right(pos, pos[i]+2*d)-1
    #print(limit)
    
    damage = [0]*n
    ans = 0
    for i in range(n):
        HP = monster[i][1]
        attack = -(-max(0, (HP-a*damage[i]))//a)
        if attack > 0:
            ans += attack
            damage[i:limit[i]+1] = list(map(lambda x:x+attack, damage[i:limit[i]+1]))
        #print(damage)
    
    print(ans)

if __name__ == '__main__':
    main(sys.argv[1:])

### FIXED CODE ###

import bisect
import itertools
import sys

input=sys.stdin.readline

def main(args):
    n, d, a = map(int,input().split())
    
    monster = [0]*n
    pos = [0]*n
    for i in range(n):
        x, h = map(int,input().split())
        monster[i] = (x, -(-h//a))
        pos[i] = x
    
    pos.sort()
    monster.sort()
    
    limit = [0]*n
    #そのモンスターを攻撃したとき巻き添えにできるやつらの中で一番右のヤツのindex
    for i in range(n):
        limit[i] = bisect.bisect_right(pos, pos[i]+2*d)-1
    
    #print(limit)
    
    damage = [0]*(n+1)
    ans = 0
    for i in range(n):
        if damage[i] >= monster[i][1]:
            pass
        else:
            plus = monster[i][1] - damage[i]
            ans += plus
            damage[i] += plus
            damage[limit[i]+1] -= plus
        #print(damage)
        
        damage[i+1] += damage[i]
    
    print(ans)

if __name__ == '__main__':
    main(sys.argv[1:])
### BUGGY CODE ###

def main():
    n = int(input())
    L = list(map(int, input().split()))
    L.sort()
    counter = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            result = 0
            left = j+1
            right = n-1
            while L[i]+L[j] > L[left]:
                if right - left <= 1:
                    if L[i]+L[j] > L[right]:
                        result = right - j
                    else:
                        result = left - j
                    break
                mid = (left+right)//2
                if L[i]+L[j] > L[mid]:
                    left = mid
                else:
                    right = mid
            counter += result
    print(counter)
if __name__ == '__main__':
    main()

### FIXED CODE ###

n = int(input())
l = list(map(int, input().split(' ')))
l.sort()
counter = 0
for i in range(n):
    s = i + 2
    for j in range(i+1,n):        
        while (s < n and l[i] + l[j] > l[s]):
            s += 1
        counter += s-j-1
print(counter)
### BUGGY CODE ###

n=int(input())
x=list(map(int,input().split()))
b=tuple(sorted(x))
m=(n-1+1)//2-1
for i in x:
    if b.index(i)<=m:print(b[m+1])
    else:print(b[m])

### FIXED CODE ###

n=int(input())
x=list(map(int,input().split()))
b=sorted(x)
m=(n-1+1)//2-1
for i in x:
    if i<=b[m]:print(b[m+1])
    else:print(b[m])
### BUGGY CODE ###

N = int(input())

V = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
for i in range(2**N):
    X = 0
    Y = 0
    for j in range(N):
        if ((i >> j) & 1):
            X += V[j]
            Y += C[j]
    ans = max(ans, X-Y)

print(ans)
    


### FIXED CODE ###

N = int(input())

V = list(map(int,input().split()))
C = list(map(int,input().split()))

import copy

flag = [0]*N
def dfs(x, y):
    if y==1:
        flag[x] = 1
    else:
        flag[x] = 0
    if x==N-1:
        value = 0
        cost = 0
        for i in range(N):
            if flag[i] == 1:
                value += V[i]
                cost += C[i]
        # print(flag)
        # for i in range(N):
        #     flag[i] = 0
        return value - cost

    
    return max(dfs(x+1, 0),dfs(x+1, 1))

print(dfs(-1,0))
### BUGGY CODE ###

N,K,Q=map(int, input().split())
#print(N,K,Q)

if K>Q:
  for j in range(N):
    print('Yes')
else:    
 A=list()
 for j in range(Q):
   A.append(int(input()))

#print(A)
#C=list()
 for j in range(N):
   if Q-A.count(j+1)<K:
     print('Yes')
   else:
     print('No')
 




### FIXED CODE ###

N,K,Q=map(int, input().split())
score=[Q]*N
#print(score)
for j in range(Q):
  c=int(input())
  score[c-1] += -1
 
#print(score)

for j in range(N):
  if score[j]<K:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def main():
    A, B, N = map(int, input().split())
    ret = 0
    for i in range(min(B,N+1)):
        ret = max(ret, i*A//B)
    print(ret)

if __name__ == '__main__':
    main()

### FIXED CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def main():
    A, B, N = map(int, input().split())
    N = min(B-1, N)
    ret = (N * A)//B - A * (N//B)
    print(ret)



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

s = list(input())

words = ['dream', 'dreamer', 'erase', 'eraser']
candidate = ['']
ans = 'NO'

while len(candidate)>0:
    p = candidate.pop()
    if list(p) == s:
        ans = 'YES'
        break
    if len(p) >= len(s):
        continue
    if p == ''.join(s[0:len(p)]):
        for w in words:
            candidate.append(p+w)

print(ans)


### FIXED CODE ###

s = list(input())

words = ['dream', 'dreamer', 'erase', 'eraser']
ans = 'YES'

pointer = len(s)
while pointer > 0:
    if s[pointer-5: pointer] == list(words[0]):
        pointer -= 5
    elif s[pointer-5: pointer] == list(words[2]):
        pointer -= 5
    elif s[pointer-7: pointer] == list(words[1]):
        pointer -= 7
    elif s[pointer-6: pointer] == list(words[3]):
        pointer -= 6
    else:
        ans = 'NO'
        break

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

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

rl = []

for i in range(k-1,n):
  j = 0
  r = 1
  while j < k:
    r = r * a[i-j]
    j += 1
  rl.append(r)
  i += 1



for m in range(n-k):
  if rl[m+1] > rl[m]:
    print('Yes')
  else:
    print('No')

### FIXED CODE ###

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

for i in range(k,n):
  if a[i] > a[i-k]:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

N = int(input())
li = []
count = 0
for n in range(N):
    i = input()
    if i in li:
        pass
    else:
        li.append(i)
        count+=1
print(count)

### FIXED CODE ###

N = int(input())
li = []
count = 0
for n in range(N):
    i = input()
    li.append(i)
print(len(set(li)))
### BUGGY CODE ###

n = int(input())
h = list(map(int,input().split()))
h.append(10**10)
ans = 0

for i in range(n):
  cnt = 0
  for j in range(i,n):
    if h[j]>=h[j+1]:
      cnt +=1
    else:
      if cnt > ans:
        ans = cnt
      break

print(ans)

### FIXED CODE ###

n = int(input())
h = list(map(int,input().split()))
h = h[::-1]
h.append(10**10)
cnt = 0
L = []
for i in range(n):
  L.append(cnt)
  if h[i+1]>=h[i]:
    cnt +=1
  else:
    cnt =0
print(max(L))
### BUGGY CODE ###

N, K = map(int, input().split())
MOD = 10**9 + 7
kai = []
kai.append(1)
for idx in range(1, MOD+5):
	kai.append((kai[idx-1] * idx) % MOD)

#Mod pow
def modpow(ori, po, mod):
	res = 1;
	while(po > 0):
		if(po&1):
			res *= ori;
			res %= mod;
		ori *= ori;
		ori %= mod;
		po >>= 1;
	return res;

def comb(n, k):
	return (kai[n] * modpow(kai[k], MOD-2, MOD) * modpow(kai[n-k], MOD-2, MOD))%MOD


for i in range(K):
	blue=comb(K-1, i)%MOD
	red=comb(N-K+1, i+1)%MOD
	print((blue*red)%MOD)

### FIXED CODE ###

N, K = map(int, input().split())
MOD = 10**9 + 7
kai = []
kai.append(1)
for idx in range(1, N+5):
	kai.append((kai[idx-1] * idx) % MOD)

#Mod pow
#def modpow(ori, po, mod):
#	res = 1;
#	while(po > 0):
#		if(po&1):
#			res *= ori;
#			res %= mod;
#		ori *= ori;
#		ori %= mod;
#		po >>= 1;
#	return res;



comb = [[0] * 2005 for i in range(2005)]

def combination():
	for i in range(2000):
		comb[i][0] = 1;
		for j in range(1, i+1):
			comb[i][j] = comb[i-1][j-1] + comb[i-1][j];
			comb[i][j] %= MOD





#def comb(n, k):
#	return (kai[n] * modpow(kai[k], MOD-2, MOD) * modpow(kai[n-k], MOD-2, MOD))%MOD

combination()

for i in range(K):
	blue=comb[K-1][i]%MOD
	red=comb[N-K+1][i+1]%MOD
	print((blue*red)%MOD)
### BUGGY CODE ###

from collections import deque
import sys
N = int(input().strip("\n"))
ALL = N*(N-1)//2
A = [list(map(lambda x :int(x)-1, input().split())) for _ in range(N)]

# 対戦カード (インデックス0>インデックス[1]だけ使う)
all_patterns = [[[] for _ in range(N)] for _ in range(N)]
lines_to_here = [[0 for _ in range(N)] for _ in range(N)]


# トポロジカルソート
for i in range(len(A)):
    for j in range(len(A[i])-1):
        playerA = i
        playerB = A[i][j]
        if playerA < playerB:
            playerA, playerB = playerB, playerA
        next_playerA = i
        next_playerB = A[i][j+1]
        if next_playerA < next_playerB:
            next_playerA, next_playerB = next_playerB, next_playerA
        
        all_patterns[playerA][playerB].append([next_playerA,next_playerB])
        lines_to_here[next_playerA][next_playerB] += 1

next_out = deque()
for i in range(1,N):
    for j in range(i):
        if lines_to_here[i][j] == 0:
            next_out.append([i,j])

if len(next_out) == 0:
    print(-1)
    sys.exit()

indexin_topological_sorted = [[0 for _ in range(N)] for _ in range(N)]

# topological sort
topological_sorted = []
while len(next_out) > 0:
    added = next_out.pop()
    topological_sorted.append(added)
    indexin_topological_sorted[added[0]][added[1]] = len(topological_sorted)-1
    for direction in all_patterns[added[0]][added[1]]:
        lines_to_here[direction[0]][direction[1]]-=1
        if lines_to_here[direction[0]][direction[1]] == 0:
            next_out.append(direction)

# if there is closed circuit number remain
for i in range(1,N):
    for j in range(i):
        if lines_to_here[i][j] > 0:
            print(-1) 
            sys.exit()

costs = [0 for _ in range(len(topological_sorted))]

max_cost = 0
for i in range(len(topological_sorted)):
    processing = topological_sorted[i]
    now_cost = costs[i]
    for direction in all_patterns[processing[0]][processing[1]]:
        direction_in_topological_sorted = indexin_topological_sorted[direction[0]][direction[1]]
        if costs[direction_in_topological_sorted] < 1+now_cost:
            costs[direction_in_topological_sorted] = 1+now_cost
        if max_cost < costs[direction_in_topological_sorted]:
            max_cost = costs[direction_in_topological_sorted]
print(max_cost+1)


### FIXED CODE ###

from collections import deque

N = int(input().strip("\n"))
ALL = N*(N-1)//2
A = [deque(list(map(lambda x :int(x)-1, input().split()))) for _ in range(N)]

day = 0
candidates = set(range(N))
while len(candidates)>0:
    day += 1
    nextday = set()
    checked = set()
    #print(candidates)
    for candidate in candidates:
        if len(A[candidate]) == 0:
            continue
        if candidate in checked:
            continue
        opponent = A[candidate][0]
        if opponent in checked:
            continue
        if A[opponent][0] == candidate:
            A[opponent].popleft()
            A[candidate].popleft()
            nextday.add(candidate)
            nextday.add(opponent)
            checked.add(candidate)
            checked.add(opponent)
    candidates = nextday

for p in A:
    if len(p) > 0:
        print(-1)
        break
else:
    print(day-1)
### BUGGY CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

dd = [(1,0),(0,1),(-1,0),(0,-1)]
h,w = inpl()
cy,cx = inpl()
gy,gx = inpl()
cy,cx,gy,gx = cy-1,cx-1,gy-1,gx-1
s = [input() for _ in range(h)]
res = [[INF] * w for _ in range(h)]
q = deque([(cy,cx)])
res[cy][cx] = 0
while q:
    oy,ox = q.popleft()
    for dy in range(-2,3):
        for dx in range(-2,3):
            ny = dy + oy
            nx = dx + ox
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == '.':
                f = False
                if (dy,dx) in dd:
                    if res[ny][nx] > res[oy][ox]:
                        f = True
                        res[ny][nx] = min(res[ny][nx], res[oy][ox])
                else:
                    if res[ny][nx] > res[oy][ox] + 1:
                        f = True
                        res[ny][nx] = min(res[ny][nx], res[oy][ox] + 1)
                if f: q.append((ny,nx))
print(res[gy][gx] if res[gy][gx] != INF else -1)

### FIXED CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

dd = [(1,0),(0,1),(-1,0),(0,-1)]
h,w = inpl()
cy,cx = inpl()
gy,gx = inpl()
cy,cx,gy,gx = cy-1,cx-1,gy-1,gx-1
s = [input() for _ in range(h)]
res = [[INF] * w for _ in range(h)]
res[cy][cx] = 0
seen = set()
q = deque([(cy,cx)])
while q:
    oy,ox = q.popleft()
    if (oy,ox) in seen: continue
    seen.add((oy,ox))
    for dy,dx in dd:
        ny = oy+dy; nx = ox+dx
        if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == '.':
            if res[ny][nx] > res[oy][ox]:
                res[ny][nx] = res[oy][ox]
                q.appendleft((ny,nx))
        for dy in range(-2,3):
            for dx in range(-2,3):
                ny = oy+dy; nx = ox+dx
                if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == '.':
                    if res[ny][nx] > res[oy][ox] + 1:
                        res[ny][nx] = res[oy][ox] + 1
                        q.append((ny,nx))
ans = res[gy][gx]
print(-1 if ans  == INF else ans)
### BUGGY CODE ###

import heapq
import sys

input = sys.stdin.readline

inf = 10**12

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

data = [[] for _ in range(N)]

sumA = 0

for i in range(M):
    U,V,A,B = map(int,input().split())
    sumA += A
    U -= 1
    V -= 1
    data[U].append((B,V,A))###(距離, 行き先, 費用)
    data[V].append((B,U,A))###(距離, 行き先, 費用)

P = sumA

C = [0]*N
D = [0]*N

for i in range(N):
    C[i],D[i] = map(int,input().split())

dist = [[inf]*(P+1) for _ in range(N)]
#visit = [[0]*(P+1) for _ in range(N)]
###heapにぶち込んだことがあるかないか

q = [(0,0,min(S,P))]###(仮の最短距離, 頂点番号, 所持金)
heapq.heapify(q)

while q:
    v = heapq.heappop(q)
    if v[0] > dist[v[1]][v[2]]:
        continue
    dist[v[1]][v[2]] = v[0]

    if v[2] + C[v[1]] <= P:
        heapq.heappush(q,(v[0]+D[v[1]],v[1],v[2]+C[v[1]]))

    for x in data[v[1]]:
        if x[2] > v[2]:
            continue
        heapq.heappush(q,(v[0]+x[0],x[1],v[2]-x[2]))
    
    ###
    #print(P)
    #print(q)
    #print(v)
    ###

for i in range(1,N):
    print(min(dist[i]))
        



### FIXED CODE ###

import heapq
import sys

input = sys.stdin.readline

inf = 10**13

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

data = [[] for _ in range(N)]

sumA = 0

for i in range(M):
    U,V,A,B = map(int,input().split())
    sumA += A
    U -= 1
    V -= 1
    data[U].append((B,V,A))###(距離, 行き先, 費用)
    data[V].append((B,U,A))###(距離, 行き先, 費用)

P = sumA

C = [0]*N
D = [0]*N

for i in range(N):
    C[i],D[i] = map(int,input().split())

dist = [[inf]*(P+1) for _ in range(N)]
#visit = [[0]*(P+1) for _ in range(N)]
###heapにぶち込んだことがあるかないか

q = [(0,0,min(S,P))]###(仮の最短距離, 頂点番号, 所持金)
heapq.heapify(q)

while q:
    v = heapq.heappop(q)
    if v[0] >= dist[v[1]][v[2]]:
        continue
    dist[v[1]][v[2]] = v[0]

    if v[2] + C[v[1]] <= P:
        if dist[v[1]][v[2]+C[v[1]]] == inf:
            heapq.heappush(q,(v[0]+D[v[1]],v[1],v[2]+C[v[1]]))

    for x in data[v[1]]:
        if x[2] > v[2]:
            continue
        if dist[x[1]][v[2]-x[2]] == inf:
            heapq.heappush(q,(v[0]+x[0],x[1],v[2]-x[2]))
    
    ###
    #print(P)
    #print(q)
    #print(v)
    ###

for i in range(1,N):
    print(min(dist[i]))
### BUGGY CODE ###

n = int(input())
stack = []
for _ in range(n):
    stack.append(int(input()))

candidate = [-10**10]
for i, v in enumerate(stack):
    for j in stack[:i]:
        candidate.append(v - j)
    candidate = [max(candidate)]

print(candidate[0])

### FIXED CODE ###

n = int(input())
stack = []
for _ in range(n):
    stack.append(int(input()))

maxv = -10**9
minv = stack[0]
for i in range(1, len(stack)):
    maxv = max(maxv, stack[i] - minv)
    minv = min(minv, stack[i])

print(maxv)
### BUGGY CODE ###

import heapq

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

q = []
for _ in range(m):
    b, c = map(int, input().split())
    q += [-c]*b

heapq.heapify(q)

for i in range(n):
    if not q: break
    x = -heapq.heappop(q)
    if a[i] < x:
        a[i] = x
    else: break

print(sum(a))


### FIXED CODE ###

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
a.sort(reverse=True)
print(sum(a[:n]))
### BUGGY CODE ###

import bisect
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

ans = 0
for b in B:
    a_index = bisect.bisect_left(A, b)
    c_index = bisect.bisect_right(C, b)
    ans += len(A[:a_index])*len(C[c_index:])
print(ans)

### 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:
    a_ind = bisect.bisect_left(A, b)
    c_ind = bisect.bisect_right(C, b)
    ans += a_ind*(n-c_ind)
    
print(ans)
### BUGGY CODE ###

import math
K = int(input()) + 1
A = 0
for a in range(1, K):
    for b in range(1, K):
        for c in range(1, K):
            A += math.gcd(math.gcd(a, b), c)
print(A)

### FIXED CODE ###

import math
import itertools

K = int(input()) + 1
L = list(itertools.combinations_with_replacement(list(range(1, K)), 3))
a = 1
A = 0
for i in L:
  if len(set(i)) == 3:
    a = 6
  elif len(set(i)) == 2:
    a = 3
  else:
    a = 1
  A += math.gcd(math.gcd(i[0], i[1]), i[2]) * a
print(A)