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

n,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[0 for i in range(n)]
for i in range(1,n):
  dp[i]=min([dp[j]+abs(a[i]-a[j]) for j in range(max(0,i-k),i)])
print(dp[-1])


### FIXED CODE ###

import sys
input = sys.stdin.readline
def fun(n,a,k):
 dp=[0]*n
 for i in range(1,n):
  dp[i]=min([dp[j]+abs(a[i]-a[j]) for j in range(max(0,i-k),i)])
 print(dp[len(dp)-1])
if __name__ == '__main__':
 n,k = map(int, input().split())
 a=list(map(int,input().split()))
 fun(n,a,k)
### BUGGY CODE ###

N = int(input())

XY = []
for _ in range(N):
    x, y = map(int, input().split())
    XY.append((x, y))
    
# BITの定義
import math
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    # index 1からiの値の和を返す
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    # index iの値にxを加算する
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

#######################
# 開始
p_list = XY[:]

# y座標の圧縮
from operator import itemgetter
p_list = sorted(p_list, key=itemgetter(1))
p_list = [ (x, y, i+1) for i, (x, y) in enumerate(p_list)]

# xでソート
p_list= sorted(p_list)

a = [0] * N # 左下
b = [0] * N# 左上
c = [0] * N# 右下
d = [0] * N# 右上

# a,bを求める
bit = Bit(N)
for i, (x, y, r) in enumerate(p_list):
    tmp = bit.sum(r)
    a[i] = tmp
    b[i] = i-tmp
    bit.add(r, 1)

# c,dを求める
bit = Bit(N)
for i, (x, y, r) in enumerate(reversed(p_list)):
    tmp = bit.sum(r)
    c[i] = tmp
    d[i] = i-tmp
    bit.add(r, 1)
c = c[::-1]
d = d[::-1]

# ある点が四角に含まれる場合の総数を求めるメソッド
power = [1] * N
for i in range(1, N):
    power[i] = power[i-1] * 2 % 998244353
def p_comb(a, b, c, d):
    a = power[a] - 1
    b = power[b] - 1
    c = power[c] - 1
    d = power[d] - 1
    
    ret = (a+1) * (d+1) * c * b
    ret += (b+1) * (c+1) * a * d
    ret -= a*b*c*d
    ret += power[N-1]
    return ret % 998244353
 
ans = 0
for i in range(N):
    ans += p_comb(a[i], b[i], c[i], d[i])
    
print(ans % 998244353)   


### FIXED CODE ###

N = int(input())

XY = []
for _ in range(N):
    x, y = map(int, input().split())
    XY.append((x, y))
    
# BITの定義
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    # index 1からiの値の和を返す
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    # index iの値にxを加算する
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

#######################
# 開始
from operator import itemgetter
def solve():
    p_list = XY[:]

    # y座標の圧縮
    p_list = sorted(p_list, key=itemgetter(1))
    p_list = [ (x, y, i+1) for i, (x, y) in enumerate(p_list)]

    # xでソート
    p_list= sorted(p_list)

    # ある点が四角に含まれる場合の総数を求めるメソッド
    power = [1] * N
    for i in range(1, N):
        power[i] = power[i-1] * 2 % 998244353

    # a,bを求める
    bit = Bit(N)
    ans = 0
    for i, (x, y, r) in enumerate(p_list):
        tmp = bit.sum(r)
        a = tmp
        b = i-tmp
        c = r-tmp-1
        d = (N-r+1)-(i-tmp)-1
        bit.add(r, 1)
        _a = power[a] - 1
        _b = power[b] - 1
        _c = power[c] - 1
        _d = power[d] - 1

        ret = (_a+1) * (_d+1) * _c * _b
        ret += (_b+1) * (_c+1) * _a * _d
        ret -= _a*_b*_c*_d
        ret += power[N-1]
        ans += ret % 998244353

    print(ans % 998244353)
    
solve()
### BUGGY CODE ###

S=int(input())

ma=pow(10,9)

