CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
P = list(map(int, input().split()))

ans = 1
index = P[0]

for i in range(1, N):
    if index > P[i]:
        index = P[i]
        ans += 1


print(ans)


# Optimized solution
#!/usr/bin/env python3

N = int(eval(input()))
P = list(map(int, input().split()))
M = [0] * N
M[0] = P[0]
m = P[0]
for i in range(1, N):
    m = min(P[i], m)
    M[i] = m


ans = 0
for i in range(N):
    if P[i] <= M[i]:ans+=1

print(ans)
# Original solution
n=int(eval(input()))
l=list(map(int,input().split()))

cnt=0
for i in range(1,n+1):
  if min(l[:i])==l[i-1]:
    cnt+=1
  
    
print(cnt)

# Optimized solution
n=int(eval(input()))
l=list(map(int,input().split()))

cnt=0
num=max(l)

for i in l:
  num=min(num,i)
  if num==i:
    cnt+=1
    
print(cnt)
# Original solution
import sys

s = sys.stdin.readline().rstrip()

def main():
    cnt = 0
    for i in range(len(s) - 1):
        if s[i] != s[i+1]:
            cnt += 1
    return cnt

if __name__ == '__main__':
    ans = main()
    print(ans)

# Optimized solution
import sys

s = sys.stdin.readline().rstrip()

def main():
    prev = '$'
    cnt = -1
    for c in s:
        cnt += c != prev
        prev = c
    print(cnt)

if __name__ ==  '__main__':
    main()
# Original solution
def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def all_same(arr):
    for i in range(len(arr)-1):
        if arr[i] != arr[i+1]:
            return False
    return True

def opposite(x):
    return (x - 1) ** 2

def update(arr):
    count = 0
    while(not all_same(arr)):
        append = opposite(arr[-1])
        other = arr[-1]
        i = -1
        while(arr[i] == other or -i == len(arr) + 1):
            arr[i] = append
            i -= 1
        if -i == len(arr)/2:
            arr.reverse()
        count += 1
    return count

if __name__ == '__main__':
    s = input()
    arr = line(s)
    print((update(arr)))


# Optimized solution
def line(s):
    ret = [0] * len(s)
    for i in range(len(s)):
        ret[i] = 0 if s[i] == 'W' else 1

    return ret

def count(arr):
    c = 0
    for i in range(len(arr)-1):
        if arr[i+1] != arr[i]:
            c += 1
    return c

if __name__ == '__main__':
    s = input()
    arr = line(s)
    print((count(arr)))
# Original solution
def isSame(string):
    for i in range(1, len(string)):
        if string[i] != string[0]:
            return False
    return True

s = input()
a = []
l = len(s)
for i in range(l):
    a.append(s[i])

n = 0

while not isSame(a):
    if a[0] == 'B':
        for i in range(l):
            if a[i] == 'B':
                a[i] = 'W'
            else:
                break
        a.insert(0, 'W')
    elif a[0] == 'W':
        for i in range(l):
            if a[i] == 'W':
                a[i] = 'B'
            else:
                break
        a.insert(0, 'B')
    n += 1
    l += 1

print(n)


# Optimized solution
s = input()
temp = s[0]
n = 0
for i in range(len(s)):
    if s[i] != temp:
        n += 1
        temp = s[i]
print(n)
# Original solution
import sys
input = sys.stdin.readline
S = list(input().rstrip())
N = len(S)
cnt = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
print(cnt)
    

# Optimized solution
import sys
input = sys.stdin.readline

s = input().rstrip()
cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        cnt += 1
print(cnt)
# Original solution
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    S = itertools.groupby(list(iss()))
    q = ''
    ret = 0
    for s in S:
        if q == '':
            q = s
        if q != s:
            ret += 1
            q = s
    print(ret)


if __name__ == '__main__':
    main()


# Optimized solution
import sys
import os
import math
import bisect
import collections
import itertools
import heapq

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    S = list(iss())
    q = S[0]
    ret = 0
    for s in S:
        if q != s:
            ret += 1
            q = s
    print(ret)


if __name__ == '__main__':
    main()
# Original solution
S = input().strip()
cnt = 0
cur = S[0]
cnt += 1
for i in range(1,len(S)):
    if S[i]==cur:continue
    else:
        cnt += 1
        cur = S[i]
