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

from sys import stdin, stdout
def readLine_int_list():return list(map(int, stdin.readline().split()))

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
        
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x==y: return
    if rank[x]<rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x]+=1
            
def same(x,y):
    return find(x)==find(y)
    
n,m = readLine_int_list()
v = n + m
par = [i for i in range(0, n)]
rank = [1] * n
def main():
    G = [readLine_int_list()[1:] for i in range(n)]
    
    for i in range(n):
        for ii in G[i]:
            for j in range(i+1,n):
                if ii in G[j]:
                    unite(i,j)

    # print(par)
    
    r = "YES"
    for i in range(0, n):
        if find(i) != find(0):
            r = "NO"
    print(r)
    
    
if __name__ == '__main__':
    main()

### FIXED CODE ###

from sys import stdin, stdout
def readLine_int_list():return list(map(int, stdin.readline().split()))

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
        
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x==y: return
    if rank[x]<rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x]+=1
            
def same(x,y):
    return find(x)==find(y)
    
n,m = readLine_int_list()
v = n + m
par = [i for i in range(0, v)]
rank = [1] * v
def main():
    for i in range(n):
        for ii in readLine_int_list()[1:]:
           unite(i, ii-1+n)
           
    r = "YES"
    for i in range(0, n):
        if find(i) != find(0):
            r = "NO"
    print(r)
    
if __name__ == '__main__':
    main()
### BUGGY CODE ###

from collections import defaultdict

n, m = map(int, input().split())

cor = pen = temp_pen = 0
temp_p = "0"
pen_dic=defaultdict(lambda: 0)
ac=set()

for _ in range(m):
  p, s = input().split()
  if p in set(ac):
    continue

  t = pen_dic[p]
  if s == "AC":
    ac.add(p)
  else:
    pen_dic.update({p:t + 1})
print(len(set(ac)), sum(pen_dic[str(i)] for i in list(ac)))

### FIXED CODE ###

import sys
from collections import defaultdict

n, m = map(int, input().split())

wa=defaultdict(lambda: 0)
already=set()
ac = 0
for line in sys.stdin:
  p, s = line.split()
  p = int(p)
  if p in already:
    continue

  if s == "AC":
    ac+=1
    already.add(p)
  else:
    wa[p] += 1

print(ac, sum(wa[i] for i in already))
### BUGGY CODE ###

S = input()
N = len(S)

ans = 0
num = []
cnt = [0] * 2019
for i in range(N):
    num.append(int(S[i:]))
    cnt[num[i] % 2019] += 1
for i in range(N):
    ans += cnt[i] * (cnt[i] + 1) // 2

print(ans)


### FIXED CODE ###

S = list(input())
N = len(S)

ans = 0
cnt = [0] * 2019
a = 1
s = 0

for i in range(N)[::-1]:
    s = int(S[i]) * a + s
    a *= 10
    a = a % 2019
    cnt[s % 2019] += 1
    s = s % 2019

ans += cnt[0]
for i in range(2019):
    ans += cnt[i] * (cnt[i] - 1) // 2
print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
lst=list(map(int,input().split()))
for i in range(m):
    x=lst.index(max(lst))
    lst[x]=int(lst[x]/2)
print(sum(lst))

### FIXED CODE ###

import heapq
import math
n,m=map(int,input().split())
a=list(map(lambda x:int(x)*(-1),input().split()))
heapq.heapify(a)
for i in range(m):
    heapq.heappush(a,math.ceil(heapq.heappop(a)/2))
   
    
    
print(-1*sum(a))
### BUGGY CODE ###

from collections import Counter

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

ctr = Counter(sum(X, []))
if all(v % 2 == 0 for v in ctr.values()):
    print("YES")
else:
    print("NO")


### FIXED CODE ###

from collections import Counter

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

ctr = Counter()
for a, b in X:
    ctr[a] += 1
    ctr[b] += 1

if all(v % 2 == 0 for v in ctr.values()):
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

