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

def solve(h, w, d, AA, q, LR):
    Ainv = {
        AA[y][x]: (x, y)
        for x in range(w)
        for y in range(h)
    }

    costs = [0] * q
    for i, (l, r) in enumerate(LR):
        while l != r:
            xf, yf = Ainv[l]
            xt, yt = Ainv[l + d]
            costs[i] += abs(xt - xf) + abs(yt - yf)
            l = AA[yt][xt]

    return costs

_h, _w, _d = map(int, input().split())
_AA = [list(map(int, input().split())) for _ in range(_h)]
_q = int(input())
_LR = [list(map(int, input().split())) for _ in range(_q)]
print(*solve(_h, _w, _d, _AA, _q, _LR), sep='\n')


### FIXED CODE ###

def solve(h, w, d, AA, q, LR):
    Ainv = {
        AA[y][x]: (x, y)
        for x in range(w)
        for y in range(h)
    }
    costs = [0] * (h * w)
    for i in range(d , h * w):
        xf, yf = Ainv[i - d + 1]
        xt, yt = Ainv[i + 1]
        costs[i] = costs[i - d] + abs(xt - xf) + abs(yt - yf)

    ret = [0] * q
    for i, (l, r) in enumerate(LR):
        ret[i] = costs[r - 1] - costs[l - 1]

    return ret

_h, _w, _d = map(int, input().split())
_AA = [list(map(int, input().split())) for _ in range(_h)]
_q = int(input())
_LR = [list(map(int, input().split())) for _ in range(_q)]
print(*solve(_h, _w, _d, _AA, _q, _LR), sep='\n')
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n = int(input())
a = [int(i) for i in input().split()]

# i番目までの和が正(i: 奇数)
res1 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 1:
        while cum <= 0:
            cum += 1
            res1 += 1
    else:
        while cum >= 0:
            cum -= 1
            res1 += 1

# i番目までの和が負(i: 奇数)
res2 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0:
        while cum <= 0:
            cum += 1
            res2 += 1
    else:
        while cum >= 0:
            cum -= 1
            res2 += 1

print(min(res1,res2))

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
a = [int(i) for i in input().split()]

# i番目までの和が正(i: 奇数)
res1 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 1:
        if cum <= 0:
            res1 += (1 - cum)
            cum = 1
    else:
        if cum >= 0:
            res1 += (cum + 1)
            cum = -1

# i番目までの和が正(i: 偶数)
res2 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0:
        if cum <= 0:
            res2 += (1 - cum)
            cum = 1
    else:
        if cum >= 0:
            res2 += (cum + 1)
            cum = -1

print(min(res1,res2))
### BUGGY CODE ###

# ABC171 E 

import itertools

n = int(input())
a_l = [format(int(x), "030b") for x in input().split()]

for _d,_tp in enumerate(zip(*a_l)):
    if "1" not in _tp:
        continue
    if "0" not in _tp:
        continue
    else:
        _num = _tp.count("0")
        if _num % 2 == 1:
            for i in range(n):
                _list = list(a_l[i])
                if _list[_d] == "0":
                    _list[_d] = "1"
                else:
                    _list[_d] = "0"
                a_l[i] = "".join(_list)

print(*[int(x, 2) for x in a_l])

### FIXED CODE ###

# ABC171 E 

import itertools

n = int(input())
a_l = [list(format(int(x), "030b")) for x in input().split()]

for _d,_tp in enumerate(zip(*a_l)):
    if "1" not in _tp:
        continue
    if "0" not in _tp:
        continue
    else:
        _num = _tp.count("0")
        if _num % 2 == 1:
            for i in range(n):
                if a_l[i][_d] == "0":
                    a_l[i][_d] = "1"
                else:
                    a_l[i][_d] = "0"

print(*[int("".join(x), 2) for x in a_l])
### BUGGY CODE ###

from sys import stdin
N=int(stdin.readline().rstrip())
a=[N]+[0]*10000000
for i in range(10000000):
    if i !=0:
        if a[i-1]%2==0:
            a[i]=int((a[i-1]/2))
        else:
            a[i]=int((3*a[i-1]+1))
