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

import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def make_divisors(n):
    divisors = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            divisors.append(i)
            if i != n//i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


if __name__ == "__main__":
    n = int(input())
    a = sorted(list(map(int,input().split())))
    cnt = 0
    pattarn = []
    for i in range(n):
        b = make_divisors(a[i])
        pattarn.append(set(b))
    queue = collections.deque(a)
    for i in range(n):
        flag = 0
        p = queue.popleft()
        d = set(queue)
        e = pattarn[i]&d
        if len(e) == 0:
            cnt+=1
        queue.append(p)
    print(cnt)

### FIXED CODE ###

import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    a = sorted(list(map(int,input().split())))
    M = 1000005
    dp = [0]*(M)
    for i in range(n):
        if dp[a[i]] != 0:
            dp[a[i]] = 2
            continue
        for j in range(a[i],M,a[i]):
            dp[j]+=1
    cnt = 0
    for i in range(n):
        if dp[a[i]] == 1:
            cnt+=1
    print(cnt)
### BUGGY CODE ###

d, g = map(int, input().split())
p = []
c = []
for _ in range(d):
    _p, _c = map(int, input().split())
    p.append(_p)
    c.append(_c)

res = 0
count = 0
ans_list = []

def dfs(i, p, c, res, count):
    if i == 0:
        if res >= g:
            ans_list.append(count)
    else:
        for j in range(p[i - 1] + 1):
            _count = count + j
            _res = res + 100 * j * i
            if j == p[i - 1]:
                _res += c[i - 1]
            dfs(i - 1, p, c, _res, _count)

dfs(d, p, c, res, count)
print(min(ans_list))

### FIXED CODE ###

import numpy as np
d, g = map(int, input().split())
p = []
c = []
for _ in range(d):
    _p, _c = map(int, input().split())
    p.append(_p)
    c.append(_c)

s = np.arange(1, d + 1) * 100
p = np.array(p)

ans_list = []

bool_list = [False] * d

state_list = []

def dfs(i, n, bool_list):
    bool_list = bool_list.copy()
    if i == n:
        state_list.append(bool_list)
    else:
        dfs(i + 1, n, bool_list)
        bool_list[i] = True
        dfs(i + 1, n, bool_list)

dfs(0, d, bool_list)