print((cnt-1))

# Optimized solution
S = input().strip()
cur = S[0]
cnt = 0
for i in range(1,len(S)):
    if S[i]==cur:continue
    else:
        cnt += 1
        cur = S[i]
print(cnt)
# Original solution
S = input().strip()
N = len(S)
cur = 0
cnt = 0
for i in range(1,N):
    if S[i]!=S[cur]:
        cnt += 1
        cur = i
print(cnt)

# Optimized solution
S = input().strip()
cnt = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        cnt += 1
print(cnt)
# Original solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))


def merge(s1, s2):
    ls1 = len(s1)
    ls2 = len(s2)
    i1 = 0
    i2 = 0
    ans = []
    while (i1 < ls1 and i2 < ls2):
        if s1[i1] < s2[i2]:
            ans.append(s1[i1])
            i1 += 1
        else:
            ans.append(s2[i2])
            i2 += 1
    for i in range(i1, ls1):
        ans.append(s1[i])
    for i in range(i2, ls2):
        ans.append(s2[i])
    return ans


import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        bisect.insort_left(ABs[A], B)

As = 0
for i in range(M + 1):
    if i > 0:
        ABs[0] = merge(ABs[0], ABs[i])
    if len(ABs[0]) > 0:
        As += ABs[0].pop()
print(As)

# Optimized solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(B)
ABs[0].sort()

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            bisect.insort_left(ABs[0], b)
    if len(ABs[0]) > 0:
        As += ABs[0].pop(-1)
print(As)
# Original solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(B)
ABs[0].sort()

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            bisect.insort_left(ABs[0], b)
    if len(ABs[0]) > 0:
        As += ABs[0].pop(-1)
print(As)

# Optimized solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(B)
ABs[0].sort()

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            bisect.insort_left(ABs[0], b)
    if len(ABs[0]) > 0:
        As += ABs[0].pop()
print(As)
# Original solution
def resolve():
    import sys

    input = sys.stdin.readline
    N, M = list(map(int, input().split()))

    import bisect

    ABs = [[] for _ in range(M + 1)]
    for i in range(N):
        A, B = list(map(int, input().split()))
        if A <= M:
            ABs[A].append(B)
    ABs[0].sort()

    As = 0
    for i in range(M + 1):
        if i > 0:
            for b in ABs[i]:
                bisect.insort_left(ABs[0], b)
        if len(ABs[0]) > 0:
            As += ABs[0].pop()
    print(As)

if __name__ == '__main__':
    resolve()

# Optimized solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import heapq

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(-B)
heapq.heapify(ABs[0])

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            heapq.heappush(ABs[0], b)
    if len(ABs[0]) > 0:
        As += heapq.heappop(ABs[0])
print((-As))
# Original solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import heapq

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(-B)
heapq.heapify(ABs[0])

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            heapq.heappush(ABs[0], b)
    if len(ABs[0]) > 0:
        As += heapq.heappop(ABs[0])
print((-As))

# Optimized solution
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))

import heapq

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        ABs[A].append(-B)
heapq.heapify(ABs[0])

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            heapq.heappush(ABs[0], b)
    if len(ABs[0]) > 0:
        As -= heapq.heappop(ABs[0])
print(As)
# Original solution
from heapq import *

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        heappush(c[m - a], -b)
r=0
for i in range(m - 1, -1, -1):
    if c[i]:
        r -= heappop(c[i])
        if i:
            for x in c[i]:
                heappush(c[i - 1], x)  
print(r)


# Optimized solution
from heapq import *

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        heappush(c[m - a], -b)
r=0
for i in range(m - 1, -1, -1):
    if c[i]:
        r -= heappop(c[i])
        if i:
            f = len(c[i]) > len(c[i - 1])
            h, g = (c[i], c[i - 1]) if f else (c[i - 1], c[i])
            for x in g:
                heappush(h, x)
            if f:
                c[i - 1] = c[i]       
print(r)
# Original solution
from heapq import *

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        heappush(c[m - a], -b)
r=0
for i in range(m - 1, -1, -1):
    if c[i]:
        r -= heappop(c[i])
        if i:
            f = len(c[i]) > len(c[i - 1])
            h, g = (c[i], c[i - 1]) if f else (c[i - 1], c[i])
            for x in g:
                heappush(h, x)
            if f:
                c[i - 1] = c[i]       
