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

import sys
input = sys.stdin.readline

n = int(input().rstrip('\n'))
s = input().rstrip('\n')
l = [c for c in s]
q = int(input().rstrip('\n'))
Q = [input().rstrip('\n').split() for _ in range(q)]

class SegTree():
    def __init__(self, n_):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.tree = [2 for _ in range(2 * self.n - 1)]
    def update(self, k, a):
        k += self.n - 1
        self.tree[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
    def query(self, l, r):
        l += self.n - 1
        r += self.n - 2
        res = 2
        while r-l>1:
            if l&1 == 0:
                res = min(res,self.tree[l])
            if r&1 == 1:
                res = min(res,self.tree[r])
                r -= 1
            l = l//2
            r = (r-1)//2
        if l == r:
            res = min(res,self.tree[l])
        else:
            res = min(min(res,self.tree[l]),self.tree[r])
        return res

d = { c: SegTree(n) for c in "abcdefghijklmnopqrstuvwxyz"}

for i, c in enumerate(s):
    d[c].update(i, 1)


for a, b, c in Q:
    if a == "1":
        b = int(b) - 1
        d[l[b]].update(b, 2)
        d[c].update(b, 1)
        l[b] = c
    else:
        res = 0
        b, c = int(b), int(c)
        for key in d:
            if d[key].query(b-1, c) == 1:
                res += 1
        print(res)


### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input().rstrip('\n'))
s = input().rstrip('\n')
l = [c for c in s]
q = int(input().rstrip('\n'))
Q = [input().rstrip('\n').split() for _ in range(q)]

class SegTree():
    def __init__(self, n_):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.tree = [2 for _ in range(2 * self.n - 1)]
    def update(self, k, a):
        k += self.n - 1
        self.tree[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
    def query(self, l, r):
        l += self.n - 1
        r += self.n - 2
        res = 2
        while r-l>1:
            if l&1 == 0:
                res = min(res,self.tree[l])
            if r&1 == 1:
                res = min(res,self.tree[r])
                r -= 1
            l = l//2
            r = (r-1)//2
        if l == r:
            res = min(res,self.tree[l])
        else:
            res = min(min(res,self.tree[l]),self.tree[r])
        return res

d = { c: SegTree(n) for c in "abcdefghijklmnopqrstuvwxyz"}

for i, c in enumerate(s):
    d[c].update(i, 1)


for a, b, c in Q:
    if a == "1":
        b = int(b) - 1
        d[l[b]].update(b, 2)
        d[c].update(b, 1)
        l[b] = c
    else:
        res = 0
        b, c = int(b), int(c)
        for key in d:
            if d[key].query(b-1, c) == 1:
                res += 1
        print(res)
### BUGGY CODE ###

# -*- coding: utf-8 -*-

n = int(input())
s = []
h = []
c = []
d = []

for i in range(13):
    s.append(i + 1)
    h.append(i + 1)
    c.append(i + 1)
    d.append(i + 1)

for j in range(n):
    x = list(input().split())
    if x[0] is 'S':
        s.remove(int(x[1]))
    elif x[0] is 'H':
        h.remove(int(x[1]))
    elif x[0] is 'C':
        c.remove(int(x[1]))
    elif x[0] is 'D':
        d.remove(int(x[1]))

if len(s) != 0:
    for i in range(len(s)):
        print('{0} {1}'.format('S', s[i]))

if len(h) != 0:
    for i in range(len(h)):
        print('{0} {1}'.format('H', h[i]))

if len(c) != 0:
    for i in range(len(c)):
        print('{0} {1}'.format('C', c[i]))

while len(d) != 0:
    for i in range(len(d)):
        print('{0} {1}'.format('D', d[i]))


### FIXED CODE ###

# -*- coding: utf-8 -*-

n = int(input())
s = []
h = []
c = []
d = []

for i in range(13):
    s.append(i + 1)
    h.append(i + 1)
    c.append(i + 1)
    d.append(i + 1)

for j in range(n):
    x = list(input().split())
    if x[0] is 'S':
        s.remove(int(x[1]))
    elif x[0] is 'H':
        h.remove(int(x[1]))
    elif x[0] is 'C':
        c.remove(int(x[1]))
    elif x[0] is 'D':
        d.remove(int(x[1]))

if len(s) != 0:
    for i in range(len(s)):
        print('{0} {1}'.format('S', s[i]))

if len(h) != 0:
    for i in range(len(h)):
        print('{0} {1}'.format('H', h[i]))

if len(c) != 0:
    for i in range(len(c)):
        print('{0} {1}'.format('C', c[i]))

if len(d) != 0:
    for i in range(len(d)):
        print('{0} {1}'.format('D', d[i]))
### BUGGY CODE ###

import itertools
if __name__ == '__main__':
    n=int(input())
    string=input()
    string_set=set(string)
    div=10**9+7
    count=0
    Dict={}
    for i,s in enumerate(string):
        if s not in Dict.keys():
            Dict[s]=1
        else:
            Dict[s] += 1
        count+=1
    if len(string_set)>1:
        for i in range(2,len(string_set)+1):
            for x in itertools.combinations(Dict.keys(),i):
                temp = 1
                for y in x:
                    temp*=Dict[y]
                count+=temp
    print(count%div)

### FIXED CODE ###

if __name__ == '__main__':
    n=int(input())
    string=input()
    string_set=set(string)
    div=10**9+7
    count=1
    for s in string_set:
        count*=string.count(s)+1
    count-=1
    print(count%div)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n, m = [ int(v) for v in input().split() ]
num_list = sorted([ int(v) for v in input().split() ])
change_list = []
for i in range(m):
	a,b = [ int(v) for v in input().split() ]
	change_list += [b]*a
change_list = sorted(change_list,reverse = True)

x = len(change_list)
if n >= x:
	change_list += ([0]*(n-x))
else:
	change_list = change_list[:n]


anslist = [ max(num_list[i], change_list[i])for i in range(n)]
print(sum(anslist))


### FIXED CODE ###

import sys
input = sys.stdin.readline

def swap(a):
    return ((a[1],a[0]))


n, m = [ int(v) for v in input().split() ]
num_list = [ (int(v), 1) for v in input().split() ]
change_list = [ swap([ int(v) for v in input().split() ]) for i in range(m) ]
total_list = sorted(num_list + change_list, reverse = True)

k = n
ans = 0

for i in total_list:

    j = min(i[1], k)
    k -= j
    ans += i[0] * j
    if k == 0:
        break
print(ans)
### BUGGY CODE ###

a = [input(),input(),input()]
l = [len(a[0]),len(a[1]),len(a[2])]
ans = l[0]+l[1]+l[2]

def check(a0, l0, a1, l1, delta):
    # stt1 >= stt0, stt1 - stt0 >=l0 must return True
    i = delta
    j = 0
    while (i<l0) and (j<l1):
        if a0[i] != a1[j]:
            if a0[i] != '?' and a1[j] != '?':
                return False
        i += 1
        j += 1
    return True

memoLen = ans-min(l[0],l[1],l[2])+1
memo = [[[1]*memoLen for i in range(3)] for j in range(3)]
comb2 = [(0,1),(1,0),(1,2),(2,1),(0,2),(2,0)]
for x, y in comb2:
    me = memo[x][y]
    a0, a1 = a[x],a[y]
    l0, l1 = l[x],l[y]
    for delta in range(0,l0):
        if not check(a0,l0, a1,l1, delta):
            me[delta] = 0

comb = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,1,0),(2,0,1)]
for i,j,k in comb:
    l0, l1, l2 = l[i],l[j],l[k]
    for stt1 in range(0,l0+1):
        if memo[i][j][stt1]:
            l01 = max(l0,stt1+l1)
            for stt2 in range(stt1,l01+1):
                if memo[j][k][stt2-stt1] and memo[i][k][stt2]:
                    l012 = max(l01,stt2+l2)
                    ans = min(ans,l012)
