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

from sys import exit
n = int(input())
stone = list(input())
w_num = stone.count("W")
r_num = stone.count("R")
if w_num == n or r_num == n:
    print(0)
    exit()

def w_serch(entry_point):
    for i in range(n):
        try:
            pointer_left = entry_point + i
            if stone[pointer_left] == "W":
                return pointer_left
        except:
            pass

def r_serch(entry_point):
    for i in range(n):
        try:
            pointer_right = entry_point - i
            if stone[pointer_right] == "R":
                return pointer_right
        except:
            pass

counter = 0
for i in range(n // 2):
    entry_point_left = 0
    entry_point_right = n-1
    pointer_l = w_serch(entry_point_left)
    pointer_r = r_serch(entry_point_right)

    if pointer_l > pointer_r:
        break
    else:
        stone[pointer_l] = "R"
        stone[pointer_r] = "W"
        entry_point_left = pointer_l
        entry_point_right = pointer_r
    #print(stone, pointer_l, pointer_r)
    counter += 1
print(counter)

### FIXED CODE ###

from sys import exit
n = int(input())
stone = list(input())
w_num = stone.count("W")
r_num = stone.count("R")
if w_num == n or r_num == n:
    print(0)
    exit()

####
#stone = ["W"] * 100000 + ["R"] * 10000
####

def w_serch(entry_point):
    for i in range(n):
        try:
            pointer_left = entry_point + i
            if stone[pointer_left] == "W":
                return pointer_left
        except:
            return n

def r_serch(entry_point):
    for i in range(n):
        try:
            pointer_right = entry_point - i
            if stone[pointer_right] == "R":
                return pointer_right
        except:
            return -1

counter = 0
pointer_l = 0
pointer_r = n-1
for i in range(n // 2):
    pointer_l = w_serch(pointer_l)
    pointer_r = r_serch(pointer_r)
    #print(stone, pointer_l, pointer_r)
    if pointer_l > pointer_r:
        break
    else:
        stone[pointer_l] = "R"
        stone[pointer_r] = "W"
    #print(stone, pointer_l, pointer_r)
    counter += 1
print(counter)
### BUGGY CODE ###

n,m,s,t=map(int,input().split())
TRAIN=[None]*m

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


YEN=[10**16]*(n+1)#sから国iまで行くときの最小円
SNOOK=[10**16]*(n+1)#国iからtまで行くときの最小スヌーク
YEN[s]=0
SNOOK[t]=0


TRAINMAP=[[] for i in range(n+1)]

for i in range(m):
    TRAINMAP[TRAIN[i][0]]+=[(TRAIN[i][1],TRAIN[i][2],TRAIN[i][3])]
    TRAINMAP[TRAIN[i][1]]+=[(TRAIN[i][0],TRAIN[i][2],TRAIN[i][3])]


checktown=s
checked=set()
alltown=set(list(range(1,n+1)))

while checked != alltown:
    for i in TRAINMAP[checktown]:
        if YEN[i[0]]>YEN[checktown]+i[1]:
            YEN[i[0]]=YEN[checktown]+i[1]


    checked=checked|{checktown}
    YENcheck=10**16

    for i in (alltown - checked):
        if YENcheck>YEN[i]:
            YENcheck=YEN[i]
            checktown=i




checktown=t
checked=set()
alltown=set(list(range(1,n+1)))

while checked != alltown:
    for i in TRAINMAP[checktown]:
        if SNOOK[i[0]]>SNOOK[checktown]+i[2]:
            SNOOK[i[0]]=SNOOK[checktown]+i[2]


    checked=checked|{checktown}
    SNOOKcheck=10**16

    for i in (alltown - checked):
        if SNOOKcheck>SNOOK[i]:
            SNOOKcheck=SNOOK[i]
            checktown=i



MONEY=[10**16]*(n+1)
MONEY[n]=YEN[n]+SNOOK[n]

for i in range(n-1,0,-1):
    if YEN[i]+SNOOK[i]<MONEY[i+1]:
        MONEY[i]=YEN[i]+SNOOK[i]
    else:
        MONEY[i]=MONEY[i+1]


for i in range(1,n+1):
    print(10**15-MONEY[i])


### FIXED CODE ###

import heapq

n,m,s,t=map(int,input().split())
TRAIN=[None]*m

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


YEN=[10**16]*(n+1)#sから国iまで行くときの最小円
SNOOK=[10**16]*(n+1)#国iからtまで行くときの最小スヌーク
YEN[s]=0
SNOOK[t]=0


TRAINMAP=[[] for i in range(n+1)]

for i in range(m):
    TRAINMAP[TRAIN[i][0]]+=[(TRAIN[i][1],TRAIN[i][2],TRAIN[i][3])]
    TRAINMAP[TRAIN[i][1]]+=[(TRAIN[i][0],TRAIN[i][2],TRAIN[i][3])]



checking=[(0,s)]

while checking:

    cost,checktown=heapq.heappop(checking)
    
    for i in TRAINMAP[checktown]:
        if YEN[i[0]]>cost+i[1]:
            YEN[i[0]]=cost+i[1]
            heapq.heappush(checking,(cost+i[1],i[0]))

checking=[(0,t)]

while checking:

    cost,checktown=heapq.heappop(checking)
   
    for i in TRAINMAP[checktown]:
        if SNOOK[i[0]]>cost+i[2]:
            SNOOK[i[0]]=cost+i[2]
            heapq.heappush(checking,(cost+i[2],i[0]))



MONEY=[10**16]*(n+1)
MONEY[n]=YEN[n]+SNOOK[n]

for i in range(n-1,0,-1):
    if YEN[i]+SNOOK[i]<MONEY[i+1]:
        MONEY[i]=YEN[i]+SNOOK[i]
    else:
        MONEY[i]=MONEY[i+1]


for i in range(1,n+1):
    print(10**15-MONEY[i])
### BUGGY CODE ###

import sys
results = {}
def fact(n):
    if n in results:
        return results[n]
    ret = [1]
    if n%2==0:
        ret.append(2)
        results[n]=ret
        return ret
    for i in range(3,int(n**0.5)+1,2):
        if n%i==0:
            ret.append(i)
            results[n]=ret
            return ret
    results[n]=ret
    return ret
prime_results = {}
def isPrime(n):
    if n in prime_results:
        return prime_results[n]
    if n<=1:
        prime_results[n]=False
        return False
    if n==2:
        prime_results[n]=True
        return True
    if fact(n)==[1]:
        prime_results[n]=True
        return True
    else:
        prime_results[n]=False
        return False
lines = sys.stdin.readlines()
for line in lines:
    inp = int(line)
    s=0
    for i in range(2,inp+1):
        if isPrime(i):
            s+=1
    print (s)

### FIXED CODE ###

import sys

def primeTable(n):
    list = [True for _ in range(n+1)]
    i = 2
    while i * i <= n:
        if list[i]:
            j = i+i
            while j<= n:
                list[j] = False
                j += i
        i += 1

    table = [i for i in range(n+1) if list[i] and i >= 2]
    return table

lines = sys.stdin.readlines()
primeList = primeTable(999999)

for line in lines:
    inp = int(line)
    primes = list(filter(lambda n:n>=2 and n<=inp, primeList))
    num = len(primes)
    print (num)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
for i in range(1,n+1):
  ans=a.index(i)+1
  print(ans)

  


### FIXED CODE ###

n = int(input())
num_list = list(map(int,input().split()))
num_array = [0]*n
for i in range(n):
    num_array[num_list[i]-1] = i+1
for i in range(n):
    print(num_array[i])
### BUGGY CODE ###

a, b = map(int, input().split())
for i in range(max(a,b)):
  if abs(a-i) == abs(b-i):
    print(i)
    exit()
print("IMPOSSIBLE")

### FIXED CODE ###

a, b = map(int, input().split())
if ((a+b)/2).is_integer():
  print((a+b)//2)
  exit()
print("IMPOSSIBLE")
### BUGGY CODE ###

n = int(input())
count_dict = {}
for _ in range(n):
    e = input()
    if e in count_dict.keys():
        count_dict[e] += 1
    else:
        count_dict[e] = 0

max_list = [e[0] for e in count_dict.items() if e[1] == max(count_dict.values())]
max_list.sort()
for i in max_list:
    print(i)

### FIXED CODE ###

n = int(input())
count_dict = {}
for _ in range(n):
    e = input()
    if e in count_dict.keys():
        count_dict[e] += 1
    else:
        count_dict[e] = 1

m = max(count_dict.values())
for e in sorted(k for k in count_dict if count_dict[k] == m):
    print(e)
### 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 = [comb]
    x_list = [x]
    for i in range(1, k+1):
        comb = comb * (n-i) // i
        x = x * (m-1) % div
        comb_list.append(comb)
        x_list.append(x)

    x_list.reverse()
    for i in range(k+1):
        ret += comb_list[i] * x_list[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 ###

import sys
sys.setrecursionlimit(100000)

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

    if n == 1:
        print(data[0])
    elif n == 2:
        print(data[1], data[0])
    else:
        firs_value = [data[0]]
        second_value = [data[1], data[0]]

        def get_value(index, data):
            if index == 0:
                return firs_value
            elif index == 1:
                return second_value
            else:
                lis = get_value(index - 2, data)
                lis.append(data[index - 1])
                lis.insert(0, data[index])
                return lis

        result = get_value(n - 1, data)
        print(" ".join(str(n) for n in result))

if __name__ == '__main__':
    main()

### FIXED CODE ###

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

    if n == 1:
        print(data[0])
    else:
        result = [None] * n

        if n % 2 == 0:
            center = int(n / 2)
            for index, value in enumerate(data):
                if index % 2 == 0:
                    result[center + int(index / 2)] = value
                else:
                    result[center - int((index + 1) / 2)] = value
            print(" ".join(str(n) for n in result))
        else:
            center = int(n / 2)
            for index, value in enumerate(data):
                if index % 2 == 0:
                    result[center - int(index / 2)] = value
                else:
                    result[center + int((index + 1) / 2)] = value
            print(" ".join(str(n) for n in result))

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

def solve():
    N = int(raw_input())
    arr = []
    for i in range(N):
        arr.append(int(raw_input()))
    turning_on = 1
    count = 0
    turned_on_set = [1]
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if turning_on in turned_on_set:
            count = -1
            break
        turned_on_set.append(turning_on)
    print str(count)

if __name__ == '__main__':
    solve()


### FIXED CODE ###

def solve():
    N = int(raw_input())
    arr = []
    for i in range(N):
        arr.append(int(raw_input()))
    turning_on = 1
    count = 0
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if count > N + 1:
            count = -1
            break
    print str(count)

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

import sys
input = sys.stdin.readline

H, W = map(int, input().split())
G = [[i for i in input()] for j in range(H)]

ls = []
dp = [[-1 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if G[h][w] == "#":
            ls.append([h,w])
            dp[h][w] = 1

ans = -1
while len(ls) > 0:
    ans += 1
    temp = []
    for h, w in ls:
        if 0 <= h+1 <= H-1 and dp[h+1][w] == -1:
            dp[h+1][w] = 1
            temp.append([h+1,w])
        if 0 <= h-1 <= H-1 and dp[h-1][w] == -1:
            dp[h-1][w] = 1
            temp.append([h-1,w])
        if 0 <= w+1 <= W-1 and dp[h][w+1] == -1:
            dp[h][w+1] = 1
            temp.append([h,w+1])
        if 0 <= w-1 <= W-1 and dp[h][w-1] == -1:
            dp[h][w-1] = 1
            temp.append([h,w-1])
    ls = temp

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

H, W = map(int, input().split())
G = [[i for i in input()] for j in range(H)]

ls = []
dp = [[-1 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if G[h][w] == "#":
            ls.append([h,w])
            dp[h][w] = 1

ans = -1
while len(ls) > 0:
    ans += 1
    temp = []
    for h, w in ls:
        if 0 <= h+1 <= H-1 and dp[h+1][w] == -1:
            dp[h+1][w] = 1
            temp.append([h+1,w])
        if 0 <= h-1 <= H-1 and dp[h-1][w] == -1:
            dp[h-1][w] = 1
            temp.append([h-1,w])
        if 0 <= w+1 <= W-1 and dp[h][w+1] == -1:
            dp[h][w+1] = 1
            temp.append([h,w+1])
        if 0 <= w-1 <= W-1 and dp[h][w-1] == -1:
            dp[h][w-1] = 1
            temp.append([h,w-1])
    ls = temp

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

a,b,c,d = map(int,input().split())
tmp = 0

for i in range(a,b+1):
  if i%c != 0 and i%d != 0:
    tmp += 1
    
print(tmp)

### FIXED CODE ###

from fractions import gcd
a,b,c,d = map(int,input().split())
e = (c*d)//gcd(c,d)

print((b-a+1) - (b//c - (a-1)//c) - (b//d - (a-1)//d) + (b//e - (a-1)//e))
### BUGGY CODE ###

N, M = (int(x) for x in input().split())

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

# 魔法実行回数
magic_act_num = 0

known = [False for x in range(N)]
judge = 0

for x, y, _ in xyz:
    x = x-1
    y = y-1
    if known[x] and known[y]:
        continue;

    # zは気にしなくてよい
    if known[x]:
        if known[y] is False:
            known[y] = True
            judge += 1
    else:
        if known[y]:
            known[x] = True
            judge += 1
        else:
            known[x] = True
            known[y] = True
            judge += 2
            magic_act_num += 1

    for a, b, _ in xyz:
        a = a - 1;
        b = b - 1;
        if known[a] is x and known[b] is y:
            continue
        if known[a] is y and known[b] is x:
            continue
        if known[a] is x:
            if known[b] is not y:
                known[b] = True
        if known[a] is y:
            if known[b] is not x:
                known[b] = True
        if known[b] is x:
            if known[a] is not y:
                known[a] = True
        if known[b] is y:
            if known[a] is not x:
                known[a] = True

magic_act_num += (N-judge)

print(magic_act_num)


### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

class UnionFind:
    def __init__(self, num):
        self.p = [x for x in range(num)]
    def root(self, x):
        if self.p[x] == x:
            return x
        else:
            self.p[x] = self.root(self.p[x])
            return self.p[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx != ry:
            self.p[ry] = rx

def main():
    N, M = (int(x) for x in input().split())
    u = UnionFind(N)

    for i in range(M):
        x,y,_ = map(int, input().split())
        u.unite(x-1, y-1)

    rlist = set()
    for i in range(N):
        rlist.add(u.root(i))

    print(len(rlist))

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

k,s=map(int,input().split())
c=0

for l in range(k+1):
    for m in range(k+1):
        for n in range(k+1):
            if l+m+n==s:
                c+=1
                
print(c)

### FIXED CODE ###

k,s=map(int,input().split())
c=0

for l in range(k+1):
    for m in range(k+1):
        if 0<=s-l-m and s-l-m<=k:
            c+=1
print(c)
### BUGGY CODE ###

#!/usr/bin/env python3
def main():
    from bisect import bisect

    _ = int(input())
    A = sorted([int(x) for x in input().split()])
    B = sorted([int(x) for x in input().split()])
    C = sorted([int(x) for x in input().split()])

    lst = []
    for b in B:
        lst.append(len(C[bisect(C, b):]))
    ans = 0
    for a in A:
        ans += sum(lst[bisect(B, a):])
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
def main():
    from bisect import bisect

    N = int(input())
    A = sorted([int(x) for x in input().split()])
    B = sorted([int(x) for x in input().split()])
    C = sorted([int(x) for x in input().split()])

    lst = [N - bisect(C, b) for b in B]
    lst = lst[::-1]
    tmp_lst = [lst[0]]
    for i in lst[1:]:
        tmp_lst.append(i + tmp_lst[-1])
    tmp_lst = tmp_lst[::-1]

    ans = 0
    for a in A:
        res = bisect(B, a)
        ans += tmp_lst[res] if res < N else 0
    print(ans)


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

import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    MOD = 10 ** 9 + 7

    ans = 0
    for i, a in enumerate(A[:-1]):
        ans += sum([a ^ b for b in A[i+1:]]) % MOD
        ans %= MOD
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = list(map(int, input().split()))
    MAX_BIT = 60
    MOD = 10 ** 9 + 7

    data = [0] * (MAX_BIT + 2)
    for a in A:
        for bit in range(MAX_BIT + 1):
            if a & (1 << bit):
                data[bit] += 1
    ans = 0
    for i, d in enumerate(data):
        if d == 0:
            continue
        ans += pow(2, i, MOD) * d * (N-d)
        ans %= MOD
    print(ans)

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

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

AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, key=lambda x: x[1])
AB.reverse()

res = 0

f = [True]*M
for a, b in AB:
    if a > M:
        continue
    for i in range(a-1, M):
        if f[i]:
            f[i] = False
            res += b
            break

print(res)


### FIXED CODE ###

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

AB = sorted(AB, key=lambda x: x[0])

x = 0
res = 0

hq = []
heapq.heapify(hq)

for i in range(M):
    for j in range(x, N):
        if AB[j][0] > i+1:
            x = j
            break
        heapq.heappush(hq, AB[j][1]*-1)
    else:
        x = N
    
    if hq:
        res += heapq.heappop(hq)*-1

print(res)
### BUGGY CODE ###

def main():
    s = input()
    lst = [0] * len(s)
    for i, c in enumerate(s):
        if c == 'R':
            for j, t in enumerate(s[i+1:]):
                if t == 'L':
                    if (j + 1) % 2 == 0:
                        lst[i+j+1] += 1
                        break
                    else:
                        lst[i+j] += 1
                        break
        else:
            for j, t in enumerate(reversed(s[:i])):
                if t == 'R':
                    if (j + 1) % 2 == 0:
                        lst[i-(j+1)] += 1
                        break
                    else:
                        lst[i-j] += 1
                        break
    print(' '.join(list(map(str, lst))))


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    s = input()
    n = len(s)
    ret = [0] * n
    for i in range(n-1):
        if s[i:i+2] == "RL":
            for j in range(i, -1, -1):
                if s[j] == "R":
                    ret[i + (i-j) % 2] += 1
                else:
                    break
            for j in range(i+1, n):
                if s[j] == "L":
                    ret[i + (j-i) % 2] += 1
                else:
                    break
    
    print(" ".join(map(str, ret)))

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

# sumitb2019_d.py
'''
重複考えないとどうなるか
-> 単に nC3 個( N 個から 3 つ選び出す順列と同じ)
'''
N = int(input())
S = input()
cnt = 0
for i in range(1000):
    C = [i//100, i//10%10, i%10]
    f = 0
    for s in S:
        if int(s) == C[f]:
            f += 1
        if f == 3:
           break
    if f == 3:
        cnt += 1
print(cnt)

### FIXED CODE ###

# sumitb2019_d.py
'''
重複考えないとどうなるか
-> 単に nC3 個( N 個から 3 つ選び出す順列と同じ)
というところまで考えたが、結局わからず解説AC
重複を考えるとかなり厳しいので重複がないものに着目する。
（今回であれば 000~999 までのパスワード）
発想の転換が凄い。これ思いつくの天才だろ…。
'''
N = int(input())
S = input()
cnt = 0
for i in range(1000):
    C = [str(i//100), str(i//10%10), str(i%10)]
    f = 0
    for s in S:
        if s == C[f]:
            f += 1
        if f == 3:
           break
    if f == 3:
        cnt += 1
    if cnt ==1000:
        break
print(cnt)
### BUGGY CODE ###

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

L.sort()

cnt = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        b = L[j]
        for s in range(N-1, j, -1):
            c = L[s]
            if L[s]<L[i]+L[j]:
                cnt += s - j
                break
print(cnt)

### FIXED CODE ###

from bisect import bisect_left

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

L.sort()

cnt = 0

# 2番目に長い棒bを固定
for b in range(N):
    # bより短い棒aを一つずつ探索
    for a in range(b):
        ab = L[a] + L[b]
        # 二分探索でa+bより短い棒（c<a+bが成り立つc）の範囲を求める
        r = bisect_left(L, ab)
        # cの範囲のうちbの範囲を除外して、cの本数を求める
        cnt += r - (b + 1)

print(cnt)
### BUGGY CODE ###

K = int(input())

if K ==2 or K == 5 or K%2==0:
    print(-1)
    exit()

preamari = 7%K
ans = 1
while preamari != 0:
    preamari = (preamari*10 + 7%K)%K
    ans+=1

print(ans)

### FIXED CODE ###

K = int(input())

if K ==2 or K%5==0 or K%2==0:
    print(-1)
    exit()

preamari = 7%K
ans = 1
while preamari != 0:
    preamari = (preamari*10 + 7%K)%K
    ans+=1

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

import sys
sys.setrecursionlimit(10 ** 6)

import collections

inputs = [int(x) for x in input().split()]
n, m = inputs[0], inputs[1]
conditions = {}
for i in range(n):
  inputs = [int(x) for x in input().split()]
  if inputs[0] in conditions.keys():
    conditions[inputs[0]].append(inputs[1])
  else:
    conditions[inputs[0]] = collections.deque([inputs[1]])

for day in conditions.keys():
  conditions[day] = sorted(conditions[day], reverse=True)

ans = 0
candidates = []
for reversed_day in range(1, m + 1):
  if reversed_day in conditions.keys():
    candidates.append(conditions[reversed_day])

  if len(candidates) == 0:
    continue

  cnt, maxval, maxkey = 0, 0, 0
  for cands in candidates:
    if cands[0] > maxval:
      maxval = cands[0]
      maxkey = cnt
    cnt += 1
  ans += candidates[maxkey].pop(0)
  if len(candidates[maxkey]) == 0:
    del candidates[maxkey]
print(ans)

### FIXED CODE ###


import collections
import heapq

inputs = [int(x) for x in input().split()]
n, m = inputs[0], inputs[1]
conditions = collections.deque([])
for i in range(n):
  conditions.append([int(x) for x in input().split()])

conditions = sorted(conditions)

cnt, ans = 0, 0
candidates = []
for reversed_day in range(1, m + 1):
  while cnt < len(conditions) and conditions[cnt][0] <= reversed_day:
    heapq.heappush(candidates, -conditions[cnt][1])
    cnt += 1

  if len(candidates) == 0:
    continue
  ans -= heapq.heappop(candidates)

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

N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if L[i] + L[j] > L[k]:
                ans += 1
            else:
                break
print(ans)

### FIXED CODE ###

import bisect

N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        ans += bisect.bisect_left(L,L[i]+L[j]) - j - 1
print(ans)