print(r)


# Optimized solution
from heapq import heappush, heappop

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        c[m - a].append(b)
r=0
h = []
for l in reversed(c):
    for x in l:
        heappush(h, -x)
    if h:
        r -= heappop(h) 
print(r)
# Original solution
from heapq import heappush, heappop
from copy import deepcopy

def main():
    n, m = list(map(int,input().split()))
    
    h = []
    for i in range(n):
        day, value = list(map(int,input().split()))
        heappush(h, (-1*value, day))
    
    res = 0
    done = []
    for i in range(1, m+1):
        cur_h = deepcopy(h)
        while cur_h:
            r = heappop(cur_h)
            if r[1] <= i and not r in done:
                res += r[0]*-1
                done.append(r)
                break

    print(res)

if __name__ == "__main__":
    main()
        



# Optimized solution
from heapq import heappush, heappop
 
def main():
    n, m = list(map(int,input().split()))
    
    jobs = []
    for i in range(n):
        day, value = list(map(int,input().split()))
        jobs.append((day, value))

    jobs.sort()
    
    cur, res = 0, 0
    done = []
    h = []
    for i in range(1, m+1):
        while cur < n and jobs[cur][0] <= i:
            heappush(h, -jobs[cur][1])
            cur += 1
        if not h:
            continue
        res -= heappop(h)
 
    print(res)
 
if __name__ == "__main__":
    main()
# Original solution
n,m=list(map(int,input().split()))
A=[]
for i in range(n):
    a,b=list(map(int,input().split()))
    A.append([a,b])
A=sorted(A, key=lambda x:(x[0], -x[1]))
from collections import deque
A=deque(A)

# import numpy as np
ans=0
now=[]
for nouki in range(1,m+1):
    while A:
        a,b=A.popleft()
        if a>nouki: A.appendleft([a,b]);break
        now.append(b)
    #print(nouki, now)
    if now==[]:continue
    m=max(now)
    ans+= m
    now.remove(m)

print(ans)
    


# Optimized solution
n,m=list(map(int,input().split()))
A=[]
for i in range(n):
    a,b=list(map(int,input().split()))
    A.append([a,b])
A=sorted(A, key=lambda x:(x[0], -x[1]))
from collections import deque
A=deque(A)

# import numpy as np
import bisect as bi
ans=0
now=[]
for nouki in range(1,m+1):
    while A:
        a,b=A.popleft()
        if a>nouki: A.appendleft([a,b]);break
        bi.insort(now,b)
    #print(nouki, now)
    if now==[]:continue
    ans+= now[-1]
    del now[-1]


print(ans)
# Original solution
import bisect

n,m = (int(num) for num in input().split())
ab = [[0,0] for _ in range(n)]
for i in range(n):
    a,b = (int(num) for num in input().split())
    ab[i][0] = a
    ab[i][1] = b
#print(ab)
ab.sort(key = lambda x: x[1], reverse=True)


days = [i+1 for i in range(m)]
leng = m
ans = 0
for item in ab:
    a = item[0]
    b = item[1]
    idx = bisect.bisect_left(days,a)
    if idx < leng:
        days.pop(idx)
        leng -= 1
        ans += b
print(ans)
    

# Optimized solution
import bisect

n,m = (int(num) for num in input().split())
ab = [[0,0] for _ in range(n)]
for i in range(n):
    a,b = (int(num) for num in input().split())
    ab[i][0] = a
    ab[i][1] = b
#print(ab)
ab.sort(key = lambda x: x[1], reverse=True)


days = [i+1 for i in range(m)]
leng = m
ans = 0
for item in ab:
    if leng <= 0:
        break;
    a = item[0]
    b = item[1]
    idx = bisect.bisect_left(days,a)
    if idx < leng:
        days.pop(idx)
        leng -= 1
        ans += b