cnt=1
tmp=[]
for j in a:
    if str(j) in tmp:
        break
    else:
        tmp.append(str(j))
        cnt+=1
print(cnt)

### FIXED CODE ###

from sys import stdin
N=int(stdin.readline().rstrip())
a=[N]+[0]*10000
for i in range(10000):
    if i !=0:
        if a[i-1]%2==0:
            a[i]=int((a[i-1]/2))
        else:
            a[i]=int((3*a[i-1]+1))
cnt=1
tmp=[]
for j in a:
    if str(j) in tmp:
        break
    else:
        tmp.append(str(j))
        cnt+=1
print(cnt)
### BUGGY CODE ###

N, X, Y = map(int, input().split())
graph = [set() for _ in range(N)]
graph[X - 1].add(Y - 1)
graph[Y - 1].add(X - 1)
for i in range(N - 1):
    graph[i].add(i + 1)
    graph[i + 1].add(i)


dist = [[10 ** 9] * N for _ in range(N)]
for i in range(N):
    queue = [i]
    seen = {i}
    dist[i][i] = 0
    while queue:
        new = []
        for cur in queue:
            for nei in graph[cur]:
                dist[i][nei] = min(dist[i][nei], dist[i][cur] + 1)
                if nei not in seen:
                    new.append(nei)
                    seen.add(nei)
        queue = new
count = [0] * N
for i in range(N):
    for j in range(i + 1, N):
        count[dist[i][j]] += 1

for c in count[1:]:
    print(c)

### FIXED CODE ###

N, X, Y = map(int, input().split())
graph = [set() for _ in range(N)]
graph[X - 1].add(Y - 1)
graph[Y - 1].add(X - 1)
for i in range(N - 1):
    graph[i].add(i + 1)
    graph[i + 1].add(i)


dist = [[10 ** 9] * N for _ in range(N)]
for i in range(N):
    queue = [i]
    seen = {i}
    dist[i][i] = 0
    while queue:
        new = []
        for cur in queue:
            for nei in graph[cur]:
                dist[i][nei] = min(dist[i][nei], dist[i][cur] + 1)
                if nei not in seen:
                    new.append(nei)
                    seen.add(nei)
        queue = new
count = [0] * N
for i in range(N):
    for j in range(i + 1, N):
        count[dist[i][j]] += 1

for c in count[1:]:
    print(c)
### BUGGY CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
for i in range(M):
  A.sort()
  for j in range(BC[i][0]):
    if A[j]<BC[i][1]:
      A[j]=BC[i][1]
print(sum(A))



### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)
### BUGGY CODE ###

from collections import deque

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

divisor = deque([])

for i in range(2, M + 1) :
    if M % i == 0 :
        divisor.append(i)
    if i * N > M :
        break

while divisor :
    div = divisor.pop()
    if M // div >= N :
        print(div)
        break
else :
    print(1)


### FIXED CODE ###

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

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

div = divisors(M)

for d in div :
    if M >= N * d :
        print(d)
        break
### BUGGY CODE ###

#!/usr/bin/env python

def main():
    n, m, k = map(int, input().split())
    div = 998244353

    ret = 0
    comb = 1
    x = m * ((m-1) ** (n-k-1))
    comb_list = [0] * (k+1)
    x_list = [0] * (k+1)
    comb_list[0] = comb
    x_list[0] = x
    for i in range(1, k+1):
        comb = comb * (n-i) // i
        x = x * (m-1) % div
        comb_list[i] = comb
        x_list[i] = x

    for i in range(k+1):
        ret += comb_list[i] * x_list[k-i] % div
        ret %= div

    print(ret)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python
def modpow(x, y, mod):
    ret = 1
    z = x
    n = len(bin(y)) - 2
    for i in range(n):
        if ((y >> i) & 1):
            ret = ret * z % mod
        z = z * z % mod
    return ret

def modinv(x, mod):
    return modpow(x, mod-2, mod)

