Harryxun/stf_run huggingface dataset:
#065b
N = int(input())
a_list=[0]

for i in range(N):
    ai = int(input())
    a_list.append(ai)

def push_button(pushed_button,pushed_button_set):
	if pushed_button == 2:
		print(len(pushed_button_set))
	else:
		if pushed_button in pushed_button_set:
			print(-1)
			return -1
		else:
			pushed_button_set.add(pushed_button)
			push_button(a_list[pushed_button],pushed_button_set)
			
push_button(1,set())

### FIXED CODE ###

N = int(input())

button_dict = dict()

for i in range(1,N+1):
    button_dict[i] = int(input())

pushed_button_set = set()
i = 1
while 1:
    if i in pushed_button_set:
        print(-1)
        break
    elif i == 2:
        print(len(pushed_button_set))
        break
    else:
        pushed_button_set.add(i)
        i = button_dict[i]
A,B,N = map(int, input().split())

def f(a, b, n):
    fl1 = (a * (n + 1)) // b
    fl2 = (n + 1) // b
    fl = fl1 - a * fl2
    return fl

print(f(a, b, b-1) if n >= b-1 else f(a, b, n))

### FIXED CODE ###

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

def f(a, b, n):
   # fl1 = (a * (n + 1)) // b
    #fl2 = (n + 1) // b
    #fl = fl1 - a * fl2
    #return fl
   return int(a * n / b) - a * int(n / b)

print(f(A, B, B-1) if N >= B-1 else f(A, B, N))
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,H,W = map(int,readline().split())
RCA = [tuple(int(x) for x in line.split()) for line in readlines()]
RCA.sort(key = lambda x:-x[2])

root = list(range(H+W))
size = [0] * (H+W)
no_cycle = [True] * (H+W)

def find_root(x):
    y = root[x]
    if x == y:
        return y
    z = find_root(y)
    root[x] = z
    return z

def merge(x,y):
    x,y = find_root(x),find_root(y)
    sx,sy = size[x],size[y]
    if sx < sy:
        sx,sy = sy,sx
        x,y = y,x
    root[y] = x
    no_cycle[x] = no_cycle[x] and no_cycle[y]

answer = 0
for R,C,A in RCA:
    x,y = R-1,H+C-1
    rx,ry = find_root(x),find_root(y)
    if rx == ry:
        if not no_cycle[rx]:
            continue
        no_cycle[rx] = False
    else:
        if (not no_cycle[rx]) and (not no_cycle[ry]):
            continue
        merge(rx,ry)
    answer += A

print(answer)


### FIXED CODE ###

import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

N,H,W = map(int,readline().split())
RCA = [tuple(int(x) for x in line.split()) for line in readlines()]

RCA.sort(key = lambda x: -x[2])

RCA

root = list(range(H+W))
size = [0] * (H+W)
no_cycle = [True] * (H+W)

def find_root(x):
    y = root[x]
    if x == y:
        return y
    z = find_root(y)
    root[x] = z
    return z

def merge(x,y):
    x,y = find_root(x),find_root(y)
    sx,sy = size[x],size[y]
    if sx < sy:
        sx,sy = sy,sx
        x,y = y,x
    root[y] = x
    no_cycle[x] = no_cycle[x] and no_cycle[y]

answer = 0
for R,C,A in RCA:
    x,y = R-1,H+C-1
    rx,ry = find_root(x),find_root(y)
    if rx == ry:
        if not no_cycle[rx]:
            continue
        no_cycle[rx] = False
    else:
        if (not no_cycle[rx]) and (not no_cycle[ry]):
            continue
        merge(rx,ry)
    answer += A

print(answer)
a,b = raw_input().split(" ")
a = int(a)
b = int(b)
print str(a*b) + " " str(2*a + 2*b) 

### FIXED CODE ###

