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

import os, sys, re, math

S = input()

count = 0
while True:
    s = S.replace('01', '').replace('10', '')
    if s == S:
        break
    count += len(S) - len(s)
    S = s

print (count)    


### FIXED CODE ###

import os, sys, re, math

S = input()

s1 = S.replace('1', '')
s2 = S.replace('0', '')

print (min(len(s1), len(s2)) * 2)
### BUGGY CODE ###

s = input()
s = list(s)
q = int(input())
f = 1
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    f *= -1
  else:
    if f == 1:
      if query[1] == '1':
        s.insert(0, query[2])
      else:
        s.append(query[2])
    else:
      if query[1] == '1':
        s.append(query[2])
      else:
        s.insert(0, query[2])
if f == -1:
  s = s[::-1]
ans = ''.join(s)
print(ans)

### FIXED CODE ###

s = input()
q = int(input())
flag = 1
f = ''
b = ''
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    flag *= -1
  else:
    if flag == 1:
      if query[1] == '1':
        f += query[2]
      else:
        b += query[2]
    else:
      if query[1] == '1':
        b += query[2]
      else:
        f += query[2]
ans = f[::-1] + s + b
if flag == -1:
  ans = ans[::-1]
print(ans)
### BUGGY CODE ###

h,w,m=map(int,input().split())
bomb=[list(map(int,input().split())) for i in range(m)]

num_h = [0] * (h+1)
num_w = [0] * (w+1)
bomb_set = set()
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  bomb_set.add((hi,wi))
  
max_hs = [k for k,v in enumerate(num_h) if v == max(num_h)]
max_ws = [k for k,v in enumerate(num_w) if v == max(num_w)]

result = max(num_h) + max(num_w)
for i in max_hs:
  for j in max_ws:
    if (i,j) not in bomb_set:
      print(result)
      exit(0)
print(result-1)


### FIXED CODE ###

h,w,m=map(int,input().split())
bomb=[list(map(int,input().split())) for i in range(m)]

num_h = [0] * (h+1)
num_w = [0] * (w+1)
bomb_set = set()
for hi,wi in bomb:
  num_h[hi] += 1
  num_w[wi] += 1
  bomb_set.add((hi,wi))
  
max_h = max(num_h)
max_w = max(num_w)
max_hs = [k for k,v in enumerate(num_h) if v == max_h]
max_ws = [k for k,v in enumerate(num_w) if v == max_w]

result = max_h + max_w
for i in max_hs:
  for j in max_ws:
    if (i,j) not in bomb_set:
      print(result)
      exit(0)
print(result-1)
### BUGGY CODE ###

from math import ceil
from fractions import Fraction
from sys import exit
import heapq

n,k, *aa = map(Fraction, open(0).read().split())

left = k/2
right = k*2 + 1
mid = k/2
summ = sum(aa)
score = None
while 1:
    x = [(ceil(mid*a/summ), a) for a in aa]
    prev = score
    score = sum(a[0]-1 for a in x)
    if prev == score:
        break
    if score > k:
        right = mid
        mid = (left + right) / 2
    elif score < k:
        left = mid
        mid = (left + right) / 2
    else:
        values = [ceil(a/v) for v,a in x]
        print(max(values))
        exit()

x = [(-a/c, c, a) for c,a in x]
heapq.heapify(x)

for _ in range(int(k-score)):
    v,c,a = heapq.heappop(x)
    c += 1
    v = -a/c
    heapq.heappush(x, (v,c,a))

print(ceil(-x[0][0]))

### FIXED CODE ###

from math import ceil

n,k, *aa = map(int, open(0).read().split())

left = 0
right = 10**9