print(ans)

### FIXED CODE ###

a = [input(),input(),input()]
l = [len(a[0]),len(a[1]),len(a[2])]
ans = l[0]+l[1]+l[2]

def check(a0, l0, a1, l1, delta):
    # stt1 >= stt0, stt1 - stt0 >=l0 must return True
    i = delta
    j = 0
    while (i<l0) and (j<l1):
        if a0[i] != a1[j]:
            if a0[i] != '?' and a1[j] != '?':
                return False
        i += 1
        j += 1
    return True

memoLen = ans-min(l[0],l[1],l[2])+1
memo = [[[1]*memoLen for i in range(3)] for j in range(3)]
comb2 = [(0,1),(1,0),(1,2),(2,1),(0,2),(2,0)]
for x, y in comb2:
    me = memo[x][y]
    a0, a1 = a[x],a[y]
    l0, l1 = l[x],l[y]
    for delta in range(0,l0):
        if not check(a0,l0, a1,l1, delta):
            me[delta] = 0

comb = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,1,0),(2,0,1)]
for i,j,k in comb:
    l0, l1, l2 = l[i],l[j],l[k]
    for stt1 in range(0,l0+1):
        if memo[i][j][stt1]:
            l01 = max(l0,stt1+l1)
            for stt2 in range(stt1,l01+1):
                if memo[j][k][stt2-stt1] and memo[i][k][stt2]:
                    l012 = max(l01,stt2+l2)
                    ans = min(ans,l012)