for i in range(1,int(pow(S,1/2))+1):
    #print(i)
    if S%i==0 and S//i<=ma:
        print(0,0,i,0,0,S//i)
        break


### FIXED CODE ###

S=int(input())

X_2=pow(10,9)
X_3=(X_2-S)%X_2
Y_3=(S+X_3)//X_2

print(0,0,X_2,1,X_3,Y_3)
### BUGGY CODE ###

from collections import deque
import itertools
 
def dfs(field, h, w):
    visited = [[False] * w for _ in range(h)]
    q = deque([])
    for y in range(h):
        for x in range(w):
            if field[y][x] == '#':
                q.append((y, x, 0))
                visited[y][x] = True
    
    while q:
        if not False in itertools.chain.from_iterable(visited):
            i, j, d = q.pop()
            return d
        i, j, d = q.popleft()
        if i > 0 and visited[i - 1][j] == False and field[i - 1][j] == '.':
            q.append((i - 1, j, d + 1))
            visited[i - 1][j] = True
        if i < h - 1 and visited[i + 1][j] == False and field[i + 1][j] == '.':
            q.append((i + 1, j, d + 1))
            visited[i + 1][j] = True
        if j > 0 and visited[i][j - 1] == False and field[i][j - 1] == '.':
            q.append((i, j - 1, d + 1))
            visited[i][j - 1] = True
        if j < w - 1 and visited[i][j + 1] == False and field[i][j + 1] == '.':
            q.append((i, j + 1, d + 1))
            visited[i][j + 1] = True
    else:
        return d


h , w = map(int, input().split())
field = [input() for _ in range(h)]
dot_cnt = sum(r.count('.') for r in field)
d = dfs(field, h, w)
print(d)

### FIXED CODE ###

import numpy as np

H,W = map(int,input().split())
use = np.zeros((H,W),dtype=int)
for i in range(H):
    s = input()
    for j,st in enumerate(s):
        if st == ".":
            use[i][j] = 10**10
        else:
            use[i][j] = 0

for i in range(1,H):
    use[i,:] = np.minimum(use[i,:],use[i-1,:]+1)
for i in reversed(range(H-1)):
    use[i,:] = np.minimum(use[i,:],use[i+1,:]+1)
for j in range(1,W):
    use[:,j] = np.minimum(use[:,j],use[:,j-1]+1)
for j in reversed(range(W-1)):
    use[:,j] = np.minimum(use[:,j],use[:,j+1]+1)

print(np.max(use))
### BUGGY CODE ###

from heapq import heappop, heappush
def main():
    def push(i, j, k):
        nonlocal A, B, C, ADD
        if i >= x or j >= y or k >= z: return
        if not ADD[i][j][k]:
            heappush(H, (A[i] + B[j] + C[k], i, j, k))
            ADD[i][j][k] = True
    x, y, z, k = map(int, input().split())
    INF = 10 ** 11
    A = sorted(list(map(lambda x: -int(x), input().split())))
    B = sorted(list(map(lambda x: -int(x), input().split())))
    C = sorted(list(map(lambda x: -int(x), input().split())))
    H = []
    ADD = [[[False] * z for _ in range(y)] for _ in range(x)]
    push(0, 0, 0)
    for _ in range(k):
        ans, i, j, k = heappop(H)
        push(i + 1, j, k)
        push(i, j + 1, k)
        push(i, j, k + 1)
        print(-ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

from heapq import heappop, heappush
def main():
    def push(i, j, k):
        nonlocal A, B, C, ADD
        if i >= x or j >= y or k >= z: return
        if (i, j, k) not in ADD:
            heappush(H, (A[i] + B[j] + C[k], i, j, k))
            ADD.add((i, j, k))
    x, y, z, k = map(int, input().split())
    INF = 10 ** 11
    A = sorted(list(map(lambda x: -int(x), input().split())))
    B = sorted(list(map(lambda x: -int(x), input().split())))
    C = sorted(list(map(lambda x: -int(x), input().split())))
    H = []
    ADD = set()
    push(0, 0, 0)
    for _ in range(k):
        ans, i, j, k = heappop(H)
        push(i + 1, j, k)
        push(i, j + 1, k)
        push(i, j, k + 1)
        print(-ans)

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

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

for _ in range(M):
    A.sort()
    A[-1] //= 2

print(sum(A))


### FIXED CODE ###

import heapq

N, M = map(int, input().split())
A = list(map(lambda x: int(x) * -1, input().split()))

heapq.heapify(A)

for _ in range(M):
    heapq.heappush(A, (heapq.heappop(A) + 1) // 2)

ans = sum(A) * -1

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

T,A,K=input().split()
T=int(T)
A=int(A)
K=int(K)

for i in range(K):
	if T>=1:
		T-=1
	elif A>=1:
		A-=1
	else:
		pass
      
print(T,A)


### FIXED CODE ###

T,A,K=input().split()
T=int(T)
A=int(A)
K=int(K)

if T>=K:
	T=T-K
	print(T,A)
elif T<K:
	K=K-T
	T=0
	A=A-K
	if A<0:
		A=0
		print(T,A)
	else:
		print(T,A)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
def quick_sort(a):
    if len(a)<= 1:
        return a
    else:
        b=[]
        c=[]
        for i in range(1, len(a)):
            if a[i]<a[0]:
                b.append(a[i])
            else:
                c.append(a[i])
        return quick_sort(b)+[a[0]]+quick_sort(c)
def abs(x):
    if x>=0:
        return x
    else:
        return x*(-1)
B=[]
for i in range(N):
  B.append(A[i]-i-1)
C=quick_sort(B)
if N%2==0:
  b=(C[N//2]+C[N//2-1])//2
else:
  b=C[N//2-1]
sad=0
for j in range(N):
  sad=sad+abs(C[j]-b)
print(sad)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
  B.append(A[i]-i-1)
B.sort()
if N%2==0:
  b=(B[N//2]+B[N//2-1])//2
else:
  b=B[N//2]
sad=0
for j in range(N):
  sad=sad+abs(B[j]-b)
print(sad)
### BUGGY CODE ###

# coding: utf-8
N = int(input())
A = list(map(int, input().split()))

for i in range(1, N+1):
    print(A.index(i)+1, end=" ")
print()

### FIXED CODE ###

# coding: utf-8
N = int(input())
A = list(map(int, input().split()))

lst = [0 for i in range(N)]
for i in range(N):
    lst[A[i]-1] = i+1

print(' '.join(map(str, lst)))
### BUGGY CODE ###

def is_ok(A, index, key):
    return A[index] >= key


def binary_search(A, key):
    ng = -1
    ok = len(A)

    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if is_ok(A, mid, key):
            ok = mid
        else:
            ng = mid

    return ok


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    for m in range(M):
        B, C = map(int, input().split())
        idx = binary_search(A, C)
        A[idx:idx] = [C] * B
        A = A[B:]

    print(sum(A))

main()


### FIXED CODE ###

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    h = {}
    for a in A:
        if a in h:
            h[a] += 1
        else:
            h[a] = 1
    for m in range(M):
        B, C = map(int, input().split())
        if C in h:
            h[C] += B
        else:
            h[C] = B
    nums = list(h.keys())
    nums.sort(reverse=True)
    c = 0
    s = 0
    for i in nums:
        if c + h[i] >= N:
            s += i * (N - c)
            break
        else:
            c += h[i]
            s += i * h[i]
    print(s)

main()
### BUGGY CODE ###

import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    n,m,k = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))
    B = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    ans = 0
    ab = A + B
    ab.sort()
    while k >= 0 and len(ab) > 0:
        if k >= ab[0]:
            k -= ab[0]
            ans += 1
            ab.pop(0)
        else:
            print(ans)
            exit()
    print(ans)
    exit()






if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    N,M,K = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))
    B = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    a = [0]
    for i in range(N):
        a.append(a[i] + A[i])
    b = [0]
    for i in range(M):
        b.append(b[i] + B[i])
    ans, j = 0, M
    for i in range(N + 1):
        if a[i] > K:
            break
        while b[j] > K - a[i]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)


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

import sys

input = sys.stdin.readline

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


dp = [[float("inf") for _ in range(N+1)] for _ in range(N+1)]
for i in range(N+1):
	dp[i][i] = 0

for len in range(1, N + 1):
	for i in range(N+1-len):
		j = i + len
		if ((N-len) % 2 == 0):
			dp[i][j] = max(dp[i+1][j] + lst_a[i], dp[i][j-1] + lst_a[j-1])
		else :
			dp[i][j] = min(-lst_a[i] + dp[i + 1][j], -lst_a[j - 1]+dp[i][j - 1])


print(dp[0][N])

### FIXED CODE ###

import sys

input = sys.stdin.readline

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


dp = [[0 for _ in range(N+1)] for _ in range(N+1)]

for len in range(1, N + 1):
	for i in range(N+1-len):
		j = i + len
		if ((N-len) % 2 == 0):
			dp[i][j] = max(dp[i+1][j] + lst_a[i], dp[i][j-1] + lst_a[j-1])
		else :
			dp[i][j] = min(-lst_a[i] + dp[i + 1][j], -lst_a[j - 1]+dp[i][j - 1])


print(dp[0][N])
### BUGGY CODE ###

n = int(input())
a = [0] * n
for i in range(n):
  a[i] = int(input())

ans = []
for i in range(n):
  count = a.count(a[i])
  if count %2 == 1 :
    ans.append(a[i])

ans = list(set(ans))
print(len(ans))

### FIXED CODE ###

N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())
A.sort()

#print(A)

check = [0]
for i in range(1, N):
    if A[i] != A[i-1]:
        check.append(i)
check.append(N)

l = []
for i in range(1, len(check)):
    tmp = check[i] - check[i-1]
    l.append(tmp)

#print(l)

ans = 0
for i in range(len(l)):
    if l[i] % 2 == 1:
        ans += 1

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

n = int(input())
a = [int(input()) for i in range(n)]
i = 1
count = 0
btn = [1]
while(1):
  i = a[i-1]
  count += 1
  if i in btn:
    print(-1)
    break
  if i == 2:
    print(count)
    break
  btn.append(i)

### FIXED CODE ###

n = int(input())
a = [int(input()) for i in range(n)]
i = 1
count = 0
btn = [1]
while(1):
  i = a[i-1]
  count += 1
  if count>n:
    print(-1)
    break
  if i == 2:
    print(count)
    break
  btn.append(i)
### BUGGY CODE ###

n, k = map(int, input().strip().split())
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
#print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)

### FIXED CODE ###

n, k = map(int, input().split())
s = [int(x) for x in list(input())]

state = 1
cnt = 0
l = []
for e in s:
    if e != state:
        l.append(cnt)
        cnt = 1
        state = e
    else:
        cnt += 1

if cnt != 0:
    l.append(cnt)


if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)

print(answer)
### BUGGY CODE ###

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

m = [[0]*n for _ in range(n)]
dp = [0]*n
from collections import deque
que = deque()
for _ in range(M):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  m[a][b] = 1
  m[b][a] = 1
  if a == 0:
    que.append(b)
    dp[b] = 1
  if b == 0:
    que.append(a)
    dp[a] = 1

while que:
  c = que.popleft()
  for i in range(n):
    if m[c][i] == 1 and dp[i] == 0 :
      dp[i] = c+1
      que.append(i)

[exit(print('No')) for i in range(1,n)  if dp[i] == 0]

print('Yes')
[print(dp[i]) for i in range(1,n)]


### FIXED CODE ###

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

m = [[] for _ in range(n)]
dp = [0]*n
from collections import deque
que = deque()
for _ in range(M):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  m[a].append(b)
  m[b].append(a)
  if a == 0:
    que.append(b)
    dp[b] = 1
  if b == 0:
    que.append(a)
    dp[a] = 1

while que:
  c = que.popleft()
  for i in m[c]:
    if dp[i] == 0 :
      dp[i] = c+1
      que.append(i)

#[exit(print('No')) for i in range(1,n)  if dp[i] == 0]

print('Yes')
[print(dp[i]) for i in range(1,n)]
### BUGGY CODE ###

"""
D - Lazy Faith
"""


def solve():
    # データ読込
    A, B, Q = map(int, input().split())
    si = [-float('inf')] + [int(input()) for i in range(A)] + [float('inf')]
    ti = [-float('inf')] + [int(input()) for i in range(B)] + [float('inf')]
    xi = [int(input()) for i in range(Q)]
    # 問題計算
    for x in xi:
        left_t = 0
        right_t = 0
        for i, t in enumerate(ti):
            if x <= t:
                left_t = ti[i - 1]
                right_t = ti[i]
                break
        left_s = 0
        right_s = 0
        for i, s in enumerate(si):
            if x <= s:
                left_s = si[i - 1]
                right_s = si[i]
                break
        # left_t = max([t for t in ti if t <= x])
        # right_t = min([t for t in ti if t >= x])
        # left_s = max([s for s in si if s <= x])
        # right_s = min([s for s in si if s >= x])
        print(min(abs(x - min(left_t, left_s)),
                  abs(x - max(right_t, right_s)),
                  abs(x - right_t) + abs(right_t - left_s),
                  abs(x - left_t) + abs(left_t - right_s),
                  abs(x - right_s) + abs(right_s - left_t),
                  abs(x - left_s) + abs(left_s - right_t)))


if __name__ == '__main__':
    solve()


### FIXED CODE ###

"""
D - Lazy Faith
"""
# 解説を参考に作成（bisect）
import bisect


def solve():
    # データ読込
    A, B, Q = map(int, input().split())
    si = [-float('inf')] + [int(input()) for i in range(A)] + [float('inf')]
    ti = [-float('inf')] + [int(input()) for i in range(B)] + [float('inf')]
    xi = [int(input()) for i in range(Q)]
    # 問題計算
    for x in xi:
        txi = bisect.bisect_left(ti, x)
        left_t = ti[txi - 1]
        right_t = ti[txi]
        sxi = bisect.bisect_left(si, x)
        left_s = si[sxi - 1]
        right_s = si[sxi]
        print(min(abs(x - min(left_t, left_s)),
                  abs(x - max(right_t, right_s)),
                  abs(x - right_t) + abs(right_t - left_s),
                  abs(x - left_t) + abs(left_t - right_s),
                  abs(x - right_s) + abs(right_s - left_t),
                  abs(x - left_s) + abs(left_s - right_t)))


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

# -*- coding: utf-8 -*-
# import numpy as np

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

Cn = [0]*(N+1)

for i,Ai in enumerate(An):
    if i == 0:
        Cn[i+1] = Ai
    else:
        Cn[i+1] = Cn[i] + Ai

# print(Cn)

ans = 0
for l in range(1,N+1):
    for r in range(l,N+1):
        if (Cn[r] - Cn[l-1])%M == 0:
            ans += 1

print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

Cn = [0]*(N+1)

for i,Ai in enumerate(An):
    if i == 0:
        Cn[i+1] = Ai
    else:
        Cn[i+1] = Cn[i] + Ai

# def func(n):
#     if n <= 1:
#         return 1
#     else:
#         tmp = 1
#         for i in range(2,n+1):
#             tmp = tmp * i
#         return tmp

mod = {}
for i in range(N+1):
    if Cn[i]%M in mod:
        mod[Cn[i]%M] += 1
    else:
        mod[Cn[i]%M] = 1

ans = 0
for val in mod.values():
    if val <= 1:
        continue
    elif val == 2:
        ans += 1
    else:
        # tmp = func(val) / (func(2) * func(val - 2))
        tmp = combinations_count(val,2)
        ans += int(tmp)

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

from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]
            del num_count[B]
            _sum = 0
            for k, v in num_count.items():
                _sum += k * v
            print(_sum)
        else:
            print(_sum)


if __name__ == "__main__":
    main()


### FIXED CODE ###

from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]

            _sum = _sum - (num_count[B] * B) + num_count[B] * C
            del num_count[B]
            print(_sum)
        else:
            print(_sum)


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