print(ans)
# Original solution
def main():
    from collections import defaultdict
    from heapq import heappush, heappop
    _, m, *ab = list(map(int, open(0).read().split()))
    works = defaultdict(list)
    # == zip(ab[0::2], ab[1::2])
    for a, b in zip(*[iter(ab)]*2):
        # 最大値が欲しいが、heappopは最小値を取り出すので-1倍しておく
        works[a].append(-b)

    candidates, pay = [], 0
    for day in range(1, m+1):
        for x in works[day]:
            heappush(candidates, x)
        if candidates:
            pay -= heappop(candidates)
    print(pay)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    from collections import defaultdict
    from heapq import heappush, heappop
    _, m, *ab = list(map(int, open(0).read().split()))
    works = defaultdict(list)
    # == zip(ab[0::2], ab[1::2])
    for a, b in zip(*[iter(ab)]*2):
        # 最大値が欲しいが、heappopは最小値を取り出すので-1倍しておく
        works[a].append(-b)

    # candidates, pay = [], []
    # for day in range(1, m+1):
    #     for x in works[day]:
    #         heappush(candidates, x)
    #     if candidates:
    #         pay.append(heappop(candidates))
    # print(-sum(pay))

    # こっちの方が遅い
    candidates, pay = [], 0
    for day in range(1, m+1):
        for x in works[day]:
            heappush(candidates, x)
        if candidates:
            pay += heappop(candidates)
    print((-pay))


if __name__ == "__main__":
    main()
# Original solution
def main():
    from collections import defaultdict
    from heapq import heappush, heappop
    _, m, *ab = list(map(int, open(0).read().split()))
    works = defaultdict(list)
    for a, b in zip(ab[0::2], ab[1::2]):
        works[a].append(-b)

    candidates, pay = [], 0
    for day in range(1, m+1):
        for x in works[day]:
            heappush(candidates, x)
        if candidates:
            pay += heappop(candidates)
    print((-pay))

    
if __name__ == "__main__":
    main()


# Optimized solution
def main():
    from collections import defaultdict
    from heapq import heappush, heappop
    _, m, *ab = list(map(int, open(0).read().split()))
    works = defaultdict(list)
    for a, b in zip(*[iter(ab)]*2):
        works[a].append(-b)

    candidates, pay = [], 0
    for day in range(1, m+1):
        if day in works:
            for x in works[day]:
                heappush(candidates, x)
        if candidates:
            pay += heappop(candidates)
    print((-pay))


if __name__ == "__main__":
    main()
# Original solution
import heapq
N, M = list(map(int, input().split()))

value = [[] for _ in range(10 ** 5 + 1)]

for _ in range(N):
    a, b = list(map(int, input().split()))
    value[a].append(-1 * b)

for v in value:
    heapq.heapify(v)


ans = 0
for i in range(1, M + 1):
    my_max = 0
    for j, k in enumerate(list(range(i)), 1):
        if len(value[j]) > 0:
            tmp = -1 * heapq.heappop(value[j])
            if my_max < tmp:
                my_max = tmp
            else:
                heapq.heappush(value[j], tmp * -1)
    ans += my_max

print(ans)
        

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
value = [[] for _ in range(10 ** 5 + 1)]

for _ in range(N):
    a, b = list(map(int, input().split()))
    value[a].append(-1 * b)

koho = value[1]
heapq.heapify(koho)

ans = 0
for i in range(1, M + 1):
    if len(koho) > 0:
        ans += -1 * heapq.heappop(koho)
    if i < M:
        for v in value[i + 1]:
            heapq.heappush(koho, v)

print(ans)
# Original solution
N, M = list(map(int, input().strip().split()))

A = {}

for i in range(N):
  a, b = list(map(int, input().strip().split()))
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = []
for i in range(1, M + 1):
  if i in A:
    days.extend(A[i])
    days = sorted(days)
  if len(days) > 0:
    a = len(days)
    total += days.pop()

print(total)


# Optimized solution
import heapq

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

A = {}

for i in range(N):
  a, b = list(map(int, input().strip().split()))
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = []
for i in range(1, M + 1):
  if i in A:
    for a in A[i]:
      heapq.heappush(days, -a)
  if len(days) > 0:
    total += - heapq.heappop(days)

print(total)
# Original solution
from heapq import heappush, heappop
from collections import defaultdict
N, M = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(N)]

cand = defaultdict(list)
for a, b in X:
    cand[a].append(b)