print(ans)
### BUGGY CODE ###

import math
n=int(input())

for p in range(1, n+1):
  right=math.floor(p*0.5)+1
  cnt=0
  for i in range(1, right):
    for j in range(1, i+1):
      for k in range(1, j+1):
        if i**2+j**2+k**2+i*j+j*k+k*i==p and i>0 and j>0 and k>0:
          if i==j and j==k:
            cnt+=1
          elif i==j and j!=k:
            cnt+=3
          elif j==k and k!=i:
            cnt+=3
          elif k==i and i!=j:
            cnt+=3
          elif i!=j and j!=k:
            cnt+=6
  print(cnt)

### FIXED CODE ###

n=int(input())
lst=[0]*10000000
for i in range(1, 101):
  for j in range(1, i+1):
    for k in range(1, j+1):
      temp=i**2+j**2+k**2+i*j+j*k+k*i
      if i==j and j==k:
        lst[temp+1]+=1
      elif i==j and j!=k:
         lst[temp+1]+=3
      elif j==k and k!=i:
         lst[temp+1]+=3
      elif k==i and i!=j:
         lst[temp+1]+=3
      elif i!=j and j!=k and k!=i:
         lst[temp+1]+=6
for p in range(1, n+1):
  print(lst[p+1])
### BUGGY CODE ###

from collections import defaultdict

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

A = defaultdict(list)
D = defaultdict(list)
R = [i for i in range(1,N+1)]
for i in range(N+M-1):
    a,b = map(int,input().split())
    A[a].append(b)
    D[b].append(a)
    try:
        R.remove(b)
    except ValueError:
        pass
    
D[R[0]].append(0)
Search = [(R[0],A[R[0]])]
Stepd = [R[0]]

while Search:
    now,NEX = Search.pop()
    for s in NEX:
        if len(D[s])>=2:
            D[s].remove(now)
        else:
            Stepd.append(s)
            Search.append((s,A[s]))
            
for i in range(1,N+1):
    print(D[i][0])

### FIXED CODE ###

from collections import defaultdict
from collections import Counter

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

A = defaultdict(list)
D = defaultdict(list)
DLen = defaultdict(int)
for i in range(N+M-1):
    a,b = map(int,input().split())
    A[a].append(b)
    #D[b].append(a)
    DLen[b] += 1

start = 0
for i in range(1,N+1):
    if DLen[i] == 0:
        start = i
        break
        
D[start].append(0)
Search = [(start,A[start])]
Stepd = [start]

while Search:
    now,NEX = Search.pop()
    for s in NEX:
        if DLen[s]>=2:
            DLen[s] -= 1
        else:
            D[s] = [now]
            Stepd.append(s)
            Search.append((s,A[s]))
            
for i in range(1,N+1):
    print(D[i][0])
### BUGGY CODE ###

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
r = [[0 for _ in range(W)] for _ in range(H)]
l = [[0 for _ in range(W)] for _ in range(H)]
d = [[0 for _ in range(W)] for _ in range(H)]
u = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    c = 0
    for j in range(W):
        if S[i][j] == ".":
            r[i][j] = c
            c += 1
        else:
            c = 0
for i in range(H):
    c = 0
    for j in range(W)[::-1]:
        if S[i][j] == ".":
            c += 1
            l[i][j] = c
        else:
            c = 0
for i in range(W):
    c = 0
    for j in range(H):
        if S[j][i] == ".":
            d[j][i] = c
            c += 1
        else:
            c = 0
for i in range(W):
    c = 0
    for j in range(H)[::-1]:
        if S[j][i] == ".":
            c += 1
            u[j][i] = c
        else:
            c = 0
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, r[i][j] + l[i][j] + d[i][j] + u[i][j] - 1)
print(ans)