while right - left > 1:
    mid = (left + right) // 2
    summ = sum(0--a//mid-1 for a in aa)
    if summ <= k:
        right = mid
    else:
        left = mid

print(right)
### BUGGY CODE ###

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
a=[]
s=set()
for i in range(N):
    idx=P[i]-1
    z=set()
    tl=[]
    c=0
    tc=0
    while idx not in z and c<K:
        tl.append(C[idx])
        tc+=C[idx]
        s.add(tc)
        z.add(idx)
        idx=P[idx]-1
        c+=1
    a.append(tl)
for l in a:
    le=len(l)
    d,m=divmod(K,le)
    tc=d*sum(l)
    s.add(tc)
    for i in range(m):
        tc+=l[i]
        s.add(tc)
print(max(s))

### FIXED CODE ###

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
a=[]
ans=-10**10
for i in range(N):
    idx=P[i]-1
    z=set()
    rl=[]
    c=0
    tc=0
    while idx not in z and c<K:
        tc+=C[idx]
        rl.append(tc)
        ans=max(ans,tc)
        z.add(idx)
        idx=P[idx]-1
        c+=1
    if c<K:
        d,m=divmod(K,c)
        if m==0:
            d-=1
            m=c
        tc=d*tc
        tc+=max(rl[:m])
        ans=max(ans,tc)
print(ans)
### BUGGY CODE ###

s = input()
n = int(input())
query = []

for i in range(n):
    query.append(list(input().split()))

# print(query)

for i in range(n):
    # Reverse
    if query[i][0] == '1':
        s = s[::-1]
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            s = query[i][2] + s

        # To Tail
        else:
            s += query[i][2]

print(s)


### FIXED CODE ###

from collections import deque

s = input()
t = s[::-1]
ss = deque(s)
tt = deque(t)
isS = True
n = int(input())
query = []

# print(q1, q2)

for i in range(n):
    query.append(list(input().split()))

# print(query)

for i in range(n):
    # Reverse
    if query[i][0] == '1':
        # s = s[::-1]
        isS = not isS
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            if isS:
                ss.appendleft(query[i][2])
                tt.append(query[i][2])
            else:
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
        # To Tail
        else:
            if isS:
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
            else:
                ss.appendleft(query[i][2])
                tt.append(query[i][2])

if isS:
    temp = list(ss)
    ans = ''.join(temp)
    print(ans)
else:
    temp = list(tt)
    ans = ''.join(temp)
    print(ans)
### BUGGY CODE ###

import collections
import copy
n = int(input())
aa = list(map(int, input().split()))

def comb(n, k):
    p = (10 ** 9) + 7
    """power_funcを用いて(nCk) mod p を求める"""
    from math import factorial
    if n<0 or k<0 or n<k: return 0
    if n==0 or k==0: return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
    """a^b mod p を求める"""
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a * power_func(a, b - 1, p)) % p   

memo = {}
for i in range(len(aa)):
    if not aa[i] in memo:
        hu = copy.deepcopy(aa)
        del hu[i]
        a = collections.Counter(hu)
        summ = 0
        for j in a:
            summ += comb(a[j], 2)
        memo[aa[i]] = summ
    print(memo[aa[i]])


### FIXED CODE ###

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

sub = collections.Counter(a)
ans = 0
for i in sub:
    ans += sub[i] * (sub[i] - 1) // 2
for i in range(n):
    ad = sub[a[i]] * (sub[a[i]] - 1) // 2 - ((sub[a[i]] - 1) * (sub[a[i]] - 2) // 2)
    print(ans - ad)
### BUGGY CODE ###

R, G, B, N = [int(n) for n in input().split()]