pq = []
ans = 0
for i in range(1, M + 1):
    for c in cand[i]:
        heappush(pq, -c)
    if pq:
        ans -= heappop(pq)
    
print(ans)




# Optimized solution
from heapq import heappush, heappop

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

pq = []
for a, b in X:
    heappush(pq, (a, b))
    
cand = []
ans = 0
for t in range(1, M + 1):
    while pq:
        a, b = heappop(pq)
        if a <= t:
            heappush(cand, -b)
        else:
            heappush(pq, (a, b))
            break

    if cand:
        v = heappop(cand)
        ans += -v

print(ans)
# Original solution
N, M = list(map(int, input().split()))
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    work[i][1], work[i][0] = list(map(int, input().split()))
ans = 0
work_money = sorted(work)
schedule = [0]*M
for j in range(N):
    day = M - work_money[N - 1 - j][1]
    day_index = day
    while day_index >= 0:
        if schedule[day_index] == -1:
            break
        if schedule[day_index] == 0:
            if day_index > 0:
                if schedule[day_index - 1] == -1:
                    schedule[day_index] = -1
                    ans += work_money[N - 1 - j][0]
                    break
            schedule[day_index] = 1
            ans += work_money[N - 1 - j][0]
            break
        day_index -= 1
        if day_index == -1:
            schedule[day] = -1
print(ans)


# Optimized solution
import heapq
N, M = list(map(int, input().split()))
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    a, b = list(map(int, input().split()))
    if M-a >= 0:
        work[i][0], work[i][1] = a, -1 * b
    else:
        work[i][0], work[i][1] = 10**9, -1 * b
ans = 0
work_day = sorted(work)
index = 0
heap = []
heapq.heapify(heap)
for j in range(M+1):
    if index >= N:
        if len(heap) > 0:
            ans += heapq.heappop(heap)*(-1)
        continue
    while (work_day[index][0] <= j):
        heapq.heappush(heap, work_day[index][1])
        index += 1
        if index >= N:
            break
    if len(heap) > 0:
        ans += heapq.heappop(heap)*(-1)


print(ans)
# Original solution
import heapq

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

Days = [[] for i in range(m)]
for i in range(n):
    if Jobs[i][0] <= m:
        Days[Jobs[i][0]-1] += [[-Jobs[i][1],Jobs[i][0]]]

Heap = []
heapq.heapify(Heap)
ans = 0
for i in range(m):
    for job in Days[i]:
        heapq.heappush(Heap,job)
    if Heap:
        ans -= heapq.heappop(Heap)[0]
        
print(ans)

# Optimized solution
from heapq import heappop,heappush

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

job.sort(key=lambda x:x[1],reverse=True)
job.sort(key=lambda x:x[0])

heap = []
ans = 0

j = -1
for i in range(M)[::-1]:
    while j < N-1 and job[j+1][0]+i <= M:
        j += 1
        heappush(heap,-job[j][1])
    if heap:
        ans += -heappop(heap)

print(ans)
# Original solution
import heapq
N, M = list(map(int, input().split()))
Work = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x: x[0])

ans = 0
can_work = []
i = 0
for m in range(M + 1):
    while (i < N) and (Work[i][0]) <= m:
        heapq.heappush(can_work, -Work[i][1])
        i += 1
    if can_work:
        ans += -heapq.heappop(can_work)

print(ans)


# Optimized solution
import heapq
N, M = list(map(int, input().split()))
works = sorted([list(map(int, input().split())) for i in range(N)])

ans = 0
hq = []
i = 0
for limit in range(1, M + 1):
    while (i < N) and (works[i][0] <= limit):
        cost, value = works[i]
        heapq.heappush(hq, -value)
        i += 1

    if hq:
        ans += -heapq.heappop(hq)

print(ans)
# Original solution
from heapq import*
(N,M),*t=[list(map(int,s.split()))for s in open(0)]
q=[0]*7**6
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v[:M]:
	for j in i:heappush(q,-j)
	z-=heappop(q)
print(z)


# Optimized solution
from heapq import*
(n,m),*t=[list(map(int,s.split()))for s in open(0)]
q=[0]*7**6
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v[:m]:
  for j in i:heappush(q,-j)
  z-=heappop(q)
print(z)