def main():
    n, m, k = map(int, input().split())
    mod = 998244353

    c1 = 1
    c2 = 1
    for i in range(1, n):
        c1 = c1 * i % mod
        if i == k:
            c2 = modinv(c1, mod)
    c3 = modinv(c1, mod)
    c3_list = [0] * (k+1)
    c3_list[k] = c3
    for i in range(k-1, -1, -1):
        c3 = c3 * (n-(k-i)) % mod
        c3_list[i] = c3
    comb = (c1 * c2 % mod) * c3_list[0] % mod

    x = m * modpow(m-1, n-k-1, mod) % mod

    ret = x * comb % mod

    for i in range(1, k+1):
        c2 =c2 * (k-i+1) % mod
        comb = (c1 * c2 % mod) * c3_list[i] % mod
        x = x * (m-1) % mod

        ret += x * comb % mod
        ret %= mod

    print(ret)


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

# https://atcoder.jp/contests/m-solutions2019/submissions/5741430
from operator import mul

MOD = 10 ** 6 + 3


def inv(n):
    return pow(n, MOD - 2, MOD)


def mod_mul(x, y):
    return mul(x, y) % MOD


fact = [1]
for n in range(1, MOD):
    fact.append(mod_mul(fact[-1], n))
    # d>1 の数列を d=1 となる数列に変換する
    # MODの倍数を含む数列の積は0になるので,
    # MODの倍数を含まない区間の積が計算できればよい
    # そのような区間の右端はMOD-1であり,fact[MOD-1]が計算できればよい

Q = int(input())
for _ in range(Q):
    x, d, n = map(int, input().split())

    if x == 0:
        print(0)
        continue

    if d == 0:
        print(pow(x, n, MOD))
        continue

    xd = mod_mul(x, inv(d))
    if MOD - xd < n:
        print(0)
        continue

    dn = pow(d, n, MOD)
    print(mod_mul(mod_mul(fact[xd + n - 1], inv(fact[xd - 1])), dn))


### FIXED CODE ###

# https://atcoder.jp/contests/m-solutions2019/submissions/5741430
from operator import mul
import sys

input = sys.stdin.readline

MOD = 10 ** 6 + 3


def inv(n):
    return pow(n, MOD - 2, MOD)


def mod_mul(x, y):
    return mul(x, y) % MOD


fact = [1]
for n in range(1, MOD):
    fact.append(mod_mul(fact[-1], n))
    # d>1 の数列を d=1 となる数列に変換する
    # MODの倍数を含む数列の積は0になるので,
    # MODの倍数を含まない区間の積が計算できればよい
    # そのような区間の右端はMOD-1であり,fact[MOD-1]が計算できればよい

Q = int(input())
for _ in range(Q):
    x, d, n = map(int, input().split())

    if x == 0:
        print(0)
        continue

    if d == 0:
        print(pow(x, n, MOD))
        continue

    xd = mod_mul(x, inv(d))
    if MOD - xd < n:
        print(0)
        continue

    dn = pow(d, n, MOD)
    print(mod_mul(mod_mul(fact[xd + n - 1], inv(fact[xd - 1])), dn))
### BUGGY CODE ###

def hantei(n,data):
    for x in range(n-1):
        for y in range(x+1,n):
            if data[x] == data[y]:
                return "NO"

    return "YES"

n = int(input())
data = [int(x) for x in input().split()]
print(hantei(n,data))


### FIXED CODE ###

def hantei(n,data):
    data.sort()
    for x in range(n-1):
        if data[x] == data[x+1]:
                return "NO"
    return "YES"

n = int(input())
data = [int(x) for x in input().split()]

print(hantei(n,data))
### BUGGY CODE ###

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

start = []
goal = []
for i in range(len(A)):
    if A[i][0] == '1':
        start.append(A[i])
    elif A[i][1] == str(N):
        goal.append(A[i])

is_ok = False
for j in range(len(start)):
    for k in range(len(goal)):
        if start[j][1] == goal[k][0]:
            is_ok = True
            break
    if is_ok:
        break

if is_ok:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

### FIXED CODE ###

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

starts = {}
goals = {}
for i in range(len(A)):
    s_num = A[i][0]
    g_num = A[i][1]
    if s_num == '1':
        starts[A[i][1]] = 0
    elif g_num == str(N):
        goals[A[i][0]] = 0

is_ok = False
for s in starts:
    if s in goals:
        is_ok = True
        break