count = 0
for r in range(N//R + 1):
    for g in range(N//G + 1):
        if (N - (R * r + G * g)) % B == 0:
            b = (N - (R * r + G * g)) / B
            if  0 <= b <=  N//B:
                count += 1

print(count)

### FIXED CODE ###

R, G, B, N = [int(n) for n in input().split()]

count = 0
for r in range(N//R + 1):
    for g in range((N-r*R)//G + 1):
        b_ = N - (R * r + G * g) 
        if b_ % B == 0:
                count += 1

print(count)
### BUGGY CODE ###

n=int(input())
count=0
li=[]
for i in range(n):
    a=int(input())
    if a in li:
        li.remove(a)
        count-=1
    else:
        li.append(a)
        count+=1
print(count)

### FIXED CODE ###

n=int(input())
count=0
li=[0 for i in range(n)]
for i in range(n):
    a=int(input())
    li[i]=a
li.sort()
i=0
x=1
while i<n-1:
    if li[i]==li[i+1]:
        x+=1
        if i+2==n-1:
            count+=1
    else:
        x=1
        if i+1==n-1:
            count+=1
    if x%2==1:
        count+=1
    i+=x
    x=1
print(count)
### BUGGY CODE ###

import collections

n = int(input())
a = [int(e) for e in input().split()]
cnt = collections.Counter(a)

for i in a:
  temp = [v if k != i else v - 1 for k, v in cnt.items()]
  print(sum([ c * (c-1) for c in temp])//2)
 

### FIXED CODE ###

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

import collections

cnt = collections.Counter(s)
ans = sum([(v * (v-1)) // 2 for k, v in cnt.items()])

for i in range(len(s)):
  print(ans - cnt[s[i]] + 1)
### BUGGY CODE ###

from operator import mul
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
 
if 0 in a:
	print(0)
else:
	M = 10**18
	ans = reduce(mul, a)
	if ans > M:
		print(-1)
	else:
		print(ans)

### FIXED CODE ###

n = int(input())
s = sorted(map(int,input().split()))
M = 10 ** 18
ans = 1
for ss in s:
	ans *= ss
	if ans > M:
		ans = -1
		break
	elif ans == 0:
		break
print(ans)
### BUGGY CODE ###

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

max_ans = M // N
for a in reversed(range(1, max_ans + 1)):
    if M % a != 0:
        continue
    else:
        ans = a
        # print(a, M // a)
        break

else:
    print(1)
    exit()

print(ans)

### FIXED CODE ###

from itertools import product

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

max_ans = M // N

ans = 1
tmp = 2
tmpM = M
max_prime = int(M ** 0.5)
all_list = []
i = -1
k = 0
div = 2
while tmpM != 1:
    if tmpM % div == 0:
        tmpM = tmpM // div
        if k == 0:
            i += 1
            all_list.append(set())
            all_list[i].add(div ** k)
            k += 1
        all_list[i].add(div ** k)
        k += 1

    else:
        k = 0
        if div == 2:
            div = 3
        elif div > max_prime:
            div = tmpM
        else:
            div += 2

if all_list == []:
    print(1)
    exit()

# print(all_list)
ans = 1

for div_tuple in product(*all_list):
    tmp = 1
    for k in div_tuple:
        tmp *= k
        if tmp > max_ans:
            break
    else:
        ans = max(ans, tmp)

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

def s():
  import queue
  k = int(input())
  q = queue.Queue([1,2,3,4,5,6,7,8,9])
  qg = q.get
  qp = q.put
  for _ in range(k-1):
    x = qg()
    if x%10 == 0:
      qp(10*x)
      qp(10*x+1)
    elif x%10 == 0:
      qp(10*x+8)
      qp(10*x+9)
    else:
      i = x%10
      qp(10*x+i-1)
      qp(10*x+i)
      qp(10*x+i+1)
  x = qg()
  print(x)
s()
    
    
    


### FIXED CODE ###

k = int(input())
x = [1,2,3,4,5,6,7,8,9]
i = 0
xa = x.append
while len(x) < k:
  if x[i]%10 == 0:
    xa(10*x[i])
    xa(10*x[i]+1)
  elif x[i]%10 == 9:
    xa(10*x[i]+8)
    xa(10*x[i]+9)
  else:
    j = x[i]%10
    xa(10*x[i]+j-1)
    xa(10*x[i]+j)
    xa(10*x[i]+j+1)
  i += 1
print(x[k-1])
### BUGGY CODE ###

n = int( input() )

judge_nums = []
for i in range( n ):
	judge_num = 10**27 # 一桁目はフェイク
	str_list = list( input() )
	for s in str_list :
		judge_num += 10**(ord( s )-97)
	judge_nums.append( judge_num )
judge_nums.sort()


count = 0
for i in range( n ) :
	for j in range( i+1, n ) :
		if judge_nums[i] != judge_nums[j] :
			break
		if judge_nums[i] == judge_nums[j] :
			count += 1

print( count )



### FIXED CODE ###

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

#=-------------------------------------  
n = int( input() )

dict = {}
for i in range( n ):
	sorted_input = "".join( sorted( list( input() ) ) )
	if dict.get( sorted_input ) == None:
		dict[ sorted_input ] = 1
	else :
		dict[ sorted_input ] += 1

count = 0
for eachValue in dict.values():
	if eachValue == 1 : continue
	count += cmb( eachValue, 2 )
print( count )
### BUGGY CODE ###

import numpy as np
n, k, q = map(int, input().split())
correct = []
orig = np.ones(n) * k
for i in range(q):
    tmp = int(input())
    orig -= 1
    orig[tmp-1] += 1
for i in orig:
    if i <=0:
        print("No")
    else:
        print("Yes")

### FIXED CODE ###

import numpy as np
n, k, q = map(int, input().split())
correct = []
orig = np.ones(n) * k - q
for i in range(q):
    tmp = int(input())
    orig[tmp-1] += 1
for i in orig:
    if i <=0:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

n = int(input())
ab = []
graph = {(i + 1):[] for i in range(n)}
for i in range(n - 1):
    a, b = map(int, input().split())
    ab.append([a, b])
    graph[a].append(b)
    graph[b].append(a)
color = max([len(list(graph.values())[i]) for i in range(n)])
print(color)
branch = {}
for i in graph:
    if len(graph[i]) == color:
        start = i
        break
task = [[start, 0]]
while task:
    next_check = task.pop(0)
    check = next_check[0]
    flag = False
    temp = []
    for i in range(len(graph[check])):
        if i + 1 == next_check[1]:
            flag = True
        branch[(check, graph[check][i])] = i + 1 + flag
        task.append([graph[check][i], i + 1 + flag])
        temp.append([check, graph[check][i]])
    for i in temp:
        graph[i[0]].remove(i[1])
        graph[i[1]].remove(i[0])
for i in ab:
    if (i[0], i[1]) in branch:
        print(branch[(i[0], i[1])])
    else:
        print(branch[(i[1], i[0])])

### FIXED CODE ###

import collections

n = int(input())
graph = [[] for i in range(n + 1)]
edge = []
for i in range(n - 1):
    a, b = map(int, input().split())
    edge.append((a, b))
    graph[a].append(b)
    graph[b].append(a)
color = {}
queue = collections.deque([(1, 0)])
while queue:
    temp = queue.popleft()
    num = temp[0]
    prev = temp[1]
    if prev:
        col = color[(prev, num)]
    else:
        col = 0
    count = 1
    for i in graph[num]:
        a, b = min(i, num), max(i, num)
        if (a, b) in color.keys():
            continue
        else:
            if count == col:
                color[(a, b)] = count + 1
                count += 2
            else:
                color[(a, b)] = count
                count += 1
            queue.append((i, num))
print(max([len(graph[i]) for i in range(n + 1)]))
for i, j in edge:
    print(color[(i, j)])
### BUGGY CODE ###

n,k=map(int,input().split())
x=0
for b in range(k+1,n+1):
  for i in range(k,b):
    x+=(n-i)//b+(i>0)
print(x)

### FIXED CODE ###

n,k=map(int,input().split())
x=0
for b in range(k+1,n+1):
  r=n%b
  if r==0:
    x+=(n//b)*(b-k)
  else:  
    x+=(n//b+1)*max(0,(r-k+1))
    x+=(n//b)*max(0,(b-max(k,r+1)))
    if k==0:
      x-=1
print(x)
### BUGGY CODE ###

n = int(input())
count = [0]*n
l = []
for i in range(n):
    a = ''.join(sorted(input()))
    if a in l:
        count[l.index(a)] += 1
    else:
        l.append(a)
print(sum(int((i+1)*i/2) for i in count if i != 0))

### FIXED CODE ###

from collections import Counter
n = int(input())
a = [""]*n
for i in range(n):
    a[i] = ''.join(sorted(input()))
b = list(Counter(a).values())
print(sum(int((i-1)*i/2) for i in b))
### BUGGY CODE ###

d = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(d)]
t = [int(input()) for i in range(d)]
now = [0]*26

score = [[0] for i in range(26)]
for i in range(d):
    
    now[t[i]-1] = i+1
    ind = t[i]-1
    for j in range(26):
        if j == ind:
            score[j].append(s[i][ind]+score[j][-1])
        else:
            x = (i+1-now[j])*c[j]
            score[j].append(-x+score[j][-1])
m = int(input())
l = [list(map(int,input().split())) for i in range(m)]

for i in range(m):
    x,y = l[i]
    be = t[x-1]
    be -= 1
    now = 0
    t[x-1] = y
    y -= 1
    count = [0]
    for j in range(d):
        if be == t[j]-1:
            now = j+1
            count.append(s[j][be]+count[-1])
        else:
            count.append(-(j+1-now)*c[be]+count[-1])
    score[be] = count
    count = [0]
    now = 0
    for j in range(d):
        if y == t[j]-1:
            now = j+1
            count.append(s[j][y]+count[-1])
        else:
            count.append(-(j+1-now)*c[y]+count[-1])
    score[y] = count

    ans = 0
    for i in range(26):
        ans += score[i][-1]
    print(ans)
        


    


### FIXED CODE ###

d = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(d)]
t = [int(input()) for i in range(d)]
now = [0]*26

score = [0]*26
for i in range(d):
    
    now[t[i]-1] = i+1
    ind = t[i]-1
    for j in range(26):
        if j == ind:
            score[j] += s[i][ind]
        else:
            x = (i+1-now[j])*c[j]
            score[j]-= x
m = int(input())
l = [list(map(int,input().split())) for i in range(m)]
ans = sum(score)
for i in range(m):
    x,y = l[i]
    be = t[x-1]
    be -= 1
    now1 =  0
    now2 = 0
    t[x-1] = y
    y -= 1
    count1 = 0
    count2 = 0
    for j in range(d):
        if be == t[j]-1:
            now1 = j+1
            count1 +=s[j][be]
        else:
            count1 -= (j+1-now1)*c[be]
        if y == t[j]-1:
            now2 = j+1
            count2 +=s[j][y]
        else:
            count2 -= (j+1-now2)*c[y]

    ans += count1+count2-score[be]-score[y]
    print(ans)
    score[be] = count1
    score[y] = count2
### BUGGY CODE ###

n=int(input())
arr=input().split(" ")
arr_s=set(arr)
#print(arr)
a=0
most_e=0
most_o=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
#print(arr_e,arr_o)
arr_es=set(arr_e)
arr_os=set(arr_o)
#print(arr_es,arr_os)

for i in arr_es:
    cnt_e=arr_e.count(i)
    if cnt_e>most_e:
        most_e=cnt_e
        if most_e>=(n/2-most_e)/2:
            break
for i in arr_os:    
    cnt_o=arr_o.count(i)
    if cnt_o>most_o:
        most_o=cnt_o
        if most_o>=(n/2-most_o)/2:
            break
#print(most_e,most_o)
if len(arr_s)==1:
    print(int(n/2))
else:
    print(n-most_e-most_o)

### FIXED CODE ###

import collections
n=int(input())
arr=input().split(" ")
arr_s=set(arr)
a=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
ce=collections.Counter(arr_e).most_common()
co=collections.Counter(arr_o).most_common()
if len(arr_s)==1:
    print(int(n/2))
else:
    if ce[0][0]!=co[0][0]:
        print(n-ce[0][1]-co[0][1])
    else:
        print(n-max(ce[0][1]+co[1][1],ce[1][1]+co[0][1]))
### BUGGY CODE ###

from collections import deque

N=int(input())
A=[[-1 for i in range(N+1)] for j in range(N+1)]
B=[[] for i in range(N+1)]
for i in range(N-1):
  u,v,w=map(int,input().split())
  A[u][v]=A[v][u]=w
  B[u].append(v)
  B[v].append(u)
#print(A)
#print(B)

check=[-1]*(N+1)
check[1]=0
D = deque()
D.append(1)

while D:
  d=D.popleft()
  for b in B[d]:
    if check[b]==-1:
      D.append(b)
      check[b]=(check[d]+A[d][b])%2

for i in range(1,len(check)):
  print(check[i])

### FIXED CODE ###

from collections import deque

N = int(input())
A = [[] for i in range(N + 1)]
for i in range(N - 1):
  u, v, w=map(int, input().split())
  A[u].append([v, w])
  A[v].append([u, w])
#print(A)

check = [-1] * (N + 1)
check[1] = 0
D = deque()
D.append(1)

while D:
  d=D.popleft()
  for a in A[d]:
    if check[a[0]]==-1:
      D.append(a[0])
      check[a[0]]=(check[d]+a[1])%2

for i in range(1,len(check)):
  print(check[i])
### BUGGY CODE ###

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

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

a_count_list = []

a_dic = {}

for i in range(len(a_list)):
    key = a_list[i]
    if key in a_dic:
        a_dic[key] += 1
    else:
        a_dic[key] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    sum_select += a_solution_dic_at_k[ak]
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)


### FIXED CODE ###

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

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
all_sum_select = 0
for key in list(a_dic.keys()):
    all_sum_select += int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)

for k, ak in enumerate(a_list):
    sum_select_at_k = all_sum_select + int((a_dic[ak]-1) * (a_dic[ak]-2) / 2) \
                       - int(a_dic[ak] * (a_dic[ak]-1) / 2)


    print(sum_select_at_k)
### BUGGY CODE ###

comp = 0
def merge(A, left, right):
    global comp
    if not left+1 < right:
        return [A[left]]
    if left+2 == right:
        comp += 2
        a, b = A[left:right]
        return (a, b) if a < b else (b, a)
    mid = (left + right) // 2
    L = merge(A, left, mid)
    R = merge(A, mid, right)
    result = []
    i = j = 0
    while i < len(L) and j < len(R):
        comp += 1
        if L[i] <= R[j]:
            result.append(L[i])
            i += 1
        else:
            result.append(R[j])
            j += 1
    if i < len(L):
        result.extend(L[i:])
        comp += len(L) - i
    if j < len(R):
        result.extend(R[j:])
        comp += len(R) - j
    return result

n, *A = map(int, open(0).read().split())
B = merge(A, 0, n)
print(*B)
print(comp)

### FIXED CODE ###

comp = 0
def m(L, R):
    global comp
    j = 0
    for l in L:
        while j < len(R) and R[j] < l:
            yield R[j]
            j += 1
        yield l
    while j < len(R):
        yield R[j]
        j += 1
    comp += len(L) + len(R)
def merge(A):
    global comp
    if len(A) == 1:
        return A
    if len(A) == 2:
        comp += 2
        a, b = A
        return A if a < b else (b, a)
    mid = len(A) // 2
    A[:] = m(merge(A[:mid]), merge(A[mid:]))
    return A

n, *A = map(int, open(0).read().split())
B = merge(A)
print(*B)
print(comp)
### BUGGY CODE ###

n,m = map(int,input().split())
ba = []
m1 = m + 2
check = set(i for i in range(m + 1))
for _ in range(n):
    a,b = map(int,input().split())
    ba.append([b,a])
ba.sort(reverse=True)
cnt = 0
for BA in ba:
    a1 = BA[1]#a
    b1 = BA[0]#b
    if a1 >= m1:
        continue
    mb = m1
    m1 = a1
    for i in range(m - a1 + 1):
        if a1 + i in check:
            check.remove(a1 + i)
            cnt += b1
            m1 = mb
            break
print(cnt)

### FIXED CODE ###

from heapq import *
# from itertools import permutations
# from math import pi
# from operator import itemgetter
# from operator import mul
# from operator import xor
# from os import linesep


# def isprime(n: int) -> bool:
#     if n <= 1:
#         return False
#     if n in (2, 3, 5):
#         return True
#     if n % 2 == 0:
#         return False
#     if n % 3 == 0:
#         return False
#     lst = int(n**0.5)
#     f = 5
#     while f <= lst:
#         if n % f == 0:
#             return False
#         if n % (f + 2) == 0:
#             return False
#         f += 6
#     return True


# def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str:
#     return lnsep.join(map(lambda row: fieldsep.join(map(str, row)), matrix))


# def strbool(boolval: int) -> str:
#     return ['No', 'Yes'][boolval]


def solve() -> None:
    # read here
    n, m, *t = map(int, open(0).read().split())

    # solve here
    d = [[] for i in range(m+1)]
    for i in range(n):
        a, b = t[2*i], t[2*i+1]
        if a > m:
            continue
        d[a].append(b)

    ans = 0
    tasks = []
    for t in range(m + 1):
        # from (m days after) to today
        for tasksize in d[t]:
            heappush(tasks, -tasksize)
        if tasks:
            # do task
            ans -= heappop(tasks)

    # print here
    print(ans)


if __name__ == '__main__':
    solve()