### FIXED CODE ###

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
r = [[0 for _ in range(W)] for _ in range(H)]
l = [[0 for _ in range(W)] for _ in range(H)]
d = [[0 for _ in range(W)] for _ in range(H)]
u = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    c = 0
    for j in range(W):
        if S[i][j] == ".":
            r[i][j] = c
            c += 1
        else:
            c = 0
for i in range(H):
    c = 0
    for j in range(W)[::-1]:
        if S[i][j] == ".":
            c += 1
            l[i][j] = c
        else:
            c = 0
for i in range(W):
    c = 0
    for j in range(H):
        if S[j][i] == ".":
            d[j][i] = c
            c += 1
        else:
            c = 0
for i in range(W):
    c = 0
    for j in range(H)[::-1]:
        if S[j][i] == ".":
            c += 1
            u[j][i] = c
        else:
            c = 0
ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, r[i][j] + l[i][j] + d[i][j] + u[i][j] - 1)
print(ans)
### BUGGY CODE ###

import itertools

N,K = map(int, input().split())

X = []
Y = []

for _ in range(N):
  x,y = map(int, input().split())
  X.append(x)
  Y.append(y)

# 全部数えても間に合う

min_area = 5*10**18

for x1,x2 in itertools.combinations(X,2):
  for y1,y2 in itertools.combinations(Y,2):
    if x1 > x2:
      x1,x2 = x2,x1
    if y1 > y2:
      y1,y2 = y2,y1

    area = (x2-x1) * (y2-y1)
    if area > min_area:
      continue
      
    cnt = 0
    for x,y in zip(X,Y):
      if x1 <= x <= x2 and y1 <= y <= y2:
        cnt += 1
    if cnt < K:
      continue
      
    min_area = area
    
print(min_area)

### FIXED CODE ###

import itertools

N,K = map(int, input().split())

X = []
Y = []

for _ in range(N):
  x,y = map(int, input().split())
  X.append(x)
  Y.append(y)

# 全部数えても間に合う

min_area = 5*10**18

for x1,x2 in itertools.combinations(X,2):
  for y1,y2 in itertools.combinations(Y,2):
    if x1 > x2:
      x1,x2 = x2,x1
    if y1 > y2:
      y1,y2 = y2,y1

    area = (x2-x1) * (y2-y1)
    if area > min_area:
      continue
      
    cnt = 0
    for x,y in zip(X,Y):
      if x1 <= x <= x2 and y1 <= y <= y2:
        cnt += 1
    if cnt < K:
      continue
      
    min_area = area
    
print(min_area)
### BUGGY CODE ###

N, M = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
num = [0,2,5,5,4,5,6,3,7,6]

DP = [0] * (N+1)

for i in range(N+1):
    for j in range(M):
        n = num[A[j]]
        if i - n >= 0:
            DP[i] = max(DP[i], DP[i-n]+1)

A.sort(reverse = True)

ans = ""
remain = DP[N]
match = N
while match > 0:
    if remain == 1:
        for i in range(M):
            if num[A[i]] == match:
                ans += str(A[i])
                print(ans)
                exit()
    for i in range(M):
        n = num[A[i]]
        if match - n >= 0 and DP[match - n] == DP[match] - 1:
            ans += str(A[i])
            remain -= 1
            match -= n
            break

### FIXED CODE ###

import sys

N, M = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
A.sort()
A.reverse()

num = [0,2,5,5,4,5,6,3,7,6]

A_num = [(num[a], -a) for a in A]
A_num.sort()

min_A = -A_num[0][1]
min_num = A_num[0][0]
keta_max = N//min_num

DP = [-1] * (N+1)
DP[0] = 0

for i in range(N+1):
    for j in range(M):
        n = num[A[j]]
        if i - n >= 0 and DP[i-n] != -1:
            DP[i] = max(DP[i], DP[i-n] * 10 + A[j])

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

import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])

#e=np.cumsum(n)
#print(e)
#print(e[K:],e[:-K],e[K:]-e[:-K])


max=0
for i in range(N-K+1):
    tmp = np.sum(n[i:i+K]) 
    if tmp  > max:
        max=tmp 

print((max+K)*0.5)

### FIXED CODE ###

import numpy as np