# is_ok = False
# for j in range(len(start)):
#     for k in range(len(goal)):
#         if start[j][1] == goal[k][0]:
#             is_ok = True
#             break
#     if is_ok:
#         break

if is_ok:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
### BUGGY CODE ###

K, S = map(int, input().split())
l = [i for i in range(K+1)]
cnt = 0
for i in l:
    if i > S:
        break
    elif i == S:
        cnt += 1
        break
    elif i < S:
        for j in l:
            if i+j > S:
                break
            elif i+j == S:
                cnt += 1
                break
            elif i+j < S:
                if K >= S-i-j:
                    cnt += 1
                
print(cnt)

### FIXED CODE ###

K, S = map(int, input().split())
l = [i for i in range(K+1)]
cnt = 0
for i in l:
    if i > S:
        break
    elif i == S:
        cnt += 1
        break
    elif i < S:
        tmp1 = S-i
        if K-tmp1 >= 0:
            cnt += tmp1 + 1
        else:
            tmp2 = 2*K + 1 - tmp1
            if tmp2 > 0:
                cnt += tmp2
                
print(cnt)
### BUGGY CODE ###

n = int(input())
arr = list(map(int,input().split()))
dic = {}
d = {}
for i in arr:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1
for i in arr:
    ans=0
    if i not in d:
        for j in dic.keys():
            if i==j:
                t=dic[j]-1    
            else:t = dic[j]
            ans+=(t*(t-1))//2
        d[i]=ans
    print(d[i])

### FIXED CODE ###

n = int(input())
arr = list(map(int,input().split()))
dic = {}
for i in arr:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1
ans=0
for j in dic.keys():
    t=dic[j]
    ans+=(t*(t-1))//2
for i in arr:
    sm = ans-(dic[i]-1)
    print(sm)
### BUGGY CODE ###

N,W = map(int,input().split())
ws, vs = [], []

for i in range(N):
    w,v = map(int,input().split())
    ws.append(w)
    vs.append(v)

dp = [[0 for _ in range(W+1)] for _ in range(N)]
for i,(w,v) in enumerate(zip(ws,vs)):
    for j in range(W+1):
        if j - w >= 0:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-w] + v)
        else:
            dp[i][j] = max(dp[i][j], dp[i-1][j])

print(max(dp[-1]))

### FIXED CODE ###


import numpy as np
N,W = map(int, input().split())
# np.maximum https://www.sejuku.net/blog/69175
# 第一引数と第二引数の大小関係を要素ごとに比較して、大きい方を格納した配列を返します。
dp = np.zeros(W+1, dtype=np.int64)
for _ in range(N):
    w,v = map(int,input().split())
    # w以下ならvを足したもの、w以上ならそのままの値で、最大値を求める
    # 出力はw以上の配列として返す(それ以下は重さwなので関与しない)
    # 足す場合が前者、足さない場合が後者。その重さでの最大valueが保存されている
    np.maximum(dp[:W-w+1]+v,dp[w:],out=dp[w:]) 

print(dp[-1])
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
count=0
ans=None
d=0
for i in range(n-k+1):
  if d==0:
    s=a[count:k+count]
    gakki=1
    d=1
  else:
    del s[0]
    s.append(a[k+count])
    count+=1
    gakki=1
  
  for w in s:
    gakki*=w
  
  
  if ans is None:
    ans=gakki
  else:
    if ans<gakki:
      print('Yes')
      ans=gakki
    else:
      print("No")
      ans=gakki

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n-k):
  if a[i]<a[k+i]:
    print('Yes')
  else:
    print("No")
### BUGGY CODE ###

import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
n,q=map(int,input().split())
cnt=[0]*n
used=[0]*n
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)

def f(now):
    for to in  edges[now]:
        if used[to]:continue
        cnt[to]+=cnt[now]
        used[to]=1
        f(to)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

used[0]=1
f(0)
print(*cnt)

### FIXED CODE ###

import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
n,q=map(int,input().split())
cnt=[0]*(n)
edges=[0]*n
for i in range(n):
    edges[i]=[]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)

from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

for i in edges[0]:
    dq.append((0,i))
while dq:
    f,t=dq.popleft()
    cnt[t]+=cnt[f]
    for to in edges[t]:
        if to!=f:dq.append((t,to))
    
