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

import itertools
n,k=map(int,input().split())
a=list(map(int, input().split()))
a=sorted(a)
ans=[]
for i in itertools.combinations(a, 2):
  ans.append(i[0]*i[1])
ans=sorted(ans)
print(ans[k-1])

### FIXED CODE ###

import numpy as np
import sys
 
n,k=map(int,sys.stdin.readline().split())
a=np.array(list(map(int, input().split())))
 
a.sort()
neg=a[a<0]
zero=a[a==0]
pos=a[a>0]
 
def count(x):
    cnt=0
    if x>=0:
        cnt+=len(zero)*n
    cnt+=np.searchsorted(a,x//pos,side='right').sum()
    cnt+=(n-np.searchsorted(a,(-x-1)//(-neg),side='right')).sum()
    cnt-=np.count_nonzero(a*a<=x)
    return cnt//2
 
 
left=-10**18
right=10**18
 
while left+1<right:
    x=(left+right)//2
    out=count(x)
    if out>=k:
        right=x
    if out<k:
        left=x
print(right)
### BUGGY CODE ###

n = int(input())
s = input()
ans = set()
for i in range(1000):
    a = i % 10
    b = i // 10 % 10
    c = i // 100 % 10
    for j in range(n):
        if int(s[j]) == c:
            for k in range(j + 1, n):
                if int(s[k]) == b:
                    for l in range(k + 1, n):
                        if int(s[l]) == a:
                            ans.add(str(c) + str(b) + str(a))
print(len(ans))

### FIXED CODE ###

n = int(input())
s = input()
ans = 0
password = [0, 0, 0]
for i in range(1000):
    for j in range(3):
        password[2 - j] = i // (10 ** j) % 10
    if str(password[0]) in s:
        index = s.index(str(password[0]))
        if str(password[1]) in s[index + 1:]:
            index2 = index + 1 + s[index + 1:].index(str(password[1]))
            if str(password[2]) in s[index2 + 1:]:
                ans += 1
print(ans)
### BUGGY CODE ###

def main():

    A,B,C,D = (int(i) for i in input().split())

    count = 0
    for i in range(A,B+1):
        if i%C != 0 and i%D !=0:
            count += 1
        else:
            continue
    print(count)
    


if __name__ == '__main__':
    main()


### FIXED CODE ###

import math
import fractions

def main():

    A,B,C,D = (int(i) for i in input().split())

    CD = C * D / fractions.gcd(C,D)
    #print("CD = ", CD)

    count_C = B//C - (A-1)//C
    #print("count_C = ", count_C)

    count_D = B//D - (A-1)//D
    #print("count_D = ", count_D)

    count_CD = B//int(CD) - (A-1)// int(CD)
    #print("count_CD = ", count_CD)

    print(int(B-A+1 - (count_C + count_D - count_CD)))

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

N,X=map(int,input().split())
x=list(map(int,input().split()))

diffx=[abs(X-x[i]) for i in range(N)]
minDiff=min(diffx)
isFinished=0
for i in range(minDiff,0,-1):
    if minDiff%i==0:
        canDivide=1
        for j in range(N):
            if diffx[j]%i:
                canDivide=0
                break
        if canDivide:
            print(i)
            isFinished=1
            break
    if isFinished:
        break


### FIXED CODE ###

N,X=map(int,input().split())
x=list(map(int,input().split()))
diffx=[abs(X-x[i]) for i in range(N)]

def euclid(a,b):
    if b==0:
        return a
    else:
        return euclid(b,a%b)

gcd=diffx[0]
for i in range(1,N):
    gcd=euclid(diffx[i],gcd)
print(gcd)
### BUGGY CODE ###

N = int(input())
HP = sorted([list(map(int,input().split())) for i in range(N)],key = lambda x: x[0]+x[1])
dp = [[float('inf') for i in range(N+1)] for j in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
    for j in range(N+1):
        if dp[i-1][j-1] <= HP[i-1][0]:
            dp[i][j] = min(dp[i-1][j],dp[i-1][j-1]+HP[i-1][1])
        else:
            dp[i][j] = dp[i-1][j]
for i in range(N,-1,-1):
    if dp[-1][i] != float('inf'):
        print(i)
        break

### FIXED CODE ###

N = int(input())
HP = sorted([list(map(int,input().split())) for i in range(N)],key = lambda x: x[0]+x[1])
dp1 = [float('inf') for i in range(N+1)]
dp2 = [float('inf') for i in range(N+1)]
dp1[0] = 0
dp2[0] = 0
for i in range(1,N+1):
    for j in range(N+1):
        if dp1[j-1] <= HP[i-1][0]:
            dp2[j] = min(dp1[j],dp1[j-1]+HP[i-1][1])
        else:
            dp2[j] = dp1[j]
    dp1,dp2 =dp2,dp1
for i in range(N,-1,-1):
    if dp1[i] != float('inf'):
        print(i)
        break
### BUGGY CODE ###

import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
import math
from itertools import combinations
def resolve():
    eps = 1e-7
    N, K = lr()
    ci = [[] for _ in range(N)]
    _r = []
    for i in range(N):
        x, y, c = lr()
        ci[i].extend([x, y])
        _r.append(c)

    l = 0
    r = 10 ** 6
    while l + eps < r:
        T = (l + r) / 2
        rad = [(T/_r[i])**2 for i in range(N)]
        ans = 0
        xys = []
        for i, j in combinations(range(N), 2):
            if ci[i][0] == ci[j][0] and ci[i][1] == ci[j][1]:
                    continue
            xt = ci[j][0]-ci[i][0]
            yt = ci[j][1]-ci[i][1]
            XY = xt**2+yt**2
            a = (XY+rad[i]-rad[j])/2
            d = XY*rad[i]-a**2
            if d < eps:
                continue
            x1 = (a*xt+yt*math.sqrt(d))/XY+ci[i][0]
            y1 = (a*yt-xt*math.sqrt(d))/XY+ci[i][1]
            x2 = (a*xt-yt*math.sqrt(d))/XY+ci[i][0]
            y2 = (a*yt+xt*math.sqrt(d))/XY+ci[i][1]
            xys.append([x1, y1])
            xys.append([x2, y2])
        xys.extend(ci)
        for X, Y in xys:
            tmp = 0
            for (x, y), k in zip(ci, rad):
                if (x-X)**2+(y-Y)**2 < k+eps:
                    tmp += 1
            ans = max(ans, tmp)
            if ans >= K:
                break
        if ans >= K:
            r = T
        else:
            l = T
    print(r)
resolve()

### FIXED CODE ###

import sys
from math import sqrt
from itertools import combinations
def resolve():
    eps = 1e-7
    N, K, *xyc = map(int, sys.stdin.read().split())
    x = xyc[::3]
    y = xyc[1::3]
    ci = list(zip(x, y))
    _r = xyc[2::3]
    l = 0
    r = 10 ** 6
    while l + eps < r:
        T = (l + r) / 2
        rad = [(T/_r[i])**2 for i in range(N)]
        ans = 0
        xys = []
        for i, j in combinations(range(N), 2):
            cx1, cy1 = ci[i]
            cx2, cy2 = ci[j]
            xt = cx2-cx1
            yt = cy2-cy1
            XY = xt**2+yt**2
            a = (XY+rad[i]-rad[j])/2
            d = XY*rad[i]-a**2
            if d < eps:
                continue
            x1 = (a*xt+yt*sqrt(d))/XY+cx1
            y1 = (a*yt-xt*sqrt(d))/XY+cy1
            x2 = (a*xt-yt*sqrt(d))/XY+cx1
            y2 = (a*yt+xt*sqrt(d))/XY+cy1
            xys.append([x1, y1])
            xys.append([x2, y2])
        xys.extend(ci)
        for X, Y in xys:
            tmp = 0
            for (x, y), k in zip(ci, rad):
                if (x-X)**2+(y-Y)**2 < k+eps:
                    tmp += 1
            ans = max(ans, tmp)
            if ans >= K:
                break
        if ans >= K:
            r = T
        else:
            l = T
    print(r)
resolve()
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
for i in range(m):
  b, c = map(int, input().split())
  a += [c] * b
a = sorted(a)
print(sum(a[-n:]))


### FIXED CODE ###

from heapq import heappop, heappush

n, m = map(int, input().split())
a = list(map(int, input().split()))
q = []
for x in a:
  heappush(q, [-x, 1])
for i in range(m):
  b, c = map(int, input().split())
  heappush(q, [-c, b])
r = []
k = 0
while k < n:
  c, b = heappop(q)
  c = -c
  r += [c] * b
  k += b
print(sum(r[:n]))
### BUGGY CODE ###

s = list(input())

s_mod3 = []

for i in range(len(s)):
    s_mod3.append(int(s[i]) % 3)

minLength = 4
maxLength = len(s)
cnt = 0

for position in range(maxLength):
    modCnt = 0
    for i in range(position, maxLength):
        modCnt = (modCnt + s_mod3[i]) % 3
        if modCnt == 0:
            num = int(''.join(s[position:i+1]))
            if num % 673 == 0:
                cnt += 1

print(cnt)

### FIXED CODE ###

s = list(input())
mods = [0]*(len(s))
countRemainder = [0] * 2019
cnt = 0


m = 1
mod = 0
for i in range(len(s)):
    mod += int(s[len(s) - i - 1]) * m

    mod %= 2019
    m *= 10
    m %= 2019

    countRemainder[mod] += 1

countRemainder[0] += 1

for i in range(2019):
    cnt += int(countRemainder[i] * (countRemainder[i] - 1) / 2)

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

A,B,C=map(int,input().split())
i = 0
a = [A]
b = [B]
c = [C]
while a[i]%2==0 and b[i]%2==0 and c[i]%2==0:
  a.append((b[i]+c[i])/2)
  b.append((a[i]+c[i])/2)
  c.append((a[i]+b[i])/2)
  i += 1
print(i)

### FIXED CODE ###

import sys
A,B,C=map(int,input().split())
if A==B and B==C and A%2==0:
  print('-1')
  sys.exit()
if A==B and B==C and A%2==1:
  print(0)
  sys.exit()
i = 0
a = [A]
b = [B]
c = [C]
while a[i]%2==0 and b[i]%2==0 and c[i]%2==0:
  a.append((b[i]+c[i])/2)
  b.append((a[i]+c[i])/2)
  c.append((a[i]+b[i])/2)
  i += 1
print(i)
### BUGGY CODE ###

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

max = 0
cnt = 0

for i in range(len(H)):
    k = H[i]
    res = H[i+1:]
    for j in res:
        if(j <= k):
            cnt += 1
            k = j
        else:
            break
        if(max < cnt):
            max = cnt
    cnt = 0

print(max)


### FIXED CODE ###

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

ans = 0
tmp = 0

for i in range(N-1):
    if(H[i] < H[i+1]):
        #ans = max(ans,tmp)
        tmp = 0
    else:
        tmp += 1
    ans = max(ans,tmp)

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

N,K=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
F=sorted(list(map(int,input().split())))

def judge(t):
    global a,F,N,K
    K_t=K
    for i in range(N):
        target=t//F[i]
        if a[i]<=target:
            continue
        if K_t>=a[i]-target:#引けるかどうか
            K_t-=a[i]-target
        else:
            return False
    return True

t_max=a[0]*F[-1] #絶対True
t_min=-1 #絶対False
while t_max>t_min+1:
    t_mid=(t_max+t_min)//2
    if judge(t_mid):
        t_max=t_mid
    else:
        t_min=t_mid
print(t_max)

### FIXED CODE ###

N,K=map(int,input().split())
a=sorted(list(map(int,input().split())),reverse=True)
F=sorted(list(map(int,input().split())))
import numpy as np
a=np.array(a)
F=np.array(F)

t_max=a[0]*F[-1] #絶対True
t_min=-1 #絶対False
while t_max>t_min+1:
    t_mid=(t_max+t_min)//2
    K_temp=np.sum(np.maximum(a-t_mid//F,0))
    if K_temp<=K:
        t_max=t_mid
    else:
        t_min=t_mid
print(t_max)
### BUGGY CODE ###

from sys import stdin
from collections import deque


class BFS():
    def __init__(self, grid_graph):
        # grid表現
        self.graph = grid_graph
        self.H = len(self.graph)
        self.W = len(self.graph[0])
        # 始点からの距離
        self.dist = [[-1]*self.W for i in range(self.H)]
        # キュー
        self.que = deque()

    def bfs(self, sv_arr):
        dx = [ 1, 0,-1, 0]
        dy = [ 0, 1, 0,-1]

        # 初期条件(0-indexed)
        for sv in sv_arr:
            self.dist[sv[0]][sv[1]] = 0
            self.que.append(sv)

        while len(self.que)>0:
            v = self.que.popleft()
            for _dx, _dy in zip(dx,dy):
                nv = [v[0]+_dx,v[1]+_dy]
                if 0<=nv[0]<self.H and 0<=nv[1]<self.W:
                    if self.dist[nv[0]][nv[1]] != -1:
                        continue
                    self.dist[nv[0]][nv[1]] = self.dist[v[0]][v[1]] + 1
                    self.que.append(nv)


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    H, W = list(map(int,_in[0].split(' ')))  # type:list(int)
    graph = []
    for i in range(H):
        _ = list(_in[i+1])  # type:list(str)
        graph.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    bfs = BFS(graph)

    sv_arr = []
    for i in range(H):
        for j in range(W):
            if graph[i][j] == '#':
                sv_arr.append([i,j])
    bfs.bfs(sv_arr)
    cnt = max(sum(bfs.dist,[]))
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


if __name__ == "__main__":
    main()


### FIXED CODE ###

from sys import stdin
from collections import deque


class BFS():
    def __init__(self, grid_graph):
        # grid表現
        self.graph = grid_graph
        self.H = len(self.graph)
        self.W = len(self.graph[0])
        # 始点からの距離
        self.dist = [[-1]*self.W for i in range(self.H)]
        # キュー
        self.que = deque()

    def bfs(self, sv_arr):
        dx = [ 1, 0,-1, 0]
        dy = [ 0, 1, 0,-1]

        # 初期条件(0-indexed)
        for sv in sv_arr:
            self.dist[sv[0]][sv[1]] = 0
            self.que.append(sv)

        while self.que:
            v = self.que.popleft()
            for _dx, _dy in zip(dx,dy):
                nv = [v[0]+_dx,v[1]+_dy]
                if not 0<=nv[0]<self.H: continue 
                if not 0<=nv[1]<self.W: continue
                if self.dist[nv[0]][nv[1]] != -1: continue
                self.dist[nv[0]][nv[1]] = self.dist[v[0]][v[1]] + 1
                self.que.append(nv)


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    H, W = list(map(int,_in[0].split(' ')))  # type:list(int)
    graph = []
    for i in range(H):
        _ = list(_in[i+1])  # type:list(str)
        graph.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    bfs = BFS(graph)

    sv_arr = []
    for i in range(H):
        for j in range(W):
            if bfs.graph[i][j] == '#':
                sv_arr.append([i,j])

    bfs.bfs(sv_arr)
    cnt = max(max(_) for _ in bfs.dist)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


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

import numpy as np

h, w = map(int, input().split())

maze = [input() for i in range(h)]

cnt = [[0] * w for i in range(h)]

for y in range(h):
    total = 0
    for x in range(w):
        if maze[y][x] == "#":
            total = 0
        elif maze[y][x] == ".":
            total += 1
            cnt[y][x] += total

for x in range(w):
    total = 0
    for y in range(h):
        if maze[y][x] == "#":
            total = 0
        
        elif maze[y][x] == ".":
            total += 1
            cnt[y][x] += total

for y in range(h):
    total = 0
    for x in range(w-1,-1,-1):
        if maze[y][x] == "#":
            total = 0
        elif maze[y][x] == ".":
            total += 1
            cnt[y][x] += total

for x in range(w):
    total = 0
    for y in range(h-1,-1,-1):
        if maze[y][x] == "#":
            total = 0
        
        elif maze[y][x] == ".":
            total += 1
            cnt[y][x] += total

cnt = np.array(cnt)

print(np.max(cnt)-3)

### FIXED CODE ###

## code borrowed from "maspy"

import numpy as np
import sys
buf = sys.stdin.buffer
 
H,W = map(int,buf.readline().split())
# 周りに壁を、0,1化、壁を0で持つ
grid = np.zeros((H+2,W+2),dtype=np.bool)
grid[1:-1,1:] = (np.frombuffer(buf.read(H*(W+1)),dtype='S1') == b'.').reshape((H,W+1))
 
# x軸方向の集計
def F(transpose):
  x = grid
  if transpose:
    x = (x.T).copy()
  x = np.ravel(x) # to 1D array
  L = len(x)
  
  left_zero = np.where(x,0,-np.arange(L)) # 左の壁のindexの-1倍
  np.minimum.accumulate(left_zero, out=left_zero)
  
  right_zero = np.where(x[::-1],0,np.arange(L)) # 逆順ソートした上で左の壁のindex
  np.maximum.accumulate(right_zero, out=right_zero)
  
  y = left_zero
  y += L-right_zero[::-1]
  
  return y.reshape(grid.T.shape).T if transpose else y.reshape(grid.shape)
  
answer = (F(False) + F(True)).max()-5
print(answer)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())
a=[[] for i in range(n)]
p=[]*n
l=[0]*n

def dfs(parent,current):
    for i in a[current-1]:
        if i != parent:
            l[i-1] += l[current-1]
            dfs(current,i)


for i in range(n-1):
    t1,t2 = map(int,input().split())
    a[t1-1].append(t2)
    a[t2-1].append(t1)
    
for i in range(q):
    t1,t2 = map(int,input().split())
    l[t1-1]+=t2
dfs(-1,1)

print(" ".join(map(str,l)))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

readline = sys.stdin.readline

n,q = map(int,readline().split())
a=[[] for i in range(n)]
l=[0 for _ in range(n)]
ans=[0 for _ in range(n)]

def dfs(parent,current,value):
    value += l[current]
    ans[current] = value
    for i in a[current]:
        if i == parent:
            continue
        dfs(current,i,value)


for i in range(n-1):
    t1,t2 = map(int,readline().split())
    a[t1-1].append(t2-1)
    a[t2-1].append(t1-1)
    
for i in range(q):
    t1,t2 = map(int,readline().split())
    l[t1-1]+=t2
dfs(-1,0,0)

print(" ".join(map(str,ans)))
### BUGGY CODE ###

import fractions
import copy
from functools import reduce

import sys
sys.setrecursionlimit(100000000) 

def l(i):
    if i==0:
        return 0
    else:
        return fractions.gcd(l(i-1),A[i-1])

def r(i):
    if i==N:
        return 0
    else:
        return fractions.gcd(r(i+1),A[i])

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

ans=[]
if N==2:
    ans.append(max(A))
else:
    for i in range(N):
        ans.append(fractions.gcd(l(i),r(i+1)))

print(max(ans))

### FIXED CODE ###

import sys

sys.setrecursionlimit(1000000)

import fractions
from functools import reduce

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

def mygcd(a,b):
    if a==0:
        return b
    elif b==0:
        return a
    else:
        return fractions.gcd(a,b)

L=[0]*N
for i in range(1,N):
    L[i]=mygcd(L[i-1],A[i-1])

R=[0]*N
for i in range(N-2,-1,-1):
    R[i]=mygcd(R[i+1],A[i+1])

ans=1
for i in range(N):
    c=mygcd(L[i],R[i])
    ans=max(ans,c)

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

N = int(input())
X = list(map(int, list(input())))
X.reverse()
def int2bit(n):
	ret = [0 for i in range(18)]
	cnt = 0
	i = 0
	while n > 0:
		ret[i] = n % 2
		cnt += n % 2
		n = n // 2
		i += 1
	return (ret, cnt)

def func(bit, popcount):
	ret = 0
	base = 1
	for b in bit:
		ret = (ret + base*b) % popcount
		base = (base * 2) % popcount
	return ret

def func2(n):
	(bit, popcount) = int2bit(n)
	return func(bit, popcount)

F = [0 for _ in range(N+1)]

for n in range(1, N+1):
	a = func2(n)
	F[n] = 1 + F[a]
P = sum(X)
for i in range(N):
	idx = N - 1 - i
	X[idx] = 1 - X[idx]
	if X[idx] == 1:
		print(1 + F[func(X, P+1)])
	if X[idx] == 0:
		if P == 1:
			print(0)
		else:
			print(1 + F[func(X, P-1)])
	X[idx] = 1 - X[idx]

### FIXED CODE ###

N = int(input())
X = list(map(int, list(input())))
X.reverse()
def int2bit(n):
	ret = [0 for i in range(18)]
	cnt = 0
	i = 0
	while n > 0:
		ret[i] = n % 2
		cnt += n % 2
		n = n // 2
		i += 1
	return (ret, cnt)

def func(bit, popcount):
	ret = 0
	base = 1
	for b in bit:
		ret = (ret + base*b) % popcount
		base = (base * 2) % popcount
	return ret

def func2(n):
	(bit, popcount) = int2bit(n)
	return func(bit, popcount)

F = [0 for _ in range(N+1)]

for n in range(1, N+1):
	a = func2(n)
	F[n] = 1 + F[a]
P = sum(X)

f_1 = func(X, P+1)
f_0 = func(X, max(1, P-1))

base_1 = [1]
base_0 = [1]
for i in range(N-1):
	base_1.append((base_1[i] * 2) % (P+1))
	base_0.append((base_0[i] * 2) % max(1, P-1))
for i in range(N):
	idx = N - 1 - i
	if X[idx] == 0:
		print(1 + F[(f_1 + base_1[idx]) % (P+1)])
	if X[idx] == 1:
		if P == 1:
			print(0)
		else:
			print(1 + F[(f_0 - base_0[idx]) % (P-1)])
### BUGGY CODE ###

import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  j=bisect.bisect_left(b,a[i]+1)
  if j>=n:
    break
  for k in range(j,n):
    l=bisect.bisect_left(c,b[k]+1)
    if l>=n:
      break
    ans+=n-l
print(ans)


### FIXED CODE ###

import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  j=bisect.bisect_left(a,b[i])
  k=n-bisect.bisect_right(c,b[i])
  ans+=j*k
print(ans)
### BUGGY CODE ###

h, w = map(int, input().split())
grid = [[False for j in range(w)] for i in range(h)]

for i in range(h):
    s = input()
    for j in range(w):
        if s[j] == '.':
            grid[i][j] = True
        else:
            grid[i][j] = False

left = [[0 for j in range(w)] for i in range(h)]
right = [[0 for j in range(w)] for i in range(h)]
top = [[0 for j in range(w)] for i in range(h)]
bottom = [[0 for j in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if grid[i][j]:
            if j >= 1:
                left[i][j] = left[i][j - 1] + 1
            else:
                left[i][j] = 1
        else:
            left[i][j] = 0

for i in range(h):
    for j in range(w - 1, -1, -1):
        if grid[i][j]:
            if j < w - 1:
                right[i][j] = right[i][j + 1] + 1
            else:
                right[i][j] = 1
        else:
            right[i][j] = 0


for j in range(w):
    for i in range(h):
        if grid[i][j]:
            if i >= 1:
                top[i][j] = top[i - 1][j] + 1
            else:
                top[i][j] = 1


for j in range(w):
    for i in range(h - 1, -1, -1):
        if grid[i][j]:
            if i < h - 1:
                bottom[i][j] = bottom[i + 1][j] + 1
            else:
                bottom[i][j] = 1


nums = []
for i in range(h):
    for j in range(w):
        nums.append(left[i][j] + right[i][j] + top[i][j] + bottom[i][j] - 3)

print(max(nums))


### FIXED CODE ###

import numpy as np

h, w = map(int, input().split())
grid = [[False for j in range(w)] for i in range(h)]

for i in range(h):
    s = input()
    for j in range(w):
        if s[j] == '.':
            grid[i][j] = 1
        else:
            grid[i][j] = 0

grid = np.array(grid)

L = np.zeros((h, w), dtype=int)
R = np.zeros((h, w), dtype=int)
U = np.zeros((h, w), dtype=int)
D = np.zeros((h, w), dtype=int)

for j in range(w):
    if j == 0:
        L[:, j] = grid[:, j]
    else:
        L[:, j] = (L[:, j - 1] + 1) * grid[:, j]


for j in range(w - 1, -1, -1):
    if j >= w - 1:
        R[:, j] = grid[:, j]
    else:
        R[:, j] = (R[:, j + 1] + 1) * grid[:, j]

for i in range(h):
    if i <= 0:
        U[i, :] = grid[i, :]
    else:
        U[i, :] = (U[i - 1, :] + 1) * grid[i, :]


for i in range(h - 1, -1, -1):
    if i >= h - 1:
        D[i, :] = grid[i, :]
    else:
        D[i, :] = (D[i + 1, :] + 1) * grid[i, :]

print(np.max(L + R + U + D - 3))
### BUGGY CODE ###

S = [s for s in input()]
Q = int(input())
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        S.reverse()
    else:
        F = int(T[1])
        C = [s for s in T[2]]
        if F == 1:
            S = C + S
        else:
            S = S + C
print(''.join(S))

### FIXED CODE ###

S = input()
Q = int(input())
front = ''
end = ''
cnt = 0
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        cnt += 1
    else:
        F = int(T[1])
        C = T[2]
        if F == 1:
            if cnt%2==0:
                front = C+front
            else:
                end = end+C
        else:
            if cnt%2==0:
                end = end+C
            else:
                front=C+front
if cnt % 2 == 0:
    S = front+S+end
    print(S)
else:
    S = list(reversed(S))
    end = list(reversed(end))
    front = list(reversed(front))
    S = end+S+front
    print(''.join(S))
### BUGGY CODE ###

import sys
import numpy as np

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = np.array([])
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
#a = list(a)
#b = list(b)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if a[j] + i == m:
			l = np.append(l,b[j])
			count += 1
		else:
			break
	if l.shape[0] != 0:
		maximum = np.max(l)
		maximum_index = np.argmax(l)
		ans += maximum
		l = np.delete(l,maximum_index)

print(int(ans))


### FIXED CODE ###

import sys
import heapq

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(-a_dash)
	b.append(-b_dash)

c = zip(a,b)
c = sorted(c,reverse=True)
a,b = zip(*c)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if -a[j] + i == m:
			heapq.heappush(l,b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		maximum = heapq.heappop(l)
		ans += maximum

print(-ans)