import numpy as np

N,K = map(int, input().split())
h = np.array(list(map(int, input().split())))
dp = np.full(N,float('inf'))
dp[0] = 0
for i in range(1,N):
    for j in range(max(0,i-K),i):
        # print(h[j],':',h[i])
        dp[i] = np.fmin(dp[i], dp[j]+ np.abs(h[j]-h[i]))
    # print(dp)
print(int(dp[-1]))

### FIXED CODE ###

import numpy as np

N,K = map(int, input().split())
h = np.array(list(map(int, input().split())))
dp = np.full(N,10**10)
dp[0] = 0
for i in range(1,N):
    start = max(0,i-K)
    dp[i] = np.min(dp[start:i]+np.abs(h[i]-h[start:i]))
print(int(dp[-1]))
### BUGGY CODE ###

from collections import Counter

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

# サイズA_maxのbool配列dpを用意し、trueで初期化する
A_max = max(A)
dp = [True] * (A_max + 1)

# 数列の要素を昇順に見る O(nloglogn)
for x in range(A_max):
    # すでに計算していれば再度計算しない
    if not dp[x]:
        continue
    # xが数列Aに含まれているとき、xより大きいxの倍数yについて、dp[y]をfalseに変更する
    if x in A:
        t = 2
        while x * t <= A_max:
            dp[x * t] = False
            t += 1

# 同じ値が複数存在するかチェックする用のカウンタ
C = {a: 0 for a in A}
for a in A:
    C[a] += 1

ans = 0
for a in A:
    if C[a] == 1 and dp[a] is True:
        ans += 1

print(ans)


### FIXED CODE ###

from collections import Counter

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

# 同じ値が複数存在するかチェックする用のカウンタ
counter = Counter(A)

# D[a] = True: Aにaより小さいaの約数が存在する
A_max = max(A)
dp = {a: False for a in A}

for a in A:
    # xが数列Aに含まれているとき、aより大きいaの倍数tについて、dp[a * t]をTrueに変更する
    if counter[a] > 0:
        t = 2
        while a * t <= A_max:
            dp[a * t] = True
            t += 1

ans = 0
for a in A:
    if counter[a] == 1 and dp[a] is False:
        ans += 1

print(ans)