print(*cnt)
### BUGGY CODE ###

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

dist = [float("inf")] * n


def bellman_ford(s):
	dist[s] = 0

	for i in range(n - 1):
		for j in range(m):
			a, b, c = abc[j]
			a -= 1
			b -= 1
			c = -c

			if dist[a] == float("inf"):
				continue

			dist[b] = min(dist[b], dist[a] + c)


bellman_ford(0)
ans = -dist[-1]

negative = [False] * n

for i in range(n):
	for j in range(m):
		a, b, c = abc[j]
		a -= 1
		b -= 1
		c = -c

		if dist[a] == float("inf"):
			continue

		if dist[b] > dist[a] + c:
			dist[b] = dist[a] + c
			negative[b] = True

		if negative[a]:
			negative[b] = True

if negative[-1]:
	print("inf")
else:
	print(ans)


### FIXED CODE ###

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


def bellman_ford(s, g):
	dist = [float("inf")] * n
	dist[s] = 0
	negative = [False] * n
	for i in range(2 * n):
		for a, b, c in abc:
			a -= 1
			b -= 1
			c = -c

			if dist[b] > dist[a] + c:
				dist[b] = dist[a] + c
				if i >= n:
					negative[b] = True

	if negative[g]:
		ret = "inf"

	else:
		ret = -dist[g]

	return ret


ans = bellman_ford(0, n - 1)
print(ans)
### BUGGY CODE ###

from fractions import Fraction
MAX_SPEED = 30
while True:
    N, M = map(int, input().split())
    if not (N | M):
        break
    S, G = map(lambda x: int(x) - 1, input().split())
    edge = []
    for _ in range(M):
        x, y, d, c = map(int, input().split())
        edge.append((x - 1, y - 1, d, c))
        edge.append((y - 1, x - 1, d, c))
    INF = float('inf')
    dist = [[[INF for _ in range(N)] for _ in range(MAX_SPEED + 1)] for _ in range(N)]
    dist[S][0][S] = Fraction()
    while True:
        flag = False
        for x, y, d, c in edge:
            for j in range(min(MAX_SPEED + 1, c + 2)):
                for k in range(N):
                    if k == y or (j <= c and dist[x][j][k] == INF):
                        continue
                    # print(x, y, d, c, j, k, dist[x][j][k])
                    if j > 1 and dist[y][j - 1][x] > dist[x][j][k] + Fraction(d, j - 1):
                        dist[y][j - 1][x] = dist[x][j][k] + Fraction(d, j - 1)
                        flag = True
                    if 0 < j <= c and dist[y][j][x] > dist[x][j][k] + Fraction(d, j):
                        dist[y][j][x] = dist[x][j][k] + Fraction(d, j)
                        flag = True
                    if j < c and dist[y][j + 1][x] > dist[x][j][k] + Fraction(d, j + 1):
                        dist[y][j + 1][x] = dist[x][j][k] + Fraction(d, j + 1)
                        flag = True
        if not flag:
            break
    """
    for i in range(N):
        print(*dist[i], sep='\n')
        print()
    """
    ans = min(dist[G][1][j] for j in range(N))
    print("unreachable" if ans == INF else "{:.20f}".format(float(ans)))

### FIXED CODE ###

import sys
import heapq
if sys.version[0] == '2':
    range, input = xrange, raw_input
