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

import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc2=[]
for a in range(min([xyzk[3],len(ab)])):
    for b in abc[2]:
        abc2.append(b+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])

### FIXED CODE ###

import math
xyzk = list(map(int,input().split()))
abc = [list(map(int,input().split())) for a in range(3)]
ab=[]
for a in abc[0]:
    for b in abc[1]:
        ab.append(a+b)
ab.sort()
ab.reverse()
abc[2].sort()
abc[2].reverse()
abc2=[]
for c in range(len(abc[2])):
    for a in range(min([math.ceil(xyzk[3]/(c+1)),len(ab)])):
        abc2.append(abc[2][c]+ab[a])
abc2.sort()
abc2.reverse()
for a in range(xyzk[3]):
    print(abc2[a])
### BUGGY CODE ###

a, b, k = map(int, input().split())

for i in range(k):
    if (a >= 1): 
        a -= 1
    elif (b >= 1): 
        b -= 1
    else:
        break

print(str(a) + " " + str(b))

### FIXED CODE ###

a, b, k = map(int, input().split())

if a>=k:
    print(a-k, b)
elif a+b>=k:
    print(0, b-k+a)
else:
    print(0, 0)
### BUGGY CODE ###

import sys, math
from collections import deque

readline = sys.stdin.readline

S = int(readline())

M = 10 ** 9
rS = math.ceil(math.sqrt(S))

while True:
    rem = rS ** 2 - S


    if rem <= M:
        b = rem
        break
    else:
        b = 0
        a = 2
        c = math.floor(rem ** 0.5)
        while a < c:
            if rem % a == 0:
                b = a
                break
            a += 1
        if b != 0:
            break
    
    if rS == M:
        rS -= 50
    else:
        rS += 1
while True:
    a = 1 + 1
