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

H,W = list(map(int, input().split()))
S = list()
for i in range(H):
  S.append(input())

A = [[0] * W for i in range(H)]


for i in range(H):
  work = 0
  fj = 0
  for j in range(W+1):
    if j == W or S[i][j] == '#':
      for k in range(fj,j):
        A[i][k] += work
      work = 0
      fj = j + 1
    else:
      work += 1
      
for i in range(W):
  work = 0
  fj = 0
  for j in range(H+1):
    if j == H or S[j][i] == '#':
      for k in range(fj,j):
        A[k][i] += work
      work = 0
      fj = j + 1
    else:
      work += 1

ans = 0
for i in range(H):
  #print(A[i])
  ans = max(ans, max(A[i]))
  
print(ans-1)

### FIXED CODE ###

H, W = list(map(int, input().split()))
S = []
for i in range(H):
  S.append(input())
  
L = [[0] * W for i in range(H)]
R = [[0] * W for i in range(H)]
U = [[0] * W for i in range(H)]
D = [[0] * W for i in range(H)]

for i in range(H):
  work = 0
  for j in range(W):
    if S[i][j] == '#':
      work = 0
    else:
      work += 1
    R[i][j] = work
    
for i in range(H):
  work = 0
  for j in range(W-1,-1,-1):
    if S[i][j] == '#':
      work = 0
    else:
      work += 1
    L[i][j] = work
    
for i in range(W):
  work = 0
  for j in range(H):
    if S[j][i] == '#':
      work = 0
    else:
      work += 1
    D[j][i] = work
    
for i in range(W):
  work = 0
  for j in range(H-1,-1,-1):
    if S[j][i] == '#':
      work = 0
    else:
      work += 1
    U[j][i] = work
    
ans = 0
for i in range(H):
  for j in range(W):
    ans = max(ans, U[i][j] + D[i][j] + L[i][j] + R[i][j] - 3)
    
print(ans)
### BUGGY CODE ###

N = int(input())
s = [input().split() for i in range(N)]

count = 0
s = [''.join(sorted(str(i))) for i in s]
s_set = list(set(s))
for i in range(len(s_set)):
    s_count = s.count(s_set[i])
    count += (s_count * (s_count - 1)) // 2
print(count)


### FIXED CODE ###

N = int(input())
s = [''.join(sorted(input())) for i in range(N)]
s.sort()
count = 0
num = 1
for i in range(N-1):
    if s[i] == s[i+1]:
        num += 1
    else:
        count += num * (num - 1) // 2
        num = 1