for state in state_list:
    if np.dot(s * p, state) + np.dot(c, state) >= g:
        ans_list.append(np.dot(p, state))
    else:
        score = max(s * np.array([not i for i in state]))
        for j in range(1, p[score//100 - 1] + 1):
            if np.dot(s * p, state) + np.dot(c, state) + score * j >= g:
                ans_list.append(np.dot(p, state) + j)

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

import math

N = int(input())
sq = int(math.sqrt(N)) + 1
maxRange = 10**9

start = 1
for i in range(1, N) :
    if N // i < maxRange :
        start = i
        break

for i in range(start, sq) :
    if N % i == 0 :
        start = i
        break

print('0 0 0 {} {} 0'.format(start, N // start))

### FIXED CODE ###

S = int(input())
MOD = 10**9

y2 = -(-S // 10**9)
x2 = -(S - y2 * (10**9))

print('0 0 {} 1 {} {}'.format(MOD, x2, y2))
### BUGGY CODE ###

N = int(input())
a = list(map(int, input().split()))
mod=10**9+7
import fractions
lcm_m = a[0]%mod
for i in range(1, N):
#    lcm_m = lcm_m * a[i] // fractions.gcd(lcm_m, a[i])
#    lcm_m = (lcm_m%(10^9+7))*(a[i]%(10^9+7))//(fractions.gcd(lcm_m,a[i])%(10^9+7))
#    lcm_m = (lcm_m%(10**9+7))*a[i]//(fractions.gcd(lcm_m%(10**9+7),a[i]))
#    lcm_m = ((lcm_m%mod)*(a[i]%mod))%mod//(fractions.gcd(lcm_m,a[i]))
    lcm_m = ((lcm_m)*(a[i]%mod))//(fractions.gcd(lcm_m,a[i]))
#print(lcm_m)

#sol = 0
#for i in range(N):
#    sol += lcm_m / a[i]
#print(int(sol)%(10**9+7))
sol=sum([lcm_m//x for x in a])
print(sol%mod)
#print(sol)

### FIXED CODE ###

N = int(input())
a = list(map(int, input().split()))
mod=10**9+7
import fractions
lcm_m = a[0]%mod
for i in range(1, N):
#    lcm_m = lcm_m * a[i] // fractions.gcd(lcm_m, a[i])
#    lcm_m = (lcm_m%(10^9+7))*(a[i]%(10^9+7))//(fractions.gcd(lcm_m,a[i])%(10^9+7))
#    lcm_m = (lcm_m%(10**9+7))*a[i]//(fractions.gcd(lcm_m%(10**9+7),a[i]))
#    lcm_m = ((lcm_m%mod)*(a[i]%mod))%mod//(fractions.gcd(lcm_m,a[i]))
    lcm_m = ((lcm_m)*(a[i]%mod))//(fractions.gcd(lcm_m,a[i]%mod))
#print(lcm_m)

#sol = 0
#for i in range(N):
#    sol += lcm_m / a[i]
#print(int(sol)%(10**9+7))
sol=sum([lcm_m//x for x in a])
print(sol%mod)
#print(sol)
### BUGGY CODE ###

import copy

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

for i in range(0,n):
  c=0
  a_c=copy.copy(a)
  a_c.pop(i)
  a_uniq=list(set(a_c))
  for i in a_uniq:
    c+=a_c.count(i)*(a_c.count(i)-1)/2
  print('{:.0f}'.format(c))


### FIXED CODE ###

import copy

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

c=0

a_uniq=[0]*(n+1)

for i in a:
  a_uniq[i]+=1

for i in a_uniq:
  c+=sum(range(1,i))
#a_uniq=list(set(a))
#for i in a_uniq:
#  c+=sum(range(1, a.count(i)))

for i in a:
  print('{:.0f}'.format(c-a_uniq[i]+1))
### BUGGY CODE ###

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

B = [0] * N
for i in A:
    B[i-1] += 1

K = 1
F = [0] * N
for i in range(0,N):
    f = 0
    for j in range(N):
        f += min(B[j],N - i)
    f = f / (N - i)
    while K <= f:
        print(N - i)
        K += 1
    if K == N+1:
        break
    if i == (N - 1) and K != N+1:
        while K != N+1:
            print(0)
            K += 1

### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import Counter
from itertools import accumulate

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

B = list(Counter(A).values())

C = [0] * N 
for i in B:
    C[i-1] += 1

F = list(accumulate(C))

S = [0] * N
f = [0] * N
S[0] = f[0] = sum(C) 
for i in range(2, N+1):
    S[i-1] = (S[i-2] + C[i-1] + S[0] - F[i-1] ) 
    f[i-1] = S[i-1] / i

f.sort()
n = 0     
K = 1
    
while K != N+1:
    if K <= f[n]:
        print(N-n)
        K += 1
    else:
        n += 1
    if n == N:
        for i in range(N-K+1):
            print(0)
        K = N+1
### BUGGY CODE ###

import sys
input=sys.stdin.readline

cardamt,sousa=map(int,input().split())
cardlist=list(map(int,input().split()))
for i in range(sousa):
    maisu,number=map(int,input().split())
    newcard=[number]*maisu
    cardlist.extend(newcard)
cardlist.sort(reverse=True)
print(sum(cardlist[0:cardamt]))

### FIXED CODE ###

import sys
input=sys.stdin.readline
 
cardamt,sousa=map(int,input().split())
init=list(map(int,input().split()))
cardlist=[(1,a) for a in init]
for i in range(sousa):
    add=tuple(map(int,input().split()))
    cardlist.append(add)
cardlist.sort(reverse=True, key=lambda x:x[1])
result,totalcard,idx=0,0,0
while(totalcard<cardamt):
    newcardamt=cardlist[idx][0]
    if totalcard+newcardamt<=cardamt:
        totalcard=totalcard+newcardamt
        result=result+cardlist[idx][1]*newcardamt
    else:
        cardamtdelta=cardamt-totalcard
        totalcard=totalcard+cardamtdelta
        result=result+cardlist[idx][1]*cardamtdelta
    idx+=1
    #print(result,totalcard)
print(result)
### BUGGY CODE ###

# from math import factorial,sqrt,ceil #,gcd
# from itertools import permutations,combinations,combinations_with_replacement
# from collections import deque,Counter
# from bisect import bisect_left
# from heapq import heappush,heappop
# from numba import njit
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from fractions import gcd

# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np    # numpy.lcm()
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb,perm #permはnPk
# import networkx as nx
# G = Graph()

# slist = "abcdefghijklmnopqrstuvwxyz"
N,P = map(int,input().split())

ans = 1
i = 1
while i**N <= P:
    if P % (i**N)==0:
        ans = i
    i += 1
 
print(ans)
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))


### FIXED CODE ###

# from math import factorial,sqrt,ceil #,gcd
# from itertools import permutations,combinations,combinations_with_replacement
from collections import deque,Counter
# from bisect import bisect_left
# from heapq import heappush,heappop
# from numba import njit
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from fractions import gcd

# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np    # numpy.lcm()
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb,perm #permはnPk
# import networkx as nx
# G = Graph()

# slist = "abcdefghijklmnopqrstuvwxyz"
N,P = map(int,input().split())

def prime_factorize(n):  #nの素因数分解結果をリストで返す。Counterで素数ごとの指数確認可能 
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

cnt =Counter(prime_factorize(P))

ans = 1
for k,v in cnt.items():
    if v >= N:
        ans *= k**(v//N)

print(ans)
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))
### BUGGY CODE ###

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

### FIXED CODE ###

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

N=int(input())
B=list(map(int,input().split()))
def DP(L):
	if L==[]:
		return 1,[]
	ret=[]
	for i in range(len(L)):
		if L[i]==i+1:
			tL=L[:]
			del tL[i]
			f,dp=DP(tL)
			if f==1:
				return 1,[i+1]+dp
	return -1,[]
f,dp=DP(B)
if f==1:
	for d in dp[::-1]:
		print(d)
else:
	print(-1)

### FIXED CODE ###

N=int(input())
B=list(map(int,input().split()))
def DP(L):
	if L==[]:
		return 1,[]
	ret=[]
	for i in range(len(L))[::-1]:
		if L[i]==i+1:
			tL=L[:]
			del tL[i]
			f,dp=DP(tL)
			if f==1:
				return 1,[i+1]+dp
			else:
				break
	return -1,[]
f,dp=DP(B)
if f==1:
	for d in dp[::-1]:
		print(d)
else:
	print(-1)
### BUGGY CODE ###

n,max_w = map(int,input().split())
w=[]
v=[]

for i in range(n):
  tmp_w,tmp_v = map(int,input().split()) 
  w.append(tmp_w)
  v.append(tmp_v)

dp =[[-1]*(max_w+1) for i in range(n+1)]
  
#i番目以降の品物から、重さの総和がj以下となるように選ぶ
def knapsac(i,j):
  if dp[i][j]>=0:
    return dp[i][j]
  if i == n:
    #品物は残っていない
    dp[i][j] = 0
  elif j < w[i]:
    #品物が入らない
    dp[i][j] = knapsac(i+1,j)
  else:
    #品物が入る→品物を入れる場合と入れない場合を両方試す
    dp[i][j] = max(knapsac(i+1,j),knapsac(i+1,j-w[i])+v[i])
  return dp[i][j]

print(knapsac(0,max_w))

### FIXED CODE ###

n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])
### BUGGY CODE ###

import itertools


n, a, b, c = map(int, input().split())
L = [int(input())for _ in range(n)]
L_perm = list(itertools.permutations(L))
ans = 10**9
for l in L_perm:
    for i in range(1, n-1):
        A_tree = l[:i]
        synthesis_A = 10 * (i-1)
        fix_A = abs(a-sum(A_tree))
        for j in range(i+1, n):
            B_tree = l[i:j]
            synthesis_B = 10 * (j-i-1)
            fix_B = abs(b-sum(B_tree))
            for k in range(j+1, n+1):
                C_tree = l[j:k]
                synthesis_C = 10 * (k-j-1)
                fix_C = abs(c-sum(C_tree))

                total = fix_A + fix_B + fix_C + synthesis_A + synthesis_B + synthesis_C
                ans = min(ans, total)

print(ans)

### FIXED CODE ###

import itertools


n, a, b, c = map(int, input().split())
L = [int(input())for _ in range(n)]
L_perm = list(itertools.permutations(L))
ans = 10**9
for l in L_perm:
    for i in range(1, n-1):
        A_tree = l[:i]
        synthesis_A = 10 * (i-1)
        fix_A = abs(a-sum(A_tree))
        for j in range(i+1, n):
            B_tree = l[i:j]
            synthesis_B = 10 * (j-i-1)
            fix_B = abs(b-sum(B_tree))
            for k in range(j+1, n+1):
                C_tree = l[j:k]
                synthesis_C = 10 * (k-j-1)
                fix_C = abs(c-sum(C_tree))

                total = fix_A + fix_B + fix_C + synthesis_A + synthesis_B + synthesis_C
                ans = min(ans, total)

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

S = input()

L = []
for i in range(len(S)+1):
  for j in range(len(S)+1):
    if i<j:
      if int(S[i:j])>2018:
        L.append(S[i:j])

B = list(map(int,L))
A = list(filter(lambda x:x%2019==0,B))
print(len(A))



### FIXED CODE ###

S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)
### BUGGY CODE ###

while True:
    r,c = map(int, input().split())
    if (r|c)==0: break

    array = []
    for i in range(r):
        array.append(list(map(int, input().split())))
    num = [0]*c
    for i in range(r):
        for j in range(c):
            num[j] = num[j]*2+array[i][j]

    maxx = -1
    for i in range(1 << r):
        answer = 0
        for j in range(c):
            oksenbei = bin(num[j]^i).count("1")
            answer += max(oksenbei, r-oksenbei)
        maxx = max(maxx, answer)

    print(maxx)

### FIXED CODE ###

while True:
    r,c = map(int, input().split())
    if (r|c)==0: break

    num = [0]*c
    for i in range(r):
        instr = input()
        for j in range(c):
            num[j] = (num[j]<<1) + (instr[2*j]=="1")

    maxx = -1
    for i in range(1 << r):
        answer = 0
        for j in range(c):
            oksenbei = bin(num[j]^i).count("1")
            answer += oksenbei if oksenbei*2//r >= 1 else r-oksenbei
        if maxx < answer:
            maxx = answer

    print(maxx)
### BUGGY CODE ###

from operator import itemgetter
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
  l[i].sort()

ans=[]
l.sort()
B1=[];R1=[]
for i in range(n):
  B1.append(max(l[i]))
  R1.append(min(l[i]))
ans.append((max(B1)-min(B1))*(max(R1)-min(R1)))

Bleft=[]
Bright=[]
Rleft=[]
Rright=[]

M=B1[0];m=B1[0]
Bleft.append([m,M])
for i in range(1,n):
  M=max(M,B1[i])
  m=min(m,B1[i])
  Bleft.append([m,M])

B1.reverse()
M=B1[0];m=B1[0]
Bright.append([m,M])
for i in range(1,n):
  M=max(M,B1[i])
  m=min(m,B1[i])
  Bright.append([m,M])

M=R1[0];m=R1[0]
Rleft.append([m,M])
for i in range(1,n):
  M=max(M,R1[i])
  m=min(m,R1[i])
  Rleft.append([m,M])

R1.reverse()
M=R1[0];m=R1[0]
Rright.append([m,M])
for i in range(1,n):
  M=max(M,R1[i])
  m=min(m,R1[i])
  Rright.append([m,M])

for i in range(n-1):
  M1=max(Bleft[i][1],Rright[n-2-i][1])
  m1=min(Bleft[i][0],Rright[n-2-i][0])
  M2=max(Rleft[i][1],Bright[n-2-i][1])
  m2=min(Rleft[i][0],Bright[n-2-i][0])
  ans.append((M1-m1)*(M2-m2))

print(min(ans))

### FIXED CODE ###

import sys
input=sys.stdin.readline
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
  l[i].sort()

ans=[]
l.sort()
B1=[];R1=[]
for i in range(n):
  B1.append(max(l[i]))
  R1.append(min(l[i]))
ans.append((max(B1)-min(B1))*(max(R1)-min(R1)))

Bleft=[]
Bright=[]
Rleft=[]
Rright=[]

M=B1[0];m=B1[0]
Bleft.append([m,M])
for i in range(1,n):
  M=max(M,B1[i])
  m=min(m,B1[i])
  Bleft.append([m,M])

B1.reverse()
M=B1[0];m=B1[0]
Bright.append([m,M])
for i in range(1,n):
  M=max(M,B1[i])
  m=min(m,B1[i])
  Bright.append([m,M])

M=R1[0];m=R1[0]
Rleft.append([m,M])
for i in range(1,n):
  M=max(M,R1[i])
  m=min(m,R1[i])
  Rleft.append([m,M])

R1.reverse()
M=R1[0];m=R1[0]
Rright.append([m,M])
for i in range(1,n):
  M=max(M,R1[i])
  m=min(m,R1[i])
  Rright.append([m,M])

for i in range(n-1):
  M1=max(Bleft[i][1],Rright[n-2-i][1])
  m1=min(Bleft[i][0],Rright[n-2-i][0])
  M2=max(Rleft[i][1],Bright[n-2-i][1])
  m2=min(Rleft[i][0],Bright[n-2-i][0])
  ans.append((M1-m1)*(M2-m2))

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

import itertools
n, m = map(int, input().split())
P = [input().split() for i in range(m)]
P = list(itertools.chain.from_iterable(P))
ac_list = []
wa_list = [0] * n

counta = 0
countb = 0
for i in range(1, m * 2, 2):
    if P[i] == "AC" and int(P[i - 1]) not in ac_list:
        counta += 1
        ac_list.append(int(P[i - 1]))
        countb += wa_list[int(P[i - 1]) - 1]
    elif P[i] == "WA":
        wa_list[int(P[i - 1]) - 1] += 1
    else:
        continue

print(counta, countb)

### FIXED CODE ###

import itertools
n, m = map(int, input().split())
P = [input().split() for i in range(m)]
P = list(itertools.chain.from_iterable(P))
ac_list = [0] * n
wa_list = [0] * n
counta = 0
countb = 0
for i in range(1, m * 2, 2):
    if P[i] == "AC" and ac_list[int(P[i - 1])-1] == 0:
        counta += 1
        ac_list[int(P[i - 1])-1] = 1
        countb += wa_list[int(P[i - 1]) - 1]
    elif P[i] == "WA":
        wa_list[int(P[i - 1]) - 1] += 1
    else:
        continue

print(counta, countb)
### BUGGY CODE ###

N = int(input())
arr = list(input())
a = 0
b = 0
c = 0
count = 0
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    if arr[i] != arr[j]:
      for k in range(j + 1, N):
        if j - i != k - j:
          a = k
          b = j 
          c = i
          if arr[a] != arr[b] and arr[c] != arr[b] and arr[a] != arr[c]:
            count += 1
print(count)

### FIXED CODE ###

N = int(input())
arr = list(input())
a = arr.count("R")
b = arr.count("G")
c = arr.count("B")
count = a * b * c

for i in range(N - 2):
  for j in range(i + 1, N - 1):
    k = i + (j - i) * 2
    if k < N and arr[i] != arr[j] and arr[j] != arr[k] and arr[k] != arr[i]:
      count -= 1
print(count)
### BUGGY CODE ###

# 2019-11-16 14:51:41(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    job_list = []
    for _ in range(n):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a <= m:
            job_list.append(tuple([a, b]))

    job_list.sort(reverse=True, key=lambda x: x[1])
    rewards = [0 for _ in range(m+1)]
    count = 0
    for i in range(len(job_list)):
        for j in range(job_list[i][0], m+1):
            if rewards[j] == 0:
                rewards[j] = job_list[i][1]
                count += 1
                break
            else:
                continue
        if count == m:
            break
    
    print(sum(rewards))


if __name__ == "__main__":
    main()


### FIXED CODE ###

# 2019-11-16 14:51:41(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import heapq

def main():
    # 10**5連勤とか超人すぎる
    n, m = [int(x) for x in sys.stdin.readline().split()]

    job_list = dict((i, list()) for i in range(1, m+1))
    for _ in range(n):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a <= m:
            job_list[a].append(b)
    
    schedule = [0 for _ in range(m+1)] # 当日は報酬0
    q = []
    for i in range(1, m+1):
        for reward in job_list[i]: # 期限i日前にできる仕事の報酬一覧をheqp queueに入れる
            heapq.heappush(q, -reward) 
            # あとで最大値を取り出すために-1かける(heappop()で取り出せるのは最小値)

        if q: # その日できる仕事が一つでもあれば
            # 報酬の最小値(マイナスなので元々の最大値)を取り出して-1をかけ、scheduleにいれる
            schedule[i] = -heapq.heappop(q) # 戻り値を返すと同時にq自体も変更している
        else: # できる仕事がなければ
            pass  # その日の報酬は0のまま

    print(sum(schedule))


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

n,m = map(int, input().split())

l = [[] for _ in range(n)]
city = []
for i in range(m):
  p,y = map(int, input().split())
  city.append([p,y])
  l[p-1].append(y)

for i in l: i.sort()
for i in city:
  ans = ""
  ans += "0"*(6-len(str(i[0])))+str(i[0])
  t = str(l[i[0]-1].index(i[1])+1)
  ans += "0"*(6-len(t))+t
  print(ans)

### FIXED CODE ###

from bisect import bisect_left
n,m = map(int, input().split())
py = []
l = [[] for _ in range(n+1)]
for _ in range(m):
  p,y = map(int, input().split())
  l[p].append(y)
  py.append((p,y))
  
for i in l: i.sort()

for p,y in py:
  s = str(p)
  t = str(bisect_left(l[p],y)+1)
  ans = "0"*(6-len(s))+s + "0"*(6-len(t))+t
  print(ans)
### BUGGY CODE ###

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

ab.sort()

AB=[[ab[i][0]]*ab[i][1] for i in range(n)]
A=[]
for i in AB:
    A+=i
ans=0

for i in range(m):
    ans+=A[i]
print(ans)

### FIXED CODE ###

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

ab.sort()

ans=0
B=0
cnt=0
for a,b in ab:
    B+=b
    if B<m:
        ans+=a*b
        cnt+=b
    else:
        ans+=a*(m-cnt)
        print(ans)
        exit()
### BUGGY CODE ###

x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
e=[]
for ai in a:
    for bi in b:
        e.append(ai+bi)
e.sort(reverse=True)
e=e[:min(3001,x*y*z+1)]
ans=[]
for ci in c:
    for ei in e:
        ans.append(ci+ei)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])

### FIXED CODE ###

x,y,z,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
e=[]
for ai in a:
    for bi in b:
        e.append(ai+bi)
e.sort(reverse=True)
e=e[:k]
ans=[]
for ci in c:
    for ei in e:
        ans.append(ci+ei)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])
### BUGGY CODE ###

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

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1


    c = len(p)
    for j in range(len(p)):
        if not l <= p[j]:
            c -= 1

    for j in range(len(p)):
        if not (p[j] + 1) <= r:
            c -= 1

    print(c)


### FIXED CODE ###

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

n, q = read_line(int, int)
s, = read_line(str)

m = [0 for _ in range(len(s) + 1)]

c = 0
for i in range(len(s)):
    if s[i:i+2] == 'AC':
        c += 1
    m[i + 1] = c

for i in range(q):
    l, r = read_line(int, int)

    if c == 0:
        print(0)
        continue
    print(m[r - 1] - m[l - 1])
### BUGGY CODE ###

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

if k == 1:
    print(0)
else:
    for i in range(1000000000000000000):
        if n > abs(n-k):
            n = abs(n-k)
        elif n <= abs(n-k):
            break
    print(n)

### FIXED CODE ###

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

# if k == 1:
#     print(0)
# else:
#     for i in range(1000000000000000000):
#         if n > abs(n-k):
#             n = abs(n-k)
#         elif n <= abs(n-k):
#             break
#     print(n)
amari = n % k
if abs(amari-k) < amari:
    print(abs(amari-k))
else:
    print(amari)