a,b = raw_input().split(" ")
a = int(a)
b = int(b)
print str(a*b) + " " + str(2*a + 2*b)
t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,dif))
            ans+=ri
        else:
            R.append((n-ki-1,-dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    for i in range(len(L)):
        if L[i][0]>=len(hL):
            heapq.heappush(hL,L[i][1])
        elif L[i][0]=len(hL)-1:
            heapq.heappushpop(hL,L[i][1])
    for i in range(len(R)):
        if R[i][0]>=len(hR):
            heapq.heappush(hR,R[i][1])
        elif R[i][0]==len(hR)-1:
            heapq.heappushpop(hR,R[i][1])
    print(ans+sum(hL)+sum(hR))        
    
    

### FIXED CODE ###

t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,dif))
            ans+=ri
        else:
            R.append((n-ki-1,-dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    for i in range(len(L)):
        if L[i][0]>=len(hL):
            heapq.heappush(hL,L[i][1])
        elif L[i][0]==len(hL)-1:
            heapq.heappushpop(hL,L[i][1])
    for i in range(len(R)):
        if R[i][0]>=len(hR):
            heapq.heappush(hR,R[i][1])
        elif R[i][0]==len(hR)-1:
            heapq.heappushpop(hR,R[i][1])
    print(ans+sum(hL)+sum(hR))
A=list(map(int,input().split()))
pop = int(1/(4*M)*sum(A))
lis = []
for i in A:
    if i>=pop:
        lis.append(i)
if len(lis)>=M:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
pop = 1/(4*M)*sum(A)
lis = []
for i in A:
    if i>=pop:
        lis.append(i)
if len(lis)>=M:
    print("Yes")
else:
    print("No")
num,time = map(int, raw_input().split())
t = raw_input().split()
ans = 0
for i in t:
	if i<time:
		ans += i
	else:
	ans += time
ans += time
ans -= t[len(t)-1]
print ans

### FIXED CODE ###

num,time = map(int, raw_input().split())
t = raw_input().split()
ans = time
for i in range(0,num-1):
	if int(t[i+1])-int(t[i]) < time:
		ans += int(t[i+1])-int(t[i])
	else:
		ans += time
print ans
n=int(input())
p=[int(i) for i in input().split()]
cnt=0
for i in range(1,N-1):
  if p[i-1]<p[i] and p[i]<p[i+1]:
    cnt+=1
print(cnt)

### FIXED CODE ###

n=int(input())
p=[int(i) for i in input().split()]
cnt=0
for i in range(1,n-1):
  if p[i-1]<p[i] and p[i]<p[i+1]:
    cnt+=1
  elif p[i-1]>p[i] and p[i]>p[i+1]:
    cnt+=1
print(cnt)
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

dp = [[float("inf")]*w for _ in range(h)]
if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0

for j in range(h):
    for i in range(w):
        if i+1 < w:
            count1 = dp[i][j]
            if s[i][j] == "." and s[i+1][j] == "#":
                count1 += 1
            dp[i+1][j] = min(dp[i+1][j], count1)
        if j+1 < h:
            count2 = dp[i][j]
            if s[i][j] == "." and s[i][j+1] == "#":
                count2 += 1
            dp[i][j+1] = min(dp[i][j+1], count2)
print(dp[w-1][h-1])


### FIXED CODE ###

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

# ある経路をきめる.
# そのとき(白マスから黒マスに移動する回数)＝(その経路がいい経路にするための操作回数）
dp = [[float("inf")]*w for _ in range(h)]
if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
# dp[i][j]は00からijに行くときに白マスから黒マスに移動する回数の最小値
for i in range(h):
    for j in range(w):
        if i+1 < h:
            count1 = dp[i][j]
            if s[i][j] == "." and s[i+1][j] == "#":
                count1 += 1
            dp[i+1][j] = min(dp[i+1][j], count1)
        if j+1 < w:
            count2 = dp[i][j]
            if s[i][j] == "." and s[i][j+1] == "#":
                count2 += 1
            dp[i][j+1] = min(dp[i][j+1], count2)
print(dp[h-1][w-1])
w, h, x, y, r = map(int, input.split())
if(x < r or y < r or x + r > w or y + r > h):
    print("No")
else:
    print("Yes")


### FIXED CODE ###

w, h, x, y, r = map(int, input().split())
if(x < r or y < r or x + r > w or y + r > h):
    print("No")
else:
    print("Yes")
s = input()
n = ""
for i in s:
  if i == "9":
    n+=1
  else:
    n+=9
print(n)

### FIXED CODE ###

s = input()
n = ""
for i in s:
  if i == "9":
    n+="1"
  else:
    n+="9"
print(n)
def cmb(n, r):
  if n < r : return 0
  else : return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
md=10**9+7
n,k=map(int,input().split())
for i in range(k):
  a=cmb(n-k+1,i)*cmb(k-1,i-1)
  a=a%md
  print(a)

### FIXED CODE ###

import math
def cmb(n, r):
  if n < r : return 0
  else : return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
md=10**9+7
n,k=map(int,input().split())
for i in range(1,k+1):
  a=cmb(n-k+1,i)*cmb(k-1,i-1)
  a=a%md
  print(a)
print([8,7,6,5,4,3,2,1][(input()-400) // 200 ])

### FIXED CODE ###

print([8,7,6,5,4,3,2,1][(int(input())-400) // 200 ])
("Christmas " + (25 - (int(input())) * "Eve ")).strip()

### FIXED CODE ###

print(("Christmas "+((25-(int(input())))*"Eve ")).strip())
n,q=map(int,input().split())
ans=[0]*(n+1)
score=[0]*(n+1)
g=[[] for _ in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
for j in range(q):
    p,x=map(int,input().split())
    score[p]+=x
visited=set()
def dfs(v):
    visited.add(v)
    global ans
    global score
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if not g[v][i] in visited:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
ans_chr=""
for i in range(1,n+1):
    ans_chr=ans_chr+str(ans[i])+" "
print(ans_chr)


### FIXED CODE ###

import sys
sys.setrecursionlimit(400000)
n,q=map(int,input().split())
ans=[0]*(n+1) #答え
score=[0]*(n+1) #xの総和
g=[[] for _ in range(n+1)] #グラフ
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
for j in range(q):
    p,x=map(int,input().split())
    score[p]+=x

visited=[0]*(n+1)
def dfs(v):
    visited[v]+=1
    global ans
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if visited[g[v][i]]==0:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
print(" ".join(map(str, ans[1:])))
n=int(input())
a=[int(i) for i in input().split()]

ball_count=[0]*(n+1)
count_count=[0]*(n+1)

for i in a:
    ball_count[i]+=1
for i in ball_count:
    count_count[i]+=1

def get_total(count_count):
    ans=0
    for i in range(2,n+1):
        ans+=(i*(i-1)//2)*count_count[i]
    return ans
total=get_total(count_count)
for i in range(1,n+1):
    num=ball_count[a[i-1]]
    if num==2:
        ans=total-(num*(num-1)//2)
    elif num>2:
        ans = total - (num * (num - 1) // 2)+((num-1) * (num - 2) // 2)
    print(ans)




### FIXED CODE ###

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

ball_count = [0] * (n + 1)

for i in a:
    ball_count[i] += 1
def nCr(i):
    if i<=1:
        return 0
    else:
        return i*(i-1)//2

def get_total(ball_count):
    ans = 0
    for i in ball_count:
        ans += nCr(i)
    return ans


total = get_total(ball_count)
for i in range(1, n + 1):
    num = ball_count[a[i - 1]]
    ans = total - nCr(num) + nCr(num-1)
    print(ans)
N = int(input())

for i in range (1,int(N)+1):
    if i % 15 == 0 :
        a = 'FizzBuzz'
    elif i % 3 == 0:
        a = 'Fizz'
    elif i % 5 == 0:
        a = 'Buzz'
    else:
        b = b + i


### FIXED CODE ###

total = []

N = int(input())

for i in range(1, N + 1):
    if i % 3 == 0 and i % 5 == 0:
        pass
    elif i % 3 == 0:
        pass
    elif i % 5 == 0:
        pass
    else:
        total.append(i)

print(sum(total))
a = list(input())
b = list(set(a))

for i in range(b):
  if a.count(b[i]) % 2 != 0:
    print("No")
    break
else:
  print("Yes")
    

### FIXED CODE ###

a = list(input())
b = list(set(a))

for i in range(len(b)):
  if a.count(b[i]) % 2 != 0:
    print("No")
    break
else:
  print("Yes")
import math
from functools import reduce

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


def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


m =  lcm_list(A) -1

B = 0

for j in range(len(A)):
    B += m%A[j]

print(B)

### FIXED CODE ###

import math
from functools import reduce

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

def gcd(x,y):
    if x > y:
        x , y = y , x
    while x>0:
        x, y = y%x , x
    return y
    
def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


m =  lcm_list(A) -1

B = 0

for j in range(len(A)):
    B += m%A[j]

print(B)
S = input()
l = S.__len__()
print('%s%s%s' % [S[0], l-2, S[l-1]] 

### FIXED CODE ###

S = input()
l = S.__len__()
print('%s%s%s' % (S[0], l-2, S[l-1]))
n, h = map(int, input().split())
katana = [tuple(map(int, input().split())) for _ in range(n)]
maxa = 0
for a, _ in katana:
    maxa = max(maxa, a)
bgreedy = 0
count = 0
katana.sort(lambda x: x[1], reverse = True)
for a, b in katana:
    if b < maxa:
        break
    bgreedy += b
    count += 1
    if h <= bgreedy:
        print(count)
        exit()
print(count + (h - bgreedy)//maxa + int((h-bgreedy) % maxa != 0))

### FIXED CODE ###

n, h = map(int, input().split())
katana = [tuple(map(int, input().split())) for _ in range(n)]
maxa = 0
for a, _ in katana:
    maxa = max(maxa, a)
bgreedy = 0
count = 0
katana.sort(key = lambda x: x[1], reverse = True)
for a, b in katana:
    if b < maxa:
        break
    bgreedy += b
    count += 1
    if h <= bgreedy:
        print(count)
        exit()
print(count + (h - bgreedy)//maxa + int((h-bgreedy) % maxa != 0))
import sys
def main():
    input=sys.stdin.readline
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    
    ofset = 80*164
    #mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    """
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A)
                dp[i+1][j] |= (dp[i][j] >> A)
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A)
                dp[i][j+1] |= (dp[i][j] >> A)
    ans = 10000000000
    """
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1:
            ans = i - ofset
            break
    for i in reversed(range(ofset)):
        if (dp[h-1][w-1]>>i)&1:
            ans = min(ans, ofset - i)
            break
    print(ans)
    
if __name__=='__main__':
  main()


### FIXED CODE ###

import sys
def main():
    input=sys.stdin.readline
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    ofset = 80*164
    mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A) & mask
                dp[i+1][j] |= (dp[i][j] >> A) & mask
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A) & mask
                dp[i][j+1] |= (dp[i][j] >> A) & mask
    ans = 10000000000
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, i - ofset)
    for i in range(ofset):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, ofset - i)
    print(ans)
if __name__=='__main__':
  main()
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
sample_input = list(range(3))
sample_input[0] = '''7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5'''
sample_input[1] = '''9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast'''
sample_input[2] = ''''''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
class DoublyLinkedList():
    
    def __init__(self, a_data = []):
        self.data = a_data
    
    def insert(self, x):
        self.data.insert(0, x)
    
    def delete(self, x):
        self.data.remove(x)
    
    def delete_first(self):
        self.data.pop(0)
    
    def delete_last(self):
        self.data.pop()

    def to_str(self):
        output = ''
        for x in self.data:
            output += str(x) + ' '
        output = output.rstrip()
        return output

num_of_command = int(input())
doublely_list = DoublyLinkedList()
for n in range(num_of_command):
    str_command = input()
    if str_command == 'deleteFirst':
        doublely_list.delete_first()
        continue
    elif str_command == 'deleteLast':
        doublely_list.delete_last()
        continue
    command_name = str_command.split()[0]
    command_obj = int(str_command.split()[1])
    if command_name == 'insert':
        doublely_list.insert(command_obj)
    elif command_name == 'delete':
        doublely_list.delete(command_obj)

print(doublely_list.to_str())

### FIXED CODE ###

# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
sample_input = list(range(3))
sample_input[0] = '''7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5'''
sample_input[1] = '''9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast'''
sample_input[2] = '''8
insert 1000000000
insert 999999999
deleteLast
insert 1234566890
insert 5
deleteFirst
insert 7
delete 5'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
from collections import deque

num_of_command = int(input())
doubly_list = deque()
for n in range(num_of_command):
    str_command = input()
    if str_command == 'deleteFirst':
        doubly_list.popleft()
        continue
    elif str_command == 'deleteLast':
        doubly_list.pop()
        continue
    command_name = str_command.split()[0]
    command_obj = int(str_command.split()[1])
    if command_name == 'insert':
        doubly_list.appendleft(command_obj)
    elif command_name == 'delete':
        try:
            doubly_list.remove(command_obj)
        except ValueError:
            continue

output = ''
for item in doubly_list:
    output += str(item) + ' '
output = output.rstrip()
print(output)
X, N = map(int, input().split())
p = list(map(int, input().split()))


if N == 0:
    print(X)

for i in range(102):
    if x - i not in p:
        print(x - i)
        exit()
    
    if x + i not in p:
        print(x + i)
        exit()


### FIXED CODE ###

X, N = map(int, input().split())
p = list(map(int, input().split()))


if N == 0:
    print(X)
    exit()

for i in range(102):
    if X - i not in p:
        print(X - i)
        exit()
    
    if X + i not in p:
        print(X + i)
        exit()
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

sa = set(a)
sb = set(b)

count = [0] * (n + 1)
counta = [0] * (n + 1)
countb = [0] * (n + 1)

for v in a:
    count[v] += 1
    counta[v] += 1
for v in b:
    count[v] += 1
    countb[v] += 1

for v in count:
    if v > n:
        print('No')
        exit()

outA = dict()
for v in a:
    outA[v] = []

import heapq
q = []
for v in range(n):
    if count[v]:
        heapq.heappush(q,(-count[v],v))
    
while q[0][0] < 0:
    c, v = heapq.heappop(q)
    if count[v] + c:
        heapq.heappush(q,(-count[v],v))
        continue
    if counta[v]:
        for v2 in sb:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v].append(v2)
        count[v] -= 1
        count[v2] -= 1
        counta[v] -= 1
        countb[v2] -= 1
        if counta[v] == 0:
            sa.remove(v)
        if countb[v2] == 0:
            sb.remove(v2)
    else:
        for v2 in sa:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v2].append(v)
        count[v] -= 1
        count[v2] -= 1
        counta[v2] -= 1
        countb[v] -= 1
        if counta[v2] == 0:
            sa.remove(v2)
        if countb[v] == 0:
            sb.remove(v)
    if count[v]:
        heapq.heappush(q, (-count[v], v))

out = []
for v in a:
    out.append(outA[v].pop())
print('Yes')
print(' '.join(map(str,out)))


### FIXED CODE ###

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

sa = set(a)
sb = set(b)

count = [0] * (n + 1)
counta = [0] * (n + 1)
countb = [0] * (n + 1)

for v in a:
    count[v] += 1
    counta[v] += 1
for v in b:
    count[v] += 1
    countb[v] += 1

for v in count:
    if v > n:
        print('No')
        exit()

outA = dict()
for v in a:
    outA[v] = []

import heapq
q = []
for v in range(n + 1):
    if count[v]:
        heapq.heappush(q,(-count[v],v))
    
while q[0][0] < 0:
    c, v = heapq.heappop(q)
    if count[v] + c:
        heapq.heappush(q,(-count[v],v))
        continue
    if counta[v]:
        for v2 in sb:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v].append(v2)
        count[v] -= 1
        count[v2] -= 1
        counta[v] -= 1
        countb[v2] -= 1
        if counta[v] == 0:
            sa.remove(v)
        if countb[v2] == 0:
            sb.remove(v2)
    else:
        for v2 in sa:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v2].append(v)
        count[v] -= 1
        count[v2] -= 1
        counta[v2] -= 1
        countb[v] -= 1
        if counta[v2] == 0:
            sa.remove(v2)
        if countb[v] == 0:
            sb.remove(v)
    if count[v]:
        heapq.heappush(q, (-count[v], v))

out = []
for v in a:
    out.append(outA[v].pop())
print('Yes')
print(' '.join(map(str,out)))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import deque
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

d = [a[0]]
a=a[1:]
b = deque(d)

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))

  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())

res=0
for i in range(0,len(b)-1):
  res=res+abs(b[i]-b[i+1])
print(res)


### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
s=input()
if len(s)==2:
  print(s)
else:
  print(s[2]+s[1]+s[0])
(n, m, l) = [int(i) for i in input().split()]
A = []
for nc in range(n): 
    A.append([int(i) for i in input().split()])
B = []
for mc in range(m): 
    B.append([int(i) for i in input().split()])

product = [[0 for d in range(l)] for dd in range()]
      
for nc in range(n):
    for lc in range(l): 
        for mc range(m):
            product[nc][lc] += A[nc][mc] * B[mc][lc] 

for r in product: 
    print(' '.join([str(d) for d in r]))

### FIXED CODE ###

(n, m, l) = [int(i) for i in input().split()]
A = []
for nc in range(n): 
    A.append([int(i) for i in input().split()])
B = []
for mc in range(m): 
    B.append([int(i) for i in input().split()])

product = [[0 for d in range(l)] for dd in range(n)]
      
for nc in range(n):
    for lc in range(l): 
        for mc in range(m):
            product[nc][lc] += A[nc][mc] * B[mc][lc] 

for r in product: 
    print(' '.join([str(d) for d in r]))
n=int(input())
m=str(n)
x=0
for i in m:
  x+=i
if n%x==0:
  print('Yes')
else:
  print('No')


### FIXED CODE ###

n=int(input())
m=str(n)
x=0
for i in m:
  x+=int(i)
if n%x==0:
  print('Yes')
else:
  print('No')
N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
b_to_c = [[int(_) for _ in input().split()] for i in range(Q)]

sum = 0
memo = [0] * (10 ** 5)
for i in A:
    memo[i] += 1
    sum += i

for bc in b_to_c:
    b = bc[0]
    c = bc[1]
    sum -= (b * memo[b])
    sum += (c * memo[b])
    memo[c] += memo[b]
    memo[b] = 0
    print(sum)


### FIXED CODE ###

N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
b_to_c = [[int(_) for _ in input().split()] for i in range(Q)]

sum = 0
memo = [0] * (10 ** 5 + 1)
for i in A:
    memo[i] += 1
    sum += i

for bc in b_to_c:
    b = bc[0]
    c = bc[1]
    sum -= (b * memo[b])
    sum += (c * memo[b])
    memo[c] += memo[b]
    memo[b] = 0
    print(sum)