N, M = map(int, raw_input().split())
a = [map(int, raw_input().split()) for _ in range(M)]

flag = False

for i in range(M):
    if a[i][0] == 1:
        for j in range(M):
            if i != j and a[i][1] in a[j] and N in a[j]:
                print('POSSIBLE')
                flag = True
                break
    elif a[i][1] == 1:
        for j in range(M):
            if i != j and a[i][0] in a[j] and N in a[j]:
                print('POSSIBLE')
                flag = True
                break
if not flag:
    print('IMPOSSIBLE')

### FIXED CODE ###

N, M = map(int, raw_input().split())
a = []
b = []
for i in range(M):
    temp = map(int, raw_input().split())
    if 1 == temp[0]:
        a.append(temp[1])
    elif N == temp[1]:
        b.append(temp[0])

if any(list(set(a) & set(b))):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
### BUGGY CODE ###

from sys import stdin
N = int(stdin.readline().rstrip())
print(0)
s = input()
cnt = 0
N = (N-1)//2
if s == "Male":
    while True:
        cnt += 1
        print(N)
        s = input()
        if s == "Male" and N % 2 == 0:
            N += max(1,(L-N)//2)
        elif s == "Female" and N % 2 != 0:
            N += max(1,(L-N)//2)
        elif s == "Vacant":
            break
        else:
            N -= max((N//2),1)
        if cnt > 25:
            break
        
elif s == "Female":
    while True:
        cnt += 1
        print(N)
        s = input()
        if s == "Female" and N % 2 == 0:
            N += max(1,(L-N)//2)
        elif s == "Male" and N % 2 != 0:
            N += max(1,(L-N)//2)
        elif s == "Vacant":
            break
        else:
            N -= max((N//2)

### FIXED CODE ###

from sys import stdin
N = int(stdin.readline().rstrip())
r = N-1
l = 0
print(0)
s = input()
cnt = 0
mid = (r+l)//2
if s == "Male":
    for _ in range(25):
        print(mid)
        s = input()
        if s == "Male" and mid % 2 == 0:
            l += max((r-l)//2,1)
        elif s == "Female" and mid % 2 != 0:
            l += max((r-l)//2,1)
        elif s == "Vacant":
            break
        else:
            r -= max((r-l)//2,1)
        mid = (r+l)//2
        
elif s == "Female":
    for _ in range(25):
        print(mid)
        s = input()
        if s == "Female" and mid % 2 == 0:
            l += max((r-l)//2,1)
        elif s == "Male" and mid % 2 != 0:
            l += max((r-l)//2,1)
        elif s == "Vacant":
            break
        else:
            r -= max((r-l)//2,1)
        mid = (r+l)//2
### BUGGY CODE ###

a,b, x = map(int, input().split())
count =0
for i in range(a,b+1):
    if( i%x  ==0):
        count +=1
print(count)

### FIXED CODE ###

a,b,x = map(int, input().split())
#切り捨て
a_num = (a-1) //x
b_num = b // x

ans =b_num-a_num
print(ans)
### BUGGY CODE ###

def eratosthenes(N):
    is_prime = {i: True for i in range(2, N+1)}
    primes = []
    for i in range(2, N+1):
        if is_prime[i]:
            primes.append(i)
            if i**2 <= N:
                for j in range(i**2, N+1, i):
                    is_prime[j] = False
    return primes


def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if b == 0:
        return a
    return gcd(b, a % b)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    allgcd = A[0]
    primes = eratosthenes(1000)
    divs = set()
    pairwise_coprime = True
    for a in A:
        allgcd = gcd(allgcd, a)
        if not pairwise_coprime:
            continue
        for p in primes:
            if a % p == 0:
                if p in divs:
                    pairwise_coprime = False
                while a % p == 0:
                    a //= p
                divs.add(p)
        if a != 1:
            if a in divs:
                pairwise_coprime = False
            divs.add(a)
    if pairwise_coprime:
        print("pairwise coprime")
    else:
        if allgcd == 1:
            print("setwise coprime")
        else:
            print("not coprime")


if __name__ == "__main__":
    main()


### FIXED CODE ###

def eratosthenes(N):
    is_prime = {i: True for i in range(2, N+1)}
    primes = []
    for i in range(2, N+1):
        if is_prime[i]:
            primes.append(i)
            if i**2 <= N:
                for j in range(i**2, N+1, i):
                    is_prime[j] = False
    return primes


def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if b == 0:
        return a
    return gcd(b, a % b)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    allgcd = A[0]
    primes = eratosthenes(1000)
    divs = set()
    pairwise_coprime = True
    for a in A:
        if allgcd > 1:
            allgcd = gcd(allgcd, a)
        if not pairwise_coprime:
            continue
        for p in primes:
            if a % p == 0:
                if p in divs:
                    pairwise_coprime = False
                while a % p == 0:
                    a //= p
                divs.add(p)
            if a == 1:
                break
        if a != 1:
            if a in divs:
                pairwise_coprime = False
            divs.add(a)
    if pairwise_coprime:
        print("pairwise coprime")
    else:
        if allgcd == 1:
            print("setwise coprime")
        else:
            print("not coprime")


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

import numpy as np

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]
s = np.sum(dist)
result = []
for start_index in min_index:
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))


### FIXED CODE ###

import numpy as np

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

#min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n, W = map(int, input().split())
dp = [0]*(W+1)
for _ in range(n):
  w, v = map(int, input().split())
  for j in range(W, w-1, -1):
    dp[j] = max(dp[j-w] + v, dp[j])
print(dp[W])

### FIXED CODE ###

n, w = map(int, input().split())
l = [ list(map(int, input().split())) for _ in range(n) ]
dp = [ [0]*(w+1) for _ in range(n+1) ]
for i in range(1, n+1):
  for j in range(w+1):
    if j < l[i-1][0]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = max(dp[i-1][j-l[i-1][0]] + l[i-1][1], dp[i-1][j])
print(dp[n][w])
### BUGGY CODE ###

a,b,q = map(int,input().split())
s = []
t = []
for i in range(a):
    k = int(input())
    s.append(k)
for i in range(b):
    k = int(input())
    t.append(k)


import bisect
import itertools
#sは配列,xは知りたい値
inf = 10**15
s = [-inf]+s+[inf]
t = [-inf]+t+[inf]
#最初と最後にinfをつけておく。

for i in range(q):
    x = int(input())
    asa = bisect.bisect_left(s,x)-1
    bs = bisect.bisect_left(t,x)-1
    s1,s2 = s[asa],s[asa+1]
    t1,t2 = t[bs],t[bs+1]
    ans = min(x-min(s1,t1),max(s2,t2)-x,
    s2-t1+min(s2-x,x-t1),
    t2-s1+min(t2-x,x-s1))
    print(ans)


### FIXED CODE ###

a,b,q = map(int,input().split())
s = []
t = []
for i in range(a):
    k = int(input())
    s.append(k)
for i in range(b):
    k = int(input())
    t.append(k)


import bisect
import itertools
#sは配列,xは知りたい値
inf = 10**15
s = [-inf]+s+[inf]
t = [-inf]+t+[inf]
#最初と最後にinfをつけておく。

for i in range(q):
    x = int(input())
    asa = bisect.bisect_left(s,x)-1
    bs = bisect.bisect_left(t,x)-1
    s1,s2 = s[asa],s[asa+1]
    t1,t2 = t[bs],t[bs+1]
    ans = min(x-min(s1,t1),max(s2,t2)-x,
    s2-t1+min(s2-x,x-t1),
    t2-s1+min(t2-x,x-s1))
    print(ans)
### BUGGY CODE ###

n, s = [int(i) for i in  input().split()]
weights = []
values = []

for _ in range(n):
    w, v = [int(i) for i in input().split()]
    weights.append(w)
    values.append(v)

def knapsack(weights, values, limit):
    n = len(weights)
    # f = [[0 for _ in range(limit+1)] for _ in range(n)]

    prev = [0 for _ in range(limit+1)]

    if weights[0] <= limit:
        for w in range(weights[0], limit+1):
            prev[w] = values[0]

    for i in range(1, n):
        # print prev
        current = [0] * (limit+1)
        for w in range(1, limit+1):
            current[w] = max(prev[w], current[w])
            if weights[i] <= w:
                current[w] = max(current[w], prev[w-weights[i]] + values[i])
        prev = list(current)

    return prev[-1]

# weights = [10, 20, 30]
# values = [60, 100, 120]
print(knapsack(weights, values, s))

### FIXED CODE ###

n, s = [int(i) for i in  raw_input().split()]
weights = []
values = []

for _ in xrange(n):
    w, v = [int(i) for i in raw_input().split()]
    weights.append(w)
    values.append(v)

def knapsack(weights, values, limit):
    n = len(weights)
    # f = [[0 for _ in xrange(limit+1)] for _ in xrange(n)]

    prev = [0 for _ in xrange(limit+1)]

    if weights[0] <= limit:
        for w in xrange(weights[0], limit+1):
            prev[w] = values[0]

    for i in xrange(1, n):
        # print prev
        current = [0] * (limit+1)
        for w in xrange(1, limit+1):
            current[w] = max(prev[w], current[w])
            if weights[i] <= w:
                current[w] = max(current[w], prev[w-weights[i]] + values[i])
        prev = list(current)

    return prev[-1]

# weights = [10, 20, 30]
# values = [60, 100, 120]
print(knapsack(weights, values, s))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect
import numpy as np

def ln():
    return map(int, input().split())

N,M = ln()
AA = list(ln())
A = np.array(AA)
A = A%M

SUM = np.zeros(N+1,dtype = int)
for i in range(N):
    SUM[i+1] = SUM[i] + A[i]
SUM %= M
ans = 0

for i in range(N):
    ans += np.sum(SUM[i+1:] == SUM[i])

print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect
import numpy as np

def ln():
    return map(int, input().split())

N,M = ln()

AA = list(ln())
A = np.array(AA)

SUM = np.zeros(N+1,dtype = int)
for i in range(N):
    SUM[i+1] = (SUM[i] + A[i]) % M

ans = 0
dic = dict()
# for i in range(N):
#     dic[SUM[i]] = 1
for i in range(N+1):
    if SUM[i] in dic:
        dic[SUM[i]] += 1
    else:
        dic[SUM[i]] = 1
    ans += dic[SUM[i]] -1


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

N,K,C = map(int,input().split())
S = str(input())
works1 = 0
works2 = 0
l1 = []
l2 = []
i = 0
while i < N and works1 < K:
  if S[i] == "o":
    l2.append(i+1)
    i = i+C+1
    works1 += 1
  else:
    i = i+1
i = N-1
while i > -1 and works2 < K:
    if S[i] == "o":
      l2.append(i+1)
      i = i-C-1
      works2 = works2+1
    else:
      i = i-1

l1.extend(l2)
l = [x for x in set(l1) if l1.count(x) > 1]
for i in range(len(l)):
  print(l[i])

### FIXED CODE ###

N,K,C = map(int,input().split())
S = str(input())
works1 = 0
works2 = 0
l1 = []
l2 = []
i = 0
while i < N and works1 < K:
  if S[i] == "o":
    l1.append(i+1)
    i = i+C+1
    works1 += 1
  else:
    i = i+1
i = N-1
while i > -1 and works2 < K:
    if S[i] == "o":
      l2.append(i+1)
      i = i-C-1
      works2 = works2+1
    else:
      i = i-1

for i in range(len(l1)):
  if l1[i] == l2[-i-1] :
    print(l1[i])
### BUGGY CODE ###

n=int(input())
a=list(map(int, input().split()))
cnt=0

while any(a[i]%2==0 for i in range(n)):
    m=0
    for j in range(n):
        if a[j]%2==0:
            if a[j]>m:
                m=a[j]
                index=j

    for k in range(n):
        a[k]*=3
    
    a[index]=a[index]//6
    cnt+=1

print(cnt)

### FIXED CODE ###

n=int(input())
a=list(map(int, input().split()))
even_num=[0]*n

for i in range(n):
    cnt=0
    if a[i]%2==0:
        while a[i]%2==0:
            a[i]=a[i]/2
            cnt+=1

    even_num[i]=cnt

ans=sum(even_num)
print(ans)
### BUGGY CODE ###

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

count = 0
pre = p[0]
for i, now in enumerate(p):
    if now <=pre:
         count+=1
    pre = min(p[:i+1])
print(count)


### FIXED CODE ###

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

count = 0
pre = p[0]
for now in p:
    if now <=pre:
        count+=1
        pre = now
print(count)
### BUGGY CODE ###

N,K = map(int,input().split())
l = []
for i in range(K):
    L,R = map(int,input().split())
    l.append([L,R])
l.sort()

a = [0 for i in range(N+1)]
a[1] = 1
b = [0 for i in range(N+1)]
b[1] = 1
for i in range(2,N+1):
    for j in range(K):
        if l[j][0] < i:
            a[i] += b[i-l[j][0]]-b[max(0,i-l[j][1]-1)]
        else:
            break
    b[i] = b[i-1]+a[i]
mod = 998244353
print(a[N]%mod)

### FIXED CODE ###

N,K = map(int,input().split())
l = []
mod = 998244353
for i in range(K):
    L,R = map(int,input().split())
    l.append([L,R])
l.sort()

a = [0 for i in range(N+1)]
a[1] = 1
b = [0 for i in range(N+1)]
b[1] = 1
for i in range(2,N+1):
    for j in range(K):
        if l[j][0] < i:
            a[i] += (b[i-l[j][0]]-b[max(0,i-l[j][1]-1)])%mod
        else:
            break
    b[i] = (b[i-1]+a[i])%mod
mod = 998244353
print(a[N]%mod)
### BUGGY CODE ###

import sys
from bisect import bisect_left

INDATA = sys.stdin.read().split('\n')
idx = -1
def input():
    global idx
    idx += 1
    return INDATA[idx]

n, m, q = map(int, input().split())
LR = [0]*m
for i in range(m):
    LR[i] = list(map(int, input().split()))
P = [0]*q
Q = [0]*q
for i in range(q):
    P[i], Q[i] = map(int, input().split())
 
LR.sort(key=lambda x: (x[0], x[1]))
L = list(map(lambda x: x[0], LR))
#print(LR)
 
C = [[0]*(n+1) for _ in range(n+1)]
for i in range(1,n+1):
    l = bisect_left(L, i)
    r = bisect_left(L, i+1)-1
    R2 = list(map(lambda x: x[1], LR[l:r+1]))
    #print(l, r, R2)
    for j in range(i,n+1):
        C[i][j] = bisect_left(R2, j+1)
    #print(i, C[i])
 
for i in range(q):
    ans = 0
    for j in range(P[i], Q[i]+1):
        ans += C[j][Q[i]]
    print(ans)


### FIXED CODE ###

from bisect import bisect_left
 
n, m, q = map(int, input().split())
LR = [0]*m
for i in range(m):
    LR[i] = list(map(int, input().split()))
P = [0]*q
Q = [0]*q
for i in range(q):
    P[i], Q[i] = map(int, input().split())
 
LR.sort(key=lambda x: (x[0], x[1]))
L = list(map(lambda x: x[0], LR))
#print(LR)
 
C = [[0]*(n+1) for _ in range(n+1)]
for i in range(1,n+1):
    l = bisect_left(L, i)
    r = bisect_left(L, i+1)-1
    R2 = list(map(lambda x: x[1], LR[l:r+1]))
    #print(l, r, R2)
    for j in range(i,n+1):
        C[i][j] = bisect_left(R2, j+1)
    #print(i, C[i])
 
for i in range(q):
    ans = 0
    for j in range(P[i], Q[i]+1):
        ans += C[j][Q[i]]
    print(ans)
### BUGGY CODE ###

from sys import exit

def my_sort(L, p, q):
    C = [[i, -1] for i in range(q - p + 1)]
    if p < q:
        mid = (p + q) // 2
        A = my_sort(L, p, mid)
        B = my_sort(L, mid + 1, q)
        i = 0
        j = 0
        k = 0
        while k < q - p + 1:
            if i >= mid - p + 1:
                C[k] = B[j]
                j += 1
            elif j >= q - mid:
                C[k] = A[i]
                i += 1
            else:
                if A[i][1] <= B[j][1]:
                    C[k] = A[i]
                    i += 1
                else:
                    C[k] = B[j]
                    j += 1
            k += 1
        return C
    else:
        return [L[p]]


n = int(input())
task = []
time_total = [0 for i in range(n)]
for i in range(n):
    task.append([int(i) for i in input().split()])
task = my_sort(task, 0, n - 1)
time_total[0] = task[0][0]
for i in range(n - 1):
    time_total[i + 1] = time_total[i] + task[i + 1][0]
while i < n:
    if time_total[i] > task[i][1]:
        print("No")
        exit()
    i += 1
print("Yes")

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
task = []
time_total = [-1 for i in range(n)]
for i in range(n):
    task.append([int(i) for i in input().split()])
task_sorted = sorted(task, key = lambda x:x[1])
time_total[0] = task_sorted[0][0]
for i in range(n - 1):
    time_total[i + 1] = time_total[i] + task_sorted[i + 1][0]
for i in range(n):
    if time_total[i] > task_sorted[i][1]:
        print("No")
        sys.exit()
print("Yes")
### BUGGY CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

N, Q = map(int, input().split())
link = [[] for _ in range(N)]
a = [0] * (N-1)
b = [0] * (N-1)
for i in range(N-1):
    a[i], b[i] = map(int, input().split())

cost = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    cost[p-1] += x

for i in range(N-1):
    link[a[i]-1].append([b[i]-1, cost[i]])
    link[b[i]-1].append([a[i]-1, cost[i]])

nums = [0] * N
def dfs(v, c, p):
    for nv in link[v]:
        if nv[0] == p:
            continue
        nums[nv[0]] += c + cost[nv[0]]
        dfs(nv[0], c + cost[nv[0]], v)
    return

nums[0] = cost[0]
dfs(0, cost[0], -1)

print(*nums)


### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def dfs(v, p):
    for u in link[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

N, Q = map(int, input().split())
link = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    link[a-1].append(b-1)
    link[b-1].append(a-1)

ans = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x

dfs(0, -1)

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

k, s = map(int, input().split())
count=0
if k < s/3:
  print(0)
  quit()
for a in range(0, k+1):
  if a == s:
    print(count+1)
    quit()
  for b in range(0, k+1):
    if a+b == s:
      count+=1
      break
    elif a+b > s:
      break
    for c in range(0, k+1):
      if a+b+c == s:
        count+=1
      elif a+b+c > s:
        break
print(count)

### FIXED CODE ###

k, s = map(int, input().split())
count=0
for a in range(0, k+1):
  for b in range(0, k+1):
    if s-a-b >= 0 and s-a-b <= k:
      count+=1
print(count)
### BUGGY CODE ###

N, M = map(int, input().split())
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    work[i][1], work[i][0] = 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]
    while day >= 0:
        if schedule[day] == 0:
            schedule[day] += 1
            ans += work_money[N - 1 - j][0]
            break
        day -= 1
print(ans)


### FIXED CODE ###

import heapq
N, M = map(int, input().split())
work = [0] * N
for i in range(N):
    work[i] = [0] * 2
    a, b = 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)