N,K = [int(i) for i in input().split()]
n = np.array([int(i) for i in input().split()])
n=(n+1)
t=np.empty(N+1)
t[0] = 0
t[1:]=np.cumsum(n)
print(max(t[K:]-t[:-K])*0.5)
### BUGGY CODE ###



N,K = input().split()
AList = list(map(int, input().split()))
CSList = []

i=int(K)
j=0
while j<=int(N)-int(K):
    BList=AList[j:i]
    if j==0:
        MAE=BList[0]
    elif MAE>=BList[-1]:
        print("No")
        MAE=BList[0]
    elif MAE<BList[-1]:
        print("Yes")
        MAE=BList[0]
    i+=1
    j+=1










### FIXED CODE ###



N,K = map(int, input().split())
AList = list(map(int, input().split()))

A=str(K)
i=int(A)
j=0
while j<=N-K:
    #BList=AList[j:i]
    if j==0:
        MAE=AList[j]
    elif MAE>=AList[i-1]:
        print("No")
        MAE=AList[j]
    elif MAE<AList[i-1]:
        print("Yes")
        MAE=AList[j]
    i+=1
    j+=1
### BUGGY CODE ###

A = input()
N = int(input())
L = []
for i in range(len(A)):
    for j in range(i+1, len(A)+1):
        L.append(A[i:j])
L = list(set(L))
L.sort()
print(L[N-1])

### FIXED CODE ###

A = list(input())
K = int(input())
L = []
for i in range(len(A)):
    for j in range(i+1, i+K+1):
        L.append(''.join(A[i:j]))
L = list(set(L))
L.sort()
print(L[K-1])
### BUGGY CODE ###


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

ans = 1
flag = 1
flag2 = 0

for d in data:
  if d == 0:
    flag2 = 1

for d in data:
  ans = ans * d
  if ans > 1e18:
    flag = 0

if(flag2):
  print(0)
elif(flag):
  print(ans)
else:
  print(-1)

### FIXED CODE ###


n = map(int, input())
data = list(map(int, input().split()))
INF = 1e18
ans = 1

for d in data:
  ans = ans * d
  if ans > INF:
    ans = -1
    break

if 0 in data:
  ans = 0

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

a = input().split()
n = int(a[0])
m = int(a[1])
qq = int(a[2])

x = [[0] * n for _ in range(n)]

for _ in range(m):
    a = input().split()
    x[int(a[0]) - 1][int(a[1]) - 1] += 1

c = list()

for i in range(n):
    c.append(list())
    for j in range(n):
        a = x[i][j]

        if 0 < j:
            a += c[i][j - 1]

        c[i].append(a)

for _ in range(qq):
    r = 0
    a = input().split()
    p = int(a[0])
    q = int(a[1])
    for j in range(p - 1, q):
        r += c[j][q - 1]
        if 1 < p:
            r -= c[j][p - 2]

    print(r)


### FIXED CODE ###

a = input().split()
n = int(a[0])
m = int(a[1])
qq = int(a[2])

x = [[0] * n for _ in range(n)]

for _ in range(m):
    a = input().split()
    x[int(a[0]) - 1][int(a[1]) - 1] += 1

c = list()

for i in range(n):
    c.append(list())
    for j in range(n):
        a = x[i][j]

        if 0 < j:
            a += c[i][j - 1]

        c[i].append(a)

z = list()

for i in range(n):
    z.append(list())
    for j in range(n):
        a = c[i][j]

        if 0 < i:
            a += z[i - 1][j]

        z[i].append(a)

for _ in range(qq):
    r = 0
    a = input().split()
    p = int(a[0])
    q = int(a[1])

    if 0 < q:
        r += z[q - 1][q - 1]

    if 0 < q and 1 < p:
        r -= z[q - 1][p - 2] + z[p - 2][q - 1]

    if 1 < p:
        r += z[p - 2][p - 2]

    print(r)
### BUGGY CODE ###

n,k = map(int,input().split())
a=list(map(int,input().split()))
def makebit(a):
    ans=''
    while a>0:
        ans=str(a%2)+ans
        a=a//2
    return ans

def makexor(a,b):
    l=max(len(a),len(b))
    if len(a)<l:a=(l-len(a))*'0'+a
    if len(b)<l:b=(l-len(b))*'0'+b
    ans=''
    for i in range(l):
        if a[i]!=b[i]:ans+='1'
        elif a[i]==b[i]:ans+='0'
    return ans