print('0 0 {} {} {} {}'.format(rS, b, rem // b, rS))

### FIXED CODE ###

import sys, math
from collections import deque

readline = sys.stdin.readline

S = int(readline())

M = 10 ** 9
rS = math.ceil(math.sqrt(S))

while True:
    rem = rS ** 2 - S

    if rem == 0:
        print('0 0 {} {} {} {}'.format(rS, 0, 0, rS))
        exit(0)

    if rem <= M:
        b = rem
        break
    else:
        b = 0
        a = rem // M + 1
        c = math.floor(rem ** 0.5)
        while a < c and a < M:
            if rem % a == 0:
                b = a
                break
            a += 1
        if b != 0:
            break
    
    if rS >= M:
        rS = M - 1000
    else:
        rS += 1


print('0 0 {} {} {} {}'.format(rS, b, rem // b, rS))
### BUGGY CODE ###

n = int(input())
ans = 0
a = []
for i in range(n):
    a.append(int(input()))
t = list(set(a))
for i in t:
    if a.count(i) % 2 == 1:
        ans += 1
print(ans)

### FIXED CODE ###

from collections import Counter
n = int(input())
a = Counter([input() for i in range(n)])
print(len([x for x in a.values() if x % 2 == 1]))
### BUGGY CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def main():
    N = ir()
    P = lr()
    dp = [[min(i, N-1-i)] * N for i in range(N)]
    people = [[True] * N for i in range(N)]
    for j in range(N):
        x = min(j, N-1-j)
        for i in range(N):
            dp[i][j] = min(dp[i][j], x)

    answer = 0
    for p in P:
        p -= 1
        i = p // N
        j = p % N
        answer += dp[i][j]
        people[i][j] = False
        que = deque([(i, j, dp[i][j])])
        popleft = que.popleft; append = que.append
        while que:
            y, x, cur = popleft()
            cur += people[y][x]
            for dy, dx in zip([0, 1, 0, -1], [1, 0, -1, 0]):
                ny = y + dy
                nx = x + dx
                if 0 <= ny < N and 0 <= nx < N:
                    if dp[ny][nx] > cur:
                        dp[ny][nx] = cur
                        append((ny, nx, cur))

    print(answer)
    
if __name__ == "__main__":
    main()


### FIXED CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def main():
    N = ir()
    P = lr()
    P = [((p-1)//N, (p-1)%N) for p in P]
    dp = [[min(i, N-1-i, j, N-1-j) for j in range(N)] for i in range(N)]
    people = [[True] * N for i in range(N)]

    answer = 0
    for i, j in P:
        answer += dp[i][j]
        people[i][j] = False
        que = deque([(i, j, dp[i][j])])
        popleft = que.popleft; append = que.append
        while que:
            y, x, cur = popleft()
            cur += people[y][x]
            if x > 0:
                if dp[y][x-1] > cur:
                    dp[y][x-1] = cur
                    append((y, x-1, cur))
            if x < N-1:
                if dp[y][x+1] > cur:
                    dp[y][x+1] = cur
                    append((y, x+1, cur))
            if y > 0:
                if dp[y-1][x] > cur:
                    dp[y-1][x] = cur
                    append((y-1, x, cur))
            if y < N-1:
                if dp[y+1][x] > cur:
                    dp[y+1][x] = cur
                    append((y+1, x, cur))

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

def main():
  n=int(input())
  ans=0
  l=[10**9+1]
  import bisect
  for i in range(n):
    a=int(input())
    num=bisect.bisect_left(l,a)
    if l[num]==a:
      ans-=1
      del l[num]
    else:
      l.append(a)
      l.sort()
      ans+=1
  print(ans)
if __name__=='__main__':
  main()


### FIXED CODE ###

n=int(input())
ans=0
l=[int(input()) for i in range(n)]
from collections import Counter
lst=list(Counter(l).values())
def mod2(n):
  if n%2==1:
    return 1
  else:
    return 0
ans=0
for i in lst:
  ans+=mod2(i)
print(ans)
### BUGGY CODE ###

def insert(k):
    a.append(-1)
    heap_increasekey(a, len(a) - 1, k)
def heap_increasekey(a, i, k):
    a[i] = k
    while i > 1 and a[int(i / 2)] < a[i]:
        a[i], a[int(i / 2)] = a[int(i / 2)], a[i]
        i = int(i / 2)
def heap_extractmax(a):
    m = a[1]
    a[1] = a[-1]
    a.pop()
    max_heapify(a, 1)
    return m

def max_heapify(a, i):
    l = 2 * i
    r = 2 * i + 1
    largest = l if l < len(a) and a[l] > a[i] else i
    if r < len(a) and a[r] > a[largest]:
        largest = r
    if largest != i:
        a[largest], a[i] = a[i], a[largest]
        max_heapify(a, largest)
        
a = [0]
while True:
    s = input()
    if s[2] == "d":
        break
    elif s[0] == "i":
        insert(int(s[7:]))
    else:
        print(heap_extractmax(a))



### FIXED CODE ###

from heapq import *
a = []
while True:
    s = input()
    if s[2] == "d":break
    elif s[0] == "i":heappush(a, -int(s[7:]))
    else:print(-heappop(a))
### BUGGY CODE ###

n,m,q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(m)]
B = [list(map(int, input().split())) for _ in range(q)]
l = [[0]*n for _ in range(n)]
for i in range(m):
    l[A[i][0]-1][A[i][1]-1] += 1
#print(l)

a = [[0]*n for _ in range(n)] #差に対して
for i in range(n):
    for j in range(n-i):
        if (j==0):
            a[i][j] = l[0][i]
        else:
            a[i][j] = a[i][j-1] + l[j][j+i]
#print(a)
        
ans = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(i,n):
        x = 0
        for k in range(j-i+1):
            #print(k)
            if (i!=0):
                x += a[k][j-k]-a[k][i-1]
            else:
                x += a[k][j-k]
        ans[i][j] = x
        #print(i,j,x)
#print(ans)
            

for i in range(q):
    print(ans[B[i][0]-1][B[i][1]-1])
       

### FIXED CODE ###

n,m,q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(m)]
B = [list(map(int, input().split())) for _ in range(q)]
l = [[0]*n for _ in range(n)]
for i in range(m):
    l[A[i][0]-1][A[i][1]-1] += 1
#print(l)

a = [[0]*n for _ in range(n)] #差に対して
for i in range(n):
    for j in range(n-i):
        if (j==0):
            a[i][j] = l[0][i]
        else:
            a[i][j] = a[i][j-1] + l[j][j+i]
#print(a)
        
ans = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(i,n):
        x = 0
        for k in range(j-i+1):
            #print(k)
            if (i!=0):
                x += a[k][j-k]-a[k][i-1]
            else:
                x += a[k][j-k]
        ans[i][j] = x
        #print(i,j,x)
#print(ans)
            

for i in range(q):
    print(ans[B[i][0]-1][B[i][1]-1])
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
import itertools
flag = True
while(flag):
    count = 0
    A_2 = list(itertools.combinations(A,2))
    for comb in A_2:
        diff = abs(comb[0] - comb[1])
        if diff not in A:
            A.append(diff)
        else:
            count += 1
    else:
        if (count == len(A_2)) or (K in A):
            flag = False
if K in A:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')



### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
import fractions
import functools
gcd = functools.reduce(fractions.gcd, A)
A_max = max(A)
if (A_max >= K and K % gcd == 0):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
### BUGGY CODE ###

K = int(input())

if (K%10) % 2 == 0:
    pass
    print(-1)

elif (K%10) % 5 == 0:
    pass
    print(-1)
else:
    num = 7
    for i in range(1000000):
        if num % K == 0:
            print(str(i+1))
            break
        num = num *10 + 7
    else:
        print(-1)



### FIXED CODE ###

K = int(input())

t = 7
for i in range(K):
    if t % K == 0:
        print(i + 1)
        break
    t = (t * 10 + 7) % K
else:
    print(-1)
### BUGGY CODE ###

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

ans=1

for i in range(N):
    ans = ans * A[i]


if ans > 10**18:
    print(-1)
    exit()
else:
    print(ans)

### FIXED CODE ###

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

if 0 in A:
    print(0)
    exit()

ans=1

for i in range(N):
    ans = ans * A[i]
    if ans > 10**18:
        print(-1)
        exit()

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

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


A, B = list(map(int, input().split()))
# A, B = [12, 18]
# A, B = [420, 660]
# A, B = [1, 2019]


# 1. A,B の gcd を取得して C とする
# 2. C を素因数分解する
# 3. 「素因数分解の数 + 1」を返す

def gcd(a, b):
    # a の方が大きいと仮定
    def _gcd(a, b):
        # print(a, b)
        if a % b == 0:
            return b
        else:
            return gcd(b, a % b)

    if a < b:
        return _gcd(b, a)
    else:
        return _gcd(a, b)


def factorize(x):
    res = {}
    for i in range(2, x):  # x まで行かずともよさそう
        if x % i == 0:
            while x % i == 0:
                x //= i
                res[i] = res.get(i, 0) + 1

    if x != 1:
        res[x] = 1

    return res

C = gcd(A, B)
# print('gcd', C)
factors = factorize(C)
# print(factors)
print(len(factors) + 1)


### FIXED CODE ###

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


A, B = list(map(int, input().split()))
# A, B = [12, 18]
# A, B = [420, 660]
# A, B = [1, 2019]


# 1. A,B の gcd を取得して C とする
# 2. C を素因数分解する
# 3. 「素因数分解の数 + 1」を返す

def gcd(a, b):
    # a の方が大きいと仮定
    def _gcd(a, b):
        # print(a, b)
        if a % b == 0:
            return b
        else:
            return gcd(b, a % b)

    if a < b:
        return _gcd(b, a)
    else:
        return _gcd(a, b)


def factorize(x):
    res = {}
    for i in range(2, int(math.sqrt(x) + 1)):  # x まで行かずともよさそう
        if x % i == 0:
            while x % i == 0:
                x //= i
                res[i] = res.get(i, 0) + 1

    if x != 1:
        res[x] = 1

    return res

C = gcd(A, B)
factors = factorize(C)
print(len(factors) + 1)
### BUGGY CODE ###

from collections import Counter
n = int(input())
ll = [Counter(input()) for _ in range(n)]
c = 0
for i, s in enumerate(ll):
    c += len([j for j in range(i + 1, n) if s == ll[j]])
print(c)

### FIXED CODE ###

from collections import Counter
import numpy as np
n = int(input())
ll = []
ans = 0
for _ in range(n):
    c = list(input())
    c.sort()
    ll.append("".join(c))
ans = np.sum([np.arange(v).sum() for v in Counter(ll).values() if v > 1])
print(int(ans))
### BUGGY CODE ###

n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
from collections import deque
toku=[0]*(n+1)
for i in range(1,n+1):
  ans=[0]
  d=deque()
  visit=[0]*(n+1)
  d.append(i)
  while d:
    now=d.pop()
    if visit[now]==0:
      visit[now]=1
      ans.append(ans[-1]+c[p[now-1]-1])
      d.append(p[now-1])
  syuki=sum(visit)
  if ans[-1]>=0:
    if k%syuki==0:
      toku[i]=max((k//syuki)*ans[-1],(k//syuki-1)*ans[-1]+max(ans))
      
    else:
      toku[i]=(k//syuki)*ans[-1]+max(ans[1:k%syuki+1])
  else:
    if k//syuki==0:
      toku[i]=max(ans[1:k%syuki+1])
    else:
      toku[i]=max(ans[1:])
print(max(toku[1:]))      
    

### FIXED CODE ###

n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
from collections import deque
toku=[0]*(n+1)
for i in range(1,n+1):
  ans=[0]
  d=deque()
  visit=[0]*(n+1)
  d.append(i)
  while d:
    now=d.pop()
    if visit[now]==0:
      visit[now]=1
      ans.append(ans[-1]+c[p[now-1]-1])
      d.append(p[now-1])
  syuki=sum(visit)
  if ans[-1]>=0:
    if k%syuki==0:
      toku[i]=max((k//syuki)*ans[-1],(k//syuki-1)*ans[-1]+max(ans))
      
    else:
      toku[i]=(k//syuki)*ans[-1]+max(ans[1:k%syuki+1])
  else:
    if k//syuki==0:
      toku[i]=max(ans[1:k%syuki+1])
    else:
      toku[i]=max(ans[1:])
print(max(toku[1:]))
### BUGGY CODE ###

import math
n = int(input())
s = [list(input()) for S in range(n)]
cnt = 0
for i in range(n):
	s[i].sort()
duplicated_data = [tuple(d) for d in s]
unique_data = set(duplicated_data)
s_set = [list(d) for d in unique_data]
for j in range(len(s_set)):
	if s.count(s_set[j]) >= 2:
		k = s.count(s_set[j])
		cnt += math.factorial(k)//(math.factorial(k-2)*math.factorial(2))
print(cnt)

### FIXED CODE ###

n = int(input())
#文字列を同一か判別するために全てsortして格納する
#インプットした文字列をソートして文字列として格納する
s = [''.join(sorted(input())) for _ in range(n)]
#babacaba >>> aaaabbbcとする
######################
#同一性を判定するのにはハッシュテーブルを活用すると良い
#辞書はハッシュテーブルとして高速なために使用
s_dict = {}
for i in range(n):
	if s[i] in s_dict:
		s_dict[s[i]] += 1
	else:
		s_dict[s[i]] = 1
		#辞書内になくとも自動で追加される
		#>>>{xxx:1 yyy:1 zzz:1}
ans = 0
#辞書内の加算された数字についてnCrで2つの組み合わせを検討する
for j in s_dict.values():
	ans += j*(j-1)//2
	#xxxについて、2以上あれば1組は存在するということで+
print(ans)
### BUGGY CODE ###

n = int(input())
p = list(map(lambda x: int(x), input().split()))
 
count = 0
for i in range(n):
  a = p[i]
  amin = min(p[:i+1])
  if amin == a:
    count += 1
  
print(count)

### FIXED CODE ###

n = int(input())
p = list(map(lambda x: int(x), input().split()))
 
count = 0
maxs = p[0]
for i in range(n):
  if maxs >= p[i]:
    maxs = p[i]
    count += 1
print(count)
### BUGGY CODE ###

from bisect import bisect_right
N,Q,*L = map(int, open(0).read().split())
D = L[3*N:]
ls = [0]*N
for i in range(N):
  s,t,x = L[3*i],L[3*i+1],L[3*i+2]
  ls[i] = (x,s,t)
ls.sort()
line = [0]*(2*N)
road = [0]*(2*N)
for i in range(N):
  x,s,t = ls[i]
  line[2*i] = (s-x,0,i)
  line[2*i+1] = (t-x,1,i)
  road[2*i] = s-x
  road[2*i+1] = t-x
line.sort()
road.sort()
inf = [0]*(2*N)
m = 0
for i in range(2*N):
  d,p,j = line[i]
  if p:
    m -= 1<<j
  else:
    m += 1<<j
  inf[i] = m
for d in D:
  i = bisect_right(road,d)
  if i==2*N or i==0:
    print(-1)
    continue
  m = inf[i-1]
  s = m&-m
  if s==0:
    print(-1)
  else:
    ind = len(bin(s)[2:])-1
    print(ls[ind][0])

### FIXED CODE ###

#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

N,Q = map(int,input().split())
task = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  task.append((t-x,0,x)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

# 引っかかってる場所の管理
se = set()
se_hp = [] # heapで最小値を先頭に保つ

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    while se_hp and se_hp[0] not in se:
      heappop(se_hp)
    answer[c] = se_hp[0] if se_hp else -1

print('\n'.join(map(str,answer)))
### BUGGY CODE ###

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

M = 0
JM = 0
for i in range(N-1):
  JM =0
  for j in range(i+1,N):
    JM += A[j]
  if JM > 10**9 + 7:
    JM = JM % (10 ** 9 + 7)
  M+= JM * A[i]
  if M > 10 ** 9 + 7:
    M = M % (10 ** 9 + 7)
    
print(M)

### FIXED CODE ###

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

M = 0
sum = 0
for i in range(N):
  sum += A[i]

for i in range(N-1):
  sum = sum - A[i]
  M += A[i] * sum
  if M > 10 ** 9 + 7:
    M = M % (10 ** 9 + 7)
print(M)
### BUGGY CODE ###

def divisible(n):
    ans = 0
    while n%2 == 0:
        n /=2
        ans +=1
    return ans

N = int(input())

b = 0

for i in range(N):
    if(divisible(i) > b):
      b = divisible(i)
      ans = i
    
print(max(ans))

### FIXED CODE ###

def divisible(n):
 ans = 0
 while n%2 == 0:
  n /= 2
  ans += 1
 return ans
 
N = int(input())
 
b = 0

c = 1
 
for i in range(1,N+1):
    if(divisible(i) > b):
      b = divisible(i)
      c = i
    
print(c)
### BUGGY CODE ###

N,K = input().split()

CalcList = list(range(int(K), int(N)+2))

#print(CalcList)

ans = 0

for i in CalcList:
    tmp = 0
    min = sum(range(0,i))
#    print(min)
    max = i * int(N) -min
#    print(max)

    tmp = max - min +1
    ans += tmp

print(ans % 1000000007)

### FIXED CODE ###

N,K = input().split()

CalcList = tuple(range(int(K), int(N)+2))
N = int(N)
#print(CalcList)

ans = 0

for i in CalcList:
    tmp = 0
    min = i * (i-1) /2
#    print(min)
    max = i * N -min
#    print(max)

    tmp = max - min +1
    ans += tmp

ans = ans % (10 ** 9 +7)
    
print(int(ans))
### BUGGY CODE ###

N, K = map(int, input().split())
MOD = 998244353
S = []
for _ in range(K):
    S.append(tuple(map(int, input().split())))

dp = [0] * (N + 1)
dp[1] = 1

for i in range(2, N+1):
    for L, R in S:
        RR = i - L
        if RR <= 0:
            continue
        LL = max(1, i-R)
        dp[i] += sum(dp[LL:RR+1])
    dp[i]

print(dp[N] % MOD)


### FIXED CODE ###

N, K = map(int, input().split())
MOD = 998244353
S = []
for _ in range(K):
    S.append(tuple(map(int, input().split())))

dp = [0] * (N + 1)
dp[1] = 1
sum_list = [0] * (N + 1)
sum_list[1] = 1

for i in range(2, N+1):
    for L, R in S:
        RR = i - L
        if RR <= 0:
            continue
        LL = max(1, i-R)
        if LL <= RR:
            t = sum_list[RR] - sum_list[LL-1]
            dp[i] += t
    sum_list[i] = (sum_list[i-1] + dp[i]) % MOD
    dp[i] %= MOD

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

N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
stack = [A[1]]
idx_list = [1]
while True:
    last = A[stack[-1]]
    if last in stack:
        ind = stack.index(last)
        if ind == stack[-1]:
            break
    idx_list.append(stack[-1])
    stack.append(last)
    if len(stack) == K:
        print(stack[-1])
        exit(0)
mod = len(stack[ind:])
idx = ind + (K-ind-1)%mod
print(stack[idx])

### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x)-1, input().split()))
    now = 0
    dict_ = {}
    stack = []
    for i in range(N):
        if now not in dict_:
            dict_[now] = A[now]
            stack.append(now)
            now = A[now]
        else:
            idx = stack.index(now)
            mod = i - idx
            break
    idx = idx + (K-idx) % mod
    idx = min(K, idx)
    print(stack[idx]+1)


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

from math import ceil
r,g,b,n = map(int, input().split())

ans = 0
for i in range(ceil(n/r)+1):
  for j in range(ceil(n/g)+1):
    if n - (r*i + g*j) >= 0 and (n - (r*i + g*j)) % b == 0: ans += 1
print(ans)

### FIXED CODE ###

from math import ceil
r,g,b,n = map(int, input().split())

ans = 0
for i in range(ceil(n/r)+1):
  for j in range(ceil(n/g)+1):
    if n - (r*i + g*j) >= 0 and (n - (r*i + g*j)) % b == 0: ans += 1
print(ans)
### BUGGY CODE ###

from collections import defaultdict
N,M=map(int,input().split())
x=defaultdict(list)
for i in range(N):
    a,b=map(int,input().split())
    x[a].append(b)
y=[]
an=0
for i in range(M+1):
    y+=x[i]
    y.sort()
    try:
        an+=y.pop()
    except:
        pass
print(an)

### FIXED CODE ###

import heapq
N,M=map(int,input().split())
x={}
for i in range(M+1):
    x[i]=[]
for i in range(N):
    a,b=map(int,input().split())
    if a>M:
        continue
    x[a].append(-b)
y=[]
an=0
for i in range(M+1):
    for n in x[i]:
        heapq.heappush(y,n)
    try:
        an-=heapq.heappop(y)
    except:
        pass
print(an)
### BUGGY CODE ###

import heapq
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
b=[]
for item in bc:
  b=b+[-item[1],]*item[0]

heapq.heapify(b) 

ans=0
for i in range(n):
  if len(b)==0:
    ans=ans+sum(a[i:])
    break
  else:
    tmp=-heapq.heappop(b)
    if a[i]<tmp:
      ans=ans+tmp
    else:
      ans=ans+sum(a[i:])
      break

print(ans)


### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)
### BUGGY CODE ###

n,x=map(int,input().split())
l=list(map(int,input().split()))

def make_divisor_list(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list

if n==1:
    print(abs(x-l[0]))
else:
    l.append(x)
    minimum=min(l)
    maximum=max(l)
    delta=maximum-minimum
    if delta==1:
        print(1)
    else:
        kouho=make_divisor_list(delta)
        ans=1
        for num in kouho:
            signal=1
            j=0
            while(j<n):
                if (l[j]-minimum)%num!=0:
                    signal=0
                    break
                else:
                    j+=1
            if signal==1:
                ans=num
        print(ans)


### FIXED CODE ###

n,x=map(int,input().split())
l=list(map(int,input().split()))

def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

kouho=[abs(l[i]-x) for i in range(n)]

ans=kouho[0]

for i in range(n-1):
  ans = gcd(ans,kouho[i+1])
  
print(ans)
### BUGGY CODE ###

N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
score = 1
tmp = 0
for i in range(K):
    score *= A[i]
for j in range(N-K):
    tmp = score
    score = score * A[j+K] / A[j]
    if score > tmp:
        print("Yes")
    else:
        print("No")


### FIXED CODE ###

N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
for j in range(N-K):
    if A[j+K] > A[j]:
        print("Yes")
    else:
        print("No")