print(count + num * (num - 1) // 2)
### BUGGY CODE ###

def BF(N, edges, start):
    D = {}
    P = {}
    for i in range(N):
        P[i] = None
        if i == start:
            D[i] = 0
        else:
            D[i] = float("inf")

    for i in range(N-1):
        for edge in edges:
            a, b, c = edge
            if D[b] > D[a] + c:
                D[b] = D[a] + c
                P[b] = a

    ret = (D, P)

    Nega = {}
    for i in range(N):
        Nega[i] = False
    
    for i in range(N):
        for edge in edges:
            a, b, c = edge
            if Nega[a] == True:
                Nega[b] = True
            if D[b] > D[a] + c:
                D[b] = D[a] + c
                P[b] = a
                Nega[b] = True
        
    return (D, P, Nega)

N, M, P = (int(i) for i in input().split())
edges = []
for i in range(M):
    A, B, C = (int(i) for i in input().split())
    edges.append((A-1, B-1, P-C))
D, P, Nega = BF(N, edges, 0)
if Nega[N-1] == True:
    print(-1)
else:
    if D[N-1] <= 0:
        print(-D[N-1])
    else:
        print(0)

### FIXED CODE ###

def BF(v, start, edges):
    INF = 10**18
    nega = False
    d = [INF] * v
    d[start] = 0
    for _ in range(v):
        f = False
        for a, b, c in edges:
            cost = d[a] + c
            if cost < d[b]:
                d[b] = cost
                f = True
        if not f:
            break
    else:
        nega = True
    return d, nega

from collections import deque
def BFS(N, graph, start):
    d = [-1 for i in range(N)]
    Q = deque([])
    Q.append(start)
    d[start] = 0
    while Q:
        v = Q.popleft()
        for u in graph[v]:
            if d[u] == -1:
                d[u] = d[v] + 1
                Q.append(u)
    return d

N, M, P = (int(i) for i in input().split())
graph = [[] for i in range(N)]
graphr = [[] for i in range(N)]
edges = []
for i in range(M):
    A, B, C = (int(i) for i in input().split())
    graph[A-1].append(B-1)
    graphr[B-1].append(A-1)
    edges.append((A-1, B-1, P-C))
d_start = BFS(N, graph, 0)
d_end = BFS(N, graphr, N-1)
nodes_start = [i for i in range(N) if d_start[i] >= 0]
nodes_end = [i for i in range(N) if d_end[i] >= 0]
new_edges = []
for A, B, C in edges:
    if A in nodes_start and B in nodes_start and A in nodes_end and B in nodes_end:
        new_edges.append((A, B, C))
d, Nega = BF(N, 0, new_edges)
if Nega == True:
    print(-1)
else:
    print(max(-d[N-1], 0))
### BUGGY CODE ###

import sys
from functools import lru_cache

@lru_cache(maxsize=None)
def isprime(x):
    if x == 1:
        return False
    i = 2
    while i * i <= x:
        if x % i == 0:
            return False
        i += 1
    return True

Q = int(input())
for _ in range(Q):
    l, r = map(int, sys.stdin.readline().split())
    ans = 0
    for x in range(l, r + 1, 2):
        if isprime(x) and isprime((x + 1) // 2):
            ans += 1
    print(ans)

### FIXED CODE ###

import sys
from functools import lru_cache
Q = int(input())
LR = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]

@lru_cache(maxsize=None)
def isprime(x):
    if x == 1:
        return False
    i = 2
    while i * i <= x:
        if x % i == 0:
            return False
        i += 1
    return True

s = [0] * (10 ** 5 + 1)
for x in range(1, len(s)):
    s[x] = s[x-1]
    if isprime(x) and isprime((x + 1) // 2):
        s[x] += 1

for l, r in LR:
    ans = s[r] - s[l-1]
    print(ans)
### BUGGY CODE ###

import math
k=int(input())
sums=0
for i in range(k):
    a=i+1
    for i in range(k):
        b=i+1
        for i in range(k):
            c=i+1
            d=math.gcd(a,b)
            sums=sums+math.gcd(c,d)
print(sums)

### FIXED CODE ###

import math
k=int(input())
sums=0
for i in range(k):
    a=i+1
    for i in range(k):
        b=i+1
        d=math.gcd(a,b)
        for i in range(k):
            c=i+1
            
            sums=sums+math.gcd(c,d)
print(sums)
### BUGGY CODE ###

from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	st=[]
	s=f4()
	n1=0
	ans=0
	for i in s:
		n1+=1
		st.append(i)
		#print st
		if n1>=3 and st[n1-3:]==['A','B','C']:
			#print 123
			s1=[]
			n2=n1-4
			ans+=1
			st[n1-1]="A"
			while n2>-1 and st[n2]=="A":
				ans+=1
				st[n2+2]="A"
				n2-=1
			st[n2+1],st[n2+2]="B","C"
			#print st,12
	print ans


if __name__ == '__main__':
	main()


### FIXED CODE ###

from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	
	s=f4()
	n1=0
	ans=0
	i=0
	while i<len(s)-1:
		if s[i]=="A":
			n1+=1
		elif s[i:i+2]=="BC":
			ans+=n1
			i+=1
		else:
			n1=0
		i+=1
	print ans

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

import numpy as np
n=int(input())
a=list(map(int,input().split()))
a=np.array(a)
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    a[a==b]=c
    print(sum(a))

### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
q=int(input())
s=sum(a)
d=Counter(a)
for i in range(q):
    b,c=map(int,input().split())
    if b in d:
        l=d[b]
        d[b]=0
        d[c]+=l
        s+=(c-b)*l
        print(s)
    else:print(s)
### BUGGY CODE ###

N = int(input())

ans = 1
for i in range(1, N+1):
    ans *= i
print(ans % (10 ** 9 + 7))


### FIXED CODE ###

N = int(input())

ans = 1
for i in range(1, N+1):
    ans = i * ans % (10 ** 9 + 7)
print(ans)
### BUGGY CODE ###

n=int(input())
a= list((map(int, input().split()))) 
b=[0]
c=[]
 
for i in range(n):
  if a[0]-b[-1]==1:
    b.append(a.pop(0))
  else:
    c.append(a.pop(0))
if n-len(b)+1==n:
  print(-1)
else:
  print(n-len(b)+1)

### FIXED CODE ###

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

count=1
for i in range(n):
  if a[i]==count:
    count+=1
if count==1:
  print(-1)
else:
  print(n-count+1)
### BUGGY CODE ###

h, n = map(int, input().split())
l = []
for i in range(n):
  a, b = map(int, input().split())
  l.append([a, b])

dp = [10 ** 9 for i in range(h+1)]
dp[0] = 0

for i in range(h+1):
  for j in range(n):
    y = max(i-l[j][0], 0)
    if dp[i] > l[j][1] + dp[y]:
      dp[i] = l[j][1] + dp[y]


print(dp[h])


### FIXED CODE ###

def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print(min(dp[H:]))


main()
### BUGGY CODE ###

L = input()
INF = 10**9+7
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x
count = L.count('1')
ans = pow_k(2,count)
for i in range(len(L)-1,-1,-1):
  if L[i]=='1':
    count -= 1
    m = pow_k(3,len(L)-i-1)*pow_k(2,count)
    ans += m
    ans %= INF
print(ans)

### FIXED CODE ###

L = input()
MOD = 10**9+7

pow_3=[1]
for _ in range(2*10**5):
  pow_3.append((pow_3[-1]*3)%MOD)
  
pow_2=[1]
for _ in range(2*10**5):
  pow_2.append((pow_2[-1]*2)%MOD)
  
count = L.count('1')
ans = pow_2[count]

for i in range(len(L)-1,-1,-1):
  if L[i]=='1':
    count -= 1
    m = pow_3[len(L)-i-1]*pow_2[count]
    ans += m
    ans %= MOD
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from collections import deque

H,W = list(map(int, input().rstrip().split()))
A_list = [list(input().rstrip()) for i in range(H)]
#-----

def check(matrix):
    for i in range(len(matrix)):
        matrix[i] = ["#"] + matrix[i][:] + ["#"]
    
    matrix = [["#"]*(W+2)] + matrix[:] + [["#"]*(W+2)]
    
    
    # queue = [ (row, column) ]
    # deque = [ (int, int   ) ]
    queue = deque()
    
    for i in range(1,H+1):
        for j in range(1, W+1):
            if matrix[i][j] == "#":
                queue.append( (i, j) )
    
    
    delta = [(1,0),(-1,0),(0,1),(0,-1)]
    step = -1
    
    while queue:
        step += 1
        
        for _ in range(len(queue)):
            r,c = queue.popleft()
            
            for d in delta:
                next_r = r + d[0]
                next_c = c + d[1]
                
                if matrix[next_r][next_c] == ".":
                    matrix[next_r][next_c] = "#"
                    queue.append( (next_r, next_c) )


    return step

#-----

ans = check(A_list)
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import deque

H,W = list(map(int, input().rstrip().split()))
A_list = [list(input().rstrip()) for i in range(H)]
#-----

def check(matrix):
    for i in range(len(matrix)):
        matrix[i] = ["#"] + matrix[i][:] + ["#"]
    
    matrix = [["#"]*(W+2)] + matrix[:] + [["#"]*(W+2)]
    
    
    # queue = [ (row, column) ]
    # deque = [ (int, int   ) ]
    queue = deque()
    
    for i in range(1,H+1):
        for j in range(1, W+1):
            if matrix[i][j] == "#":
                queue.append( (i, j) )
    
    
    delta = [(1,0),(-1,0),(0,1),(0,-1)]
    step = -1
    
    while queue:
        step += 1
        
        for _ in range(len(queue)):
            r,c = queue.popleft()
            
            for d in delta:
                next_r = r + d[0]
                next_c = c + d[1]
                
                if matrix[next_r][next_c] == ".":
                    matrix[next_r][next_c] = "#"
                    queue.append( (next_r, next_c) )


    return step

#-----

ans = check(A_list)
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    d={}
    c=0
    edge1=[[] for _ in range(N*(N-1)//2)]
    edge2=[[] for _ in range(N*(N-1)//2)]
    for i in range(N):
        l=list(map(lambda x: int(x)-1,input().split()))
        prev=-1
        for x in l:
            if min(i,x)*10**3+max(i,x) in d:
                if prev!=-1:
                    edge1[prev].append(d[min(i,x)*10**3+max(i,x)])
                    edge2[d[min(i,x)*10**3+max(i,x)]].append(prev)
            else:
                d[min(i,x)*10**3+max(i,x)]=c
                if prev!=-1:
                    edge1[prev].append(c)
                    edge2[c].append(prev)
                c+=1
            prev=d[min(i,x)*10**3+max(i,x)]
    stack=[]
    days=[-1]*(N*(N-1)//2)
    for i,l in enumerate(edge2):
        if not l:
            stack.append(i)
            days[i]=1
    if stack:
        for _ in range(2*10**7):
            v=stack.pop()
            for x in edge1[v]:
                if days[x]<days[v]+1:
                    days[x]=days[v]+1
                    stack.append(x)
            if not stack:
                print(max(days))
                exit()
    print(-1)
        

    

        
if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    A=[[] for _ in range(N)]
    for i in range(N):
        A[i]=deque(list(map(lambda x: int(x)-1,input().split())))
    days=1
    c=0
    pre=[]
    tmp=[]
    for i in range(N):
        if A[i] and A[A[i][0]][0]==i:
            tmp.append(i)
            tmp.append(A[i][0])
    tmp=list(set(tmp))
    for i in tmp:
        A[i].popleft()
        c+=1
    pre=tmp
    if c<N*(N-1):
        while c<N*(N-1):
            tmp=[]
            for i in pre:
                if A[i] and A[A[i][0]][0]==i:
                    tmp.append(i)
                    tmp.append(A[i][0])
            tmp=list(set(tmp))
            for i in tmp:
                A[i].popleft()
                c+=1
            pre=tmp
            days+=1
            if days>N*(N-1)//2:
                print(-1)
                exit()
    print(days)
                
            
        
    
        
        

    

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

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

Ans = 0
for i in range(N):
    xorA = A[i]
    orA = A[i]
    plusA = A[i]
    for j in range(i+1,N):
        if xorA ^ A[j] == plusA + A[j]:
            Ans += 1
        if orA & A[j] > 0:
            i=j
            break
        xorA ^= A[j]
        plusA += A[j]
        orA |= A[j]

print(Ans+N)


### FIXED CODE ###

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

Ans = 0
j,xorA = 0,0
for i in range(N):
    while j < N and xorA&A[j] == 0:
        xorA |= A[j]
        j += 1
    Ans += j-i
    xorA ^= A[i]

print(Ans)
### BUGGY CODE ###

from heapq import heappush, heappop

INF =  10000000000

def dijkstra(r, v, dic):
    dp = []
    q = []
    dp = [INF] * v
    dp[r] = 0
    heappush(q, (0, r))
    while q:
        cost, vtx = heappop(q)
        if dp[vtx] < cost:
            continue
        if vtx not in dic:
            continue
        for vtx1, cost1 in dic[vtx]:
            if cost + cost1 < dp[vtx1]:
                dp[vtx1] = cost + cost1
                heappush(q, (cost + cost1, vtx1))
    return dp

dp = []
line = input()
n = int(line)
edge = {}
for _ in range(0, n - 1):
    line = input()
    s, t, d = list(map(int, line.split()))
    if s not in edge:
        edge[s] = [[t, d]]
    else:
        edge[s] += [[t, d]]
    if t not in edge:
        edge[t] = [[s, d]]
    else:
        edge[t] += [[s, d]]
dp = dijkstra(0, n, edge)
i = [j for j, x in enumerate(dp) if x == max(dp)]
dp = dijkstra(i[0], n, edge)
diam = max(dp[i] if dp[i] != INF else 0 for i in range(0, n))
print(diam)


### FIXED CODE ###

def solve(prev, r, dic):
    d = 0
    t = r
    stack = []
    stack += [[prev, r, d]]
    while stack:
        prev, r, d1 = stack.pop(-1)
        if r in dic:
            for vtx, cost in dic[r]:
                if vtx == prev:
                    continue
                stack += [[r, vtx, d1 + cost]]
                if d < d1 + cost:
                    d = d1 + cost
                    t = vtx
    return d, t

line = input()
n = int(line)
edge = {}
for _ in range(0, n - 1):
    line = input()
    s, t, d = list(map(int, line.split()))
    if s not in edge:
        edge[s] = [[t, d]]
    else:
        edge[s] += [[t, d]]
    if t not in edge:
        edge[t] = [[s, d]]
    else:
        edge[t] += [[s, d]]
r1, t1 = solve(-1, 0, edge)
r2, t2 = solve(-1, t1, edge)
print(r2)
### BUGGY CODE ###

N = int(input())
P = list(map(int, input().split()))
result = [False]*N
count = 0
for i in range(N):
    flag = True
    for j in range(i):
        if not P[i] <= P[j]:
            flag = False
            break
        elif result[i] == True:
            break
    if flag == True:
        result[i] = True
        count += 1


print(count)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split()))
result = [False]*N
count = 0
mn = P[0]
for i in range(N):
    if P[i] <= mn:
        count += 1
        mn = P[i]

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

# coding: utf-8
import fractions


def solve(arg: str) -> str:
    a, b = map(int, arg.split())
    div = [1]

    g = fractions.gcd(a, b)
    n = 2
    while n <= g:
        if g % n == 0:
            div.append(n)
            while g % n == 0:
                g = int(g/n)
        n += 1
        
    return str(len(div))


if __name__ == "__main__":
    print(solve(input()))


### FIXED CODE ###

# coding: utf-8
import fractions


def solve(arg: str) -> str:
    a, b = map(int, arg.split())
    div = [1]

    g = fractions.gcd(a, b)
    n = 2
    while n*n <= g:
        if g % n == 0:
            div.append(n)
            while g % n == 0:
                g = int(g/n)
        n += 1
    if g != 1:
        div.append(g)

    return str(len(div))


if __name__ == "__main__":
    print(solve(input()))
### BUGGY CODE ###

L, R = map(int, input().split())
mod = 2019

for i in range(L, R) :
	for j in range(i + 1, R + 1) :
		if((i * j) % 2019 < mod) :
			mod = (i * j) % 2019
		else :
			pass
	if(mod == 0) :
		break
	else :
		pass
	
print(mod)

### FIXED CODE ###

L, R = map(int, input().split())
mod = 2019

for i in range(L, R) :
	for j in range(i + 1, R + 1) :
		if((i * j) % 2019 < mod) :
			mod = (i * j) % 2019
			x = ((i * j) // 2019) * 2019 + 2019
			if(x > i * R) :
				break
			else :
				pass
		elif(mod == 0) :
			break
		else :
			pass
	if(mod == 0) :
		break
	else :
		pass
	
print(mod)
### BUGGY CODE ###

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

ans_r=0
for p in range(0, N):
  for q in range(0, p):
    if (P[p] > P[q]):
      ans_r += 1
      break
print (N-ans_r)



### FIXED CODE ###

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

ans_r=0
min_num=P[0]
for p in range(0, N):
  if (min_num > P[p]):
    min_num=P[p]
  if (min_num < P[p]):
    ans_r += 1
print (N-ans_r)
### BUGGY CODE ###

from sys import stdin
import fractions


N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

res = []


for i in range(N):
    B = A[:]
    del B[i]
    gcd_num = B[0]
    for j in range(1, N-1):
        gcd_num = fractions.gcd(gcd_num, B[j])
    res.append(gcd_num)
        


print(max(res))


### FIXED CODE ###

from sys import stdin
import fractions

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
# インデックスを直感で使えるように先頭にダミーを入れる
A = [0] + A

"""
L(i) := A_1, A_2, ..., A_i-1 の最大公約数
R(i) := A_i, A_i+1, ..., A_N の最大公約数
Aiを書き換えることにした場合の最大公約数をM_iとすると
M_i = gcd(L(i), R(i+1))

ただし L(0)=0, R(N+1)=0
"""

L = [0 for _ in range(N+1)]
R = [0 for _ in range(N+2)]

for i in range(N):
    L[i+1] = fractions.gcd(L[i], A[i])


for i in range(N, 0, -1):
    R[i] = fractions.gcd(R[i+1], A[i])

M = []

for i in range(1, N+1):
    M_i = fractions.gcd(L[i], R[i+1])
    M.append(M_i) 

print(max(M))
### BUGGY CODE ###

N, K = [int(i) for i in input().split()]
a = list(map(int, input().split()))
dp = []


def mincos(dp, a, i, k):
    if i == 0:
        return 0
    elif i < k:
        mincos = 100000000000
        for j in range(1, i + 1):
            if mincos > dp[i - j] + abs(a[i] - a[i - j]):
                mincos = dp[i - j] + abs(a[i] - a[i - j])
        return mincos
    else:
        mincos = 10000000000
        for j in range(1, k + 1):
            if mincos > dp[i - j] + abs(a[i] - a[i - j]):
                mincos = dp[i - j] + abs(a[i] - a[i - j])
        return mincos


for i in range(N):
    dp.append(mincos(dp, a, i, K))

print(dp[N-1])

### FIXED CODE ###

import numpy as np
N, K = map(int, input().split())
h = np.array(list(map(int, input().split())))

dp = np.array([0] * N)

for i in range(1, N):
    j = max(0, i-K)
    dp[i] = (dp[j:i] + np.abs(h[i] - h[j:i])).min()


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

K, S = map(int, input().split())
count = 0
if K*3 < S:
    print(count)
else:
    for i in range(K, -1, -1):
        X = i
        for j in range(K, -1, -1):
            Y = j
            Z = S - (X + Y)
            if Y > K or Z > K:                
                continue
            elif Z < 0:
                continue
            else:
                count += 1
                #print(X, Y, Z)
print(count)

### FIXED CODE ###

K, S = map(int, input().split())
count = 0
if K*3 < S:
    print(count)
else:
    for i in range(K, -1, -1):
        X = i
        YZ = S - X
        for j in range(K, -1, -1):
            Y = j
            Z = YZ - Y
            if Z > K:
                continue
            elif Z < 0:
                continue
            else:
                count += 1
                #print(X,Y,Z)
print(count)
### BUGGY CODE ###

import sys
import numpy as np


def main():
    x, y, z, k = [int(n) for n in input().split()]
    a = sorted([int(n) for n in input().split()], reverse=True)
    b = sorted([int(n) for n in input().split()], reverse=True)
    c = sorted([int(n) for n in input().split()], reverse=True)
    result = [a[ai] + b[bi] + c[ci] for ai in range(x) for bi in range(y) for ci in range(z) if ai * bi * ci <= k]
    print(*(sorted(result, reverse=True))[:k], sep="\n")


main()


### FIXED CODE ###

import sys


def main():
    x, y, z, k = [int(n) for n in input().split()]
    va = sorted([int(n) for n in input().split()], reverse=True)
    vb = sorted([int(n) for n in input().split()], reverse=True)
    xy = min(x*y, k)
    vab = (sorted([a + b for a in va for b in vb], reverse=True))[:xy]
    vc = sorted([int(n) for n in input().split()], reverse=True)
    xyz = min(x*y*z, k)
    print(*((sorted([ab + c for ab in vab for c in vc], reverse=True))[:xyz]), sep="\n")


main()
### BUGGY CODE ###

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

abl = []


def main():
    for i in range(n):
        abl.append(tuple(map(int, input().split())))

    abl.sort()

    ans = 0

    for i in range(1, m+1):
        bm, mi = -1, -1

        for j, ab in enumerate(abl):
            if ab[0] > i:
                break
            if ab[1] > bm:
                bm = ab[1]
                mi = j

        if bm >= 0 and mi >= 0:
            ans += bm
            del abl[mi]

    return ans


print(main())


### FIXED CODE ###

from heapq import heappush, heappop

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

abl = []
for i in range(n):
    abl.append(tuple(map(int, input().split())))
abl.sort()

q = []
count = 0
ans = 0

for i in range(1, m+1):
    while count < n:
        a, b = abl[count]
        if a > i:
            break
        heappush(q, -b)
        count += 1
    if q:
        ans += -heappop(q)

print(ans)