def makenum(a):
    ans=0
    for i in range(len(a)):
        ans+=int(a[-i-1])*2**i
    return ans

for i in range(len(a)):
    a[i]=makebit(a[i])

def check(x,a):
    ans=0
    for i in a:
        ans+=makenum(makexor(x,i))
    return ans

ans=check('0',a)
for i in range(1,k+1):
    x=makebit(i)
    res=check(x,a)
    if res>ans:ans=res

print(ans)

### FIXED CODE ###

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

from collections import defaultdict
counter=defaultdict(int)
#桁ごとに１の数を数える辞書型

def makebit(a):
    return bin(a)[2:]
#数値型から２進数の文字列型を返す関数

def count(a):
    l=len(a)
    for i in range(1,l+1):
        if a[-i]=='1':
            counter[i]+=1
    return
#２進数の文字列型から、１の場所と数を数える関数

for i in range(len(a)):
    count(makebit(a[i]))
#inputしたデータをcounterに整理する

kbit=makebit(k)
#kの2進数表記
onetime=kbit.count('1')
#kに何回１が出現するか(後述)
if len(counter)>0:
    maxlenth=max(counter)
elif len(counter)==0:
    maxlenth=0
#Aiの最も大きい桁


#２の階乗のリスト

for i in range(1,1+max(maxlenth,len(kbit))):
    if counter[i]==0:
        counter[i]=0

if maxlenth>len(kbit):
    kbit='0'*(maxlenth-len(kbit))+kbit
elif maxlenth<len(kbit):
    maxlenth=len(kbit)
    
kaijo=[]
for i in range(maxlenth+1):
    kaijo.append(2**i)
#maxlenthまでで登録されていないkeyを登録

#メインとなるループ関数
def search(kbit,default,ans,before,counter):
    if kbit.count('1')==0:
        dlenth=len(kbit)
        for keta in counter:
            if keta>before:continue
            elif keta<=dlenth:
                default+=counter[keta]*kaijo[keta-1]
        if default>ans:ans=default
        return ans
    dlenth=len(kbit)-kbit.index('1')
    now=default
    newcounter=counter.copy()
    for keta in counter:
        if keta>before:continue
        elif keta>dlenth:
            default+=counter[keta]*kaijo[keta-1]
            now+=counter[keta]*kaijo[keta-1]
            newcounter.pop(keta)
        elif keta==dlenth:
            default+=(n-counter[keta])*kaijo[keta-1]
            now+=counter[keta]*kaijo[keta-1]
            newcounter.pop(keta)
        elif keta<dlenth:now+=max(n-counter[keta],counter[keta])*kaijo[keta-1]
    if kbit.index('1')==len(kbit)-1:
        now=default-(n-counter[1])
        now+=max(n-counter[1],counter[1])
        if now>ans:ans=now
        return ans
    if now>ans:ans=now
    idx=kbit.index('1')
    kbit=kbit[idx+1:]
    return search(kbit,default,ans,dlenth,newcounter)
print(search(kbit,0,0,maxlenth+1,counter))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    jobs = {}
    for _ in range(N):
        A, B = map(int, input().split())
        if A in jobs.keys():
            jobs[A] = jobs[A] + [B]
        else:
            jobs[A] = [B]

    profit = 0
    potential_jobs = []

    # M-1日後から今日まで遡る
    for elappsed_day in range(1, M+1):
        if elappsed_day in jobs.keys():
            potential_jobs += jobs.pop(elappsed_day)
        if len(potential_jobs) > 0:
            profit += potential_jobs.pop(potential_jobs.index(max(potential_jobs)))
            
    print(profit)

if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
from heapq import heappush, heappop
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    jobs = {}
    for _ in range(N):
        A, B = map(int, input().split())
        if A > M:
            continue
        if A in jobs.keys():
            jobs[A] += [-B]
        else:
            jobs[A] = [-B]

    profit = 0
    days = jobs.keys()
    potential_jobs = []
    
    # M-1日後から今日まで遡る
    for elappsed_day in range(1, M+1):
        if elappsed_day in days:
            for job in jobs[elappsed_day]:
                heappush(potential_jobs, job)
        if len(potential_jobs) > 0:
            profit += heappop(potential_jobs)
            
    print(-profit)

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

import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)


### FIXED CODE ###

import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
# AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)