MAX_SPEED = 30
dvs = (-1, 0, 1)
while True:
    N, M = map(int, input().split())
    if not (N | M):
        break
    S, G = map(lambda x: int(x) - 1, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(M):
        x, y, d, c = map(int, input().split())
        edge[x - 1].append((y - 1, d, c))
        edge[y - 1].append((x - 1, d, c))
    INF = 1e9
    dist = [[[INF for _ in range(N)] for _ in range(MAX_SPEED + 1)] for _ in range(N)]
    que = [(0.0, S, 0, S)]
    while que:
        cost, now, v, prev = heapq.heappop(que)
        if cost > dist[now][v][prev]:
            continue
        if now == G and v == 1:
            print("{:.20f}".format(cost))
            break
        dist[now][v][prev] = cost
        for x, d, c in edge[now]:
            if x == prev:
                continue
            for dv in dvs:
                nv = v + dv
                if 0 < nv <= c and dist[x][nv][now] > dist[now][v][prev] + d / nv:
                    dist[x][nv][now] = dist[now][v][prev] + d / nv
                    heapq.heappush(que, (dist[x][nv][now], x, nv, now))
    else:
        print("unreachable")
### BUGGY CODE ###

N = int(input())
dic = {}

for i in range(N):
    temp = input()
    if temp in dic:
        dic[temp] += 1
    else:
        dic[temp] = 1

max_kv_list = [kv[0] for kv in dic.items() if kv[1] == max(dic.values())]

max_kv_list.sort()

for i in range(len(max_kv_list)):
    print(max_kv_list[i])


### FIXED CODE ###

N = int(input())
dic = {}
max_v = 0

for i in range(N):
    temp = input()
    if temp in dic:
        dic[temp] += 1
    else:
        dic[temp] = 1
    max_v = max(max_v, dic[temp])

keys = sorted(dic.keys())
for k in keys:
    if dic[k] == max_v:
        print(k)
### BUGGY CODE ###

s = str(input())
l = len(s)
#lr = [1 if s[i] == 'R' and s[i+1] == 'L' else 0 for i in range(l-1)]
#print(lr)
ans = [0 for _ in range(l)]
for i in range(l):
    if s[i] == 'R':
        tmp = -1
        for j in range(i, l-1):
            if s[j] == 'R' and s[j+1] == 'L':
                tmp = j
                break
        if tmp == -1:
            ans[l-1] += 1
            continue
        if (tmp - i) % 2 == 0:
            ans[tmp] += 1
        else:
            ans[tmp+1] += 1
    elif s[i] == 'L':
        tmp = -1
        for j in reversed(range(0, i)):
            if s[j] == 'R' and s[j+1] == 'L':
                tmp = j
                break
        if tmp == -1:
            ans[0] += 1
            continue
        if (i - tmp) % 2 == 0:
            ans[tmp] += 1
        else:
            ans[tmp+1] += 1
    #print(ans)

for i in range(l-1):
    print(ans[i],end=' ')
print(ans[l-1])

### FIXED CODE ###

#https://atcoder.jp/contests/abc136/tasks/abc136_d

s = list(str(input()))
n = len(s)
t = []
u = []
v = [0] * n
for i in range(n):
    if i == 0:
        for j in range(i, n):
            if s[j] == 'L':
                t.append(j - 1)
                u.append(j - i - 1)
                break

    else:
        if s[i] == s[i-1]:
            t.append(t[i-1])
            u.append(u[i-1] + 1)
        elif s[i] == 'R':
            for j in range(i, n):
                if s[j] == 'L':
                    t.append(j - 1)
                    u.append(j - i - 1)
                    break
        else:
            
            for j in reversed(range(i)):
                if s[j] == 'R':
                    t.append(j)
                    u.append(i - j)
                    break
            '''
            t.append(i-1)
            u.append(0)
            '''
    a = u[i] % 2
    v[t[i] + a] += 1


print(' '.join(map(str, v)))
### BUGGY CODE ###

def resolve():
    n, m = map(int, input().split())
    jobs = []
    for i in range(n):
        days, reward = map(int, input().split())
        jobs.append((days, reward))

    jobs = sorted(jobs, key=lambda item: item[1], reverse=True)
    res = 0
    # 残りi日の時点でどの仕事をするか
    for i in range(m+1):
        for j in jobs:
            if j[0] <= i:
                res += j[1]
                jobs.remove(j)
                break

    print(res)
resolve()

### FIXED CODE ###

from heapq import heappush, heappop

def resolve():
    n, m = map(int, input().split())
    jobs = [[] for _ in range(m+1)]
    for i in range(n):
        days, reward = map(int, input().split())
        if days <= m:
            jobs[days].append(-reward)

    res = 0
    heap = []
    # 残りi日の時点でどの仕事をするか
    for i in range(m+1):
        for j in jobs[i]:
            heappush(heap, j)
        if len(heap) > 0:
            res += -heappop(heap)

    print(res)
resolve()