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

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
 
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		S = S[::-1]
	else:
		_, F, C = Que
		if F == "1":
			S = C + S
		else:
			S += C
 
print(S)

### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)
if flg == 1:
	print(*q, sep = "")
else:
	print(*list(q)[::-1], sep = "")
### BUGGY CODE ###

S=list(input())

while S[0]=="W":
	S.pop(0)


c=0
while "W" in S:
	c+=S.index("W")
	S.pop(S.index("W"))

print(c)

### FIXED CODE ###

S=list(input())

#while S[0]=="W":
#	S.pop(0)

#n=S.count("W")

c=0
x=0
for i in range(len(S)):
	if S[i]=="B": x+=1
	else: c+=x

print(c)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
S=[]
D=[]

for i in A:
    if i in S:
        D.append(i)
    else:
        S.append(i)

import sys
if D==[]:
    print(N)
    sys.exit()

M=N-len(D)
if len(D)%2==1:
    print(M-1)
else:
    MaxD,MinD=max(D),min(D)
    Med=-1
    for i in S:
        if MinD<=i<=MaxD:
            Med=i
            print(M)
            break
    if Med==-1:
        print(M-2)

### FIXED CODE ###

N=int(input());A=list(map(int,input().split()));S=len(set(A));print(S-1+S%2)
### BUGGY CODE ###

import sys
import math

def dp(costs, K):
    table = [0]
    for i in range(1, len(costs)):
        min_cost = table[i-1] + abs(costs[i-1] - costs[i])
        for j in range(2, K + 1):
            jump = i - j
            if(jump >= 0):
                step = table[jump] + abs(costs[jump] - costs[i])
                min_cost = min([min_cost, step])
            else: 
                break
        table.append(min_cost)
    return table[-1]
        
def main():
    for index in range(2):
        line = input()
        if index == 0:
            K = int(line.split(" ")[1])
            continue
        costs = [int(s) for s in line.split(" ")]
        print(dp(costs, K))

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
import math

def dp(costs, K):
    table = [0]
    for i in range(1, len(costs)):
        min_cost = table[i-1] + abs(costs[i-1] - costs[i])
        for j in range(2, K + 1):
            jump = i - j
            if(jump >= 0):
                step = table[jump] + abs(costs[jump] - costs[i])
                min_cost = min([min_cost, step])
            else: 
                break
        table.append(min_cost)
    return table[-1]
        
def main():
    for index in range(2):
        line = input()
        if index == 0:
            K = int(line.split(" ")[1])
            continue
        costs = [int(s) for s in line.split(" ")]
        print(dp(costs, K))

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

#import numpy as np
N = int(input())
M = list(map(int, input().split()))
result = 0
for i in range(N):
  x = M[i]
  #print(i, x)
  result += len([1 for m,n in enumerate(range(i+x+1, N)) if m+1 == M[n]])
print(result)

### FIXED CODE ###

N = int(input())
M = list(map(int, input().split()))
L = [0] * N
R = [0] * N
for i,m in enumerate(M):
  if i+m >= N:
    continue
  L[i+m] += 1
for i,m in enumerate(M[::-1]):
  if i+m >= N:
    continue
  R[i+m] += 1
R = R[::-1]
#print(L)
#print(R)
print(sum([l*r for l,r in zip(L,R)]))
### BUGGY CODE ###

a=int(input())
lis=list(map(int,input().split()))
suu=0
ans=0
while len(lis)>0:
  if lis[0]==suu+1:
    suu+=1
  else:
    ans+=1
  del lis[0]
if ans==a:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

a=int(input())
lis=list(map(int,input().split()))
suu=0
ans=0
x=0
while len(lis)>x:
  if lis[x]==suu+1:
    suu+=1
  else:
    ans+=1
  x+=1
if ans==a:
  print(-1)
else:
  print(ans)
### BUGGY CODE ###

import numpy as np

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

dp = np.full(N,float('inf'))
dp[0] = 0

for i in range(1,N):
    for j in range(max(0,i-K),i):
        # print(h[j],':',h[i])
        dp[i] = min(dp[i], dp[j]+abs(h[j]-h[i]))
    # print(dp)
print(int(dp[-1]))

### FIXED CODE ###

import numpy as np

N,K = map(int, input().split())
h = np.array(list(map(int, input().split())))
dp = np.full(N,10**10)
dp[0] = 0
for i in range(1,N):
    start = max(0,i-K)
    dp[i] = np.min(dp[start:i]+np.abs(h[i]-h[start:i]))
print(int(dp[-1]))
### BUGGY CODE ###

from heapq import *
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

g = []
for i in range(h):
    g.append(input())

#dp[i][j][direction] = (count, rem)
inf = 10**18
dp = [[[(inf, inf) for i in range(4)] for j in range(w)] for k in range(h)]

q = [((0, 0), x1-1, y1-1, i) for i in range(4)]
used = [[[False for l in range(4)] for i in range(w)] for j in range(h)]

for i in range(4):
    dp[x1-1][y1-1][i] = (0, 0)

while q:
    count, x, y, d = heappop(q)
    if used[x][y][d]:
        continue
    if x == x2 and y == y2:
        break
    used[x][y][d] = True

    for dx, dy, dd in [(-1, 0, 2), (1, 0, 0), (0, -1, 1), (0, 1, 3)]:
        if 0 <= x+dx <= h-1 and 0 <= y+dy <= w-1:
            if g[x+dx][y+dy] == "@":
                continue

            if dd == d:
                if count[1] == k-1:
                    nextcount = (count[0]+1, 0)
                else:
                    nextcount = (count[0], count[1]+1)

            else:
                nextcount = (count[0]+1, 0)

            if dp[x+dx][y+dy][dd] <= nextcount:
                continue
            dp[x+dx][y+dy][dd] = nextcount
            heappush(q, (nextcount, x+dx, y+dy, dd))

ans = inf
for i in range(4):
    temp = dp[x2-1][y2-1][i][0]
    if dp[x2-1][y2-1][i][1] > 0:
        temp += 1
    ans = min(ans, temp)

if ans >= inf:
    ans = -1
print(ans)


### FIXED CODE ###

from collections import deque
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())

g = []
for i in range(h):
    g.append(input())

inf = 10**9
dp = [[inf for j in range(w)] for k in range(h)]
dp[x1-1][y1-1] = 0
q = deque([(x1-1, y1-1)])

while q:
    x, y = q.popleft()
    if (x, y) == (x2-1, y2-1):
        break
    c = dp[x][y]
    for i in range(x+1, min(x+k+1, h)):
        if g[i][y] == "@":
            break

        if dp[i][y] == inf:
           q.append((i, y))

        if dp[i][y] >= c + 1:
            dp[i][y] = c + 1
        else:
            break


    for i in range(x-1, max(x-k-1, -1), -1):
        if g[i][y] == "@":
            break

        if dp[i][y] == inf:
           q.append((i, y))

        if dp[i][y] >= c + 1:
            dp[i][y] = c + 1
        else:
            break

    for j in range(y+1, min(y+k+1, w)):
        if g[x][j] == "@":
            break

        if dp[x][j] == inf:
            q.append((x, j))

        if dp[x][j] >= c + 1:
            dp[x][j] = c + 1
        else:
            break


    for j in range(y-1, max(y-k-1, -1), -1):
        if g[x][j] == "@":
            break

        if dp[x][j] == inf:
            q.append((x, j))

        if dp[x][j] >= c + 1:
            dp[x][j] = c + 1
        else:
            break

if dp[x2-1][y2-1] == inf:
    print(-1)
else:
    print(dp[x2-1][y2-1])
### BUGGY CODE ###

import sys
from io import StringIO
import unittest
import math

def resolve():
    x = int(input())

    a = math.floor(x ** 0.2)

    b5 = a ** 5 - x # マイナスになる

    b = 0

    right = 0
    left = -1 * (10 ** 9)
    if x == 1:
        a = 0
        b = -1
    else:
        while 1:
            mid = (right + left) // 2

            if mid ** 5 > b5:
                right = mid
            elif mid ** 5 < b5:
                left = mid
            else:
                b = mid
                break

    print(a, b, sep=' ')


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """33"""
        output = """2 -1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """1"""
        output = """0 -1"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

### FIXED CODE ###

import sys
from io import StringIO
import unittest
import math

def resolve():
    x = int(input())

    a = math.floor(x ** 0.2) + 1

    b5 = a ** 5 - x

    if x == 1:
        a = 0
        b = -1
    elif b5 == 0:
        b = 0
    else:
        right = (10 ** 9)
        left = -1 * (10 ** 9)
        pre_right = (10 ** 9)
        pre_left = -1 * (10 ** 9)

        b = 0
        pre_mid = 10 ** 9
        while 1:
            mid = (right + left) // 2
            ans = mid ** 5
            # print(left, right)

            if ans > b5:
                right = mid - 1
            elif ans < b5:
                left = mid + 1
            else:
                b = mid
                break

            if [pre_right, pre_left] == [right, left]:
                a -= 1
                b5 = a ** 5 - x
                right = (10 ** 9)
                left = -1 * (10 ** 9)

            pre_right = right
            pre_left = left

    print(a, b, sep=' ')


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """33"""
        output = """2 -1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """1"""
        output = """0 -1"""
        self.assertIO(input, output)

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

a,b=map(int, input().split())
s = 1
for i in range(a,b+1):
  s *= i
if s < 0:
  print("Negative")
elif s == 0:
  print("Zero")
else:print("Positive")

### FIXED CODE ###

def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
l = []
f = 0
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")

a,b = ma()
if a*b > 0 :
    if a+b >0:print("Positive")
    elif abs(b-a+1)%2==0:print("Positive")
    else:print("Negative")
else:print("Zero")
### BUGGY CODE ###

N=int(input())
P=list(map(int,input().split()))
ans=1
for i in range(1,N):
  MIN=min(P[0:i])
  if MIN>=P[i]:
    ans+=1
print(ans)    

### FIXED CODE ###

N=int(input())
P=list(map(int,input().split()))
ans=0
MIN=10**6
for i in range(N):
  MIN=min(MIN,P[i])
  if MIN>=P[i]:
    ans+=1
print(ans)
### BUGGY CODE ###

n, y = map(int, input().split())
for i in range(n + 1):
    for j in range(n + 1 - i):
        for k in range(n + 1 - i - j):
            if 10000 * k + 5000 * j + 1000 * i == y and k + j + i == n:
                print(k, j, i)
                exit()
print(-1, -1, -1)

### FIXED CODE ###

n, y = map(int, input().split())
for i in range(n + 1):
    for j in range(n + 1 - i):
        if 10000 * (n - i - j) + 5000 * j + 1000 * i == y:
            print(n - i - j, j, i)
            exit()
print(-1, -1, -1)
### BUGGY CODE ###

# import numpy as np

# def second_largest(numbers):
#     count = 0
#     m1 = m2 = float('-inf')
#     for x in numbers:
#         count += 1
#         if x > m2:
#             if x >= m1:
#                 m1, m2 = x, m1            
#             else:
#                 m2 = x
#     return m2 if count >= 2 else None

def second_largest(q):
    q.sort()
    return q[-2]

    

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

cnt = 0
for i in range(len(p)):
    for j in range(i+1,len(p)):
        cnt += second_largest(p[i:j+1])
print(cnt)


### FIXED CODE ###

#!/usr/bin/env python3
N=int(input())
p=list(map(int,input().split()))
LL = [0] + [i for i in range(N+1)]
RR = [i+1 for i in range(N+1)] + [N+1]
E = [(v, i+1) for i, v in enumerate(p)]
E.sort()
ans=0
for v, i in E:
  r0 = RR[i]
  r1 = RR[r0]
  l0 = LL[i]
  l1 = LL[l0]
  RR[l0] = r0
  LL[r0] = l0

  ans += ((r1 - r0)*(i-l0)+(r0-i)*(l0 - l1))*v

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

def solve(H, M, W, bombs):
    count_h = [0 for _ in range(H)]
    count_w = [0 for _ in range(W)]
    for b in bombs:
        count_h[b[0] - 1] += 1
        count_w[b[1] - 1] += 1
    max_h = max(count_h)
    max_w = max(count_w)
    points_h = [i for i, c in enumerate(count_h) if c == max_h]
    points_w = [i for i, c in enumerate(count_w) if c == max_w]
    ans = max_h + max_w
    if _find_cross_bomb(points_h, points_w, bombs):
        ans -= 1
    print(ans)


def _find_cross_bomb(points_h, points_w, bombs):
    c = 0
    for b in bombs:
        if (b[0] - 1) in points_h:
            if (b[1] - 1) in points_w:
                c += 1
    return c == len(points_h) * len(points_w)


if __name__ == "__main__":
    H, W, M = map(int, input().split())
    bombs = [list(map(int, input().split())) for i in range(M)]
    solve(H, M, W, bombs)


### FIXED CODE ###

import sys


def solve():
    readline = sys.stdin.readline
    H, W, M = map(int, readline().split())
    h = [0] * H
    w = [0] * W
    b = set()
    for _ in range(M):
        y, x = map(int, readline().split())
        x -= 1
        y -= 1
        h[y] += 1
        w[x] += 1
        b.add(y * W + x)
    h = sorted(zip(h, range(H)), reverse=True)
    ans = 0
    for i in range(W):
        x = w[i]
        for y, j in h:
            if x + y > ans:
                if j * W + i in b:
                    ans = x + y - 1
                else:
                    ans = x + y
                    break
            else:
                break
    print(ans)


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

from sys import stdin
def solve():
    import collections
    readline = stdin.readline
    h,w = map(int,readline().split())
    si,sj = map(int,readline().split())
    gi,gj = map(int,readline().split())
    si,sj,gi,gj = si-1,sj-1,gi-1,gj-1
    grid = []
    for _ in range(h):
        grid.append(readline()[:-1])
    INF = float("inf")
    dist = {(i,j):INF for i in range(h) for j in range(w)}
    q = collections.deque([(si,sj)])
    dist[si,sj] = 0
    while q:
        i,j = q.popleft()
        if (i,j) == (gi,gj):
            return dist[i,j]
        for ni,nj in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
            if 0 <= ni and ni < h and 0 <= nj and nj < w and grid[ni][nj] == ".":
                if dist[ni,nj] > dist[i,j]:
                    dist[ni,nj] = dist[i,j]
                    q.appendleft((ni,nj))
        for ni in range(i-2,i+3):
            if not (0 <= ni and ni < h): continue
            for nj in range(j-2,j+3):
                if not (0 <= nj and nj < w): continue
                if (ni,nj) not in [(i,j),(i+1,j),(i-1,j),(i,j+1),(i,j-1)] and grid[ni][nj] == ".":
                    if dist[ni,nj] > dist[i,j] + 1:
                        dist[ni,nj] = dist[i,j] + 1
                        q.append((ni,nj))

    return -1

print(solve())

### FIXED CODE ###

from sys import stdin
def solve():
    import collections
    readline = stdin.readline
    h,w = map(int,readline().split())
    si,sj = map(int,readline().split())
    gi,gj = map(int,readline().split())
    si,sj,gi,gj = si-1,sj-1,gi-1,gj-1
    grid = []
    for _ in [0]*h:
        grid.append(readline()[:-1])
    INF = float("inf")
    dist = [[INF]*w for _ in range(h)]
    q = collections.deque([(si,sj)])
    dist[si][sj] = 0
    while q:
        i,j = q.popleft()
        if (i,j) == (gi,gj):
            return dist[i][j]
        for ni,nj in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
            if 0 <= ni and ni < h and 0 <= nj and nj < w and grid[ni][nj] == ".":
                if dist[ni][nj] > dist[i][j]:
                    dist[ni][nj] = dist[i][j]
                    q.appendleft((ni,nj))
        listj = range(j-2,j+3)
        for ni in range(i-2,i+3):
            if not (0 <= ni and ni < h): continue
            for nj in listj:
                if not (0 <= nj and nj < w): continue
                if grid[ni][nj] == "." and dist[ni][nj] > dist[i][j] + 1:
                    dist[ni][nj] = dist[i][j] + 1
                    q.append((ni,nj))

    return -1

print(solve())
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import collections
# input
n = int(input())
s = []
for i in range(n):
    s.append(input())
c = collections.Counter(s)
# output
# print(c.most_common()[0][1])
# print(list(c))
o = list(k for k, v in c.items() if v == c.most_common()[0][1])
for i in sorted(o):
    print(i)


### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import Counter
# input
n = int(input())
s = []
for i in range(n):
    s.append(input())
c = Counter(s)
# output
ma = c.most_common()[0][1]
o = [k for k, v in c.items() if v == ma]
o.sort()
print('\n'.join(o))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	N = int(input())
	A = input().split()
	A = [int(s) for s in A]

	base = -1
	for i, val in enumerate(A):
		if i == 0:
			base == val
			continue
		base = gcd(val, base)

	ans = base
	for i, val in enumerate(A):
		tmp = -1
		if ans >= val or gcd(val, tmp) == ans:
			break
		for j, vol in enumerate(A):
			if i == j:
				continue
 
			if tmp == -1:
				tmp = vol
				continue
 
			tmp = gcd(vol, tmp)
			if tmp <= ans:
				break
 
		if tmp > ans:
			ans = tmp
 
	print(ans)
 
 
 
def gcd(a, b):
	if a > b:
		a, b = b, a
		
	while a > 0:
		a, b = b % a, a
	return b
	
if __name__ == "__main__":
	main()

### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
	
def main():
	N = int(input())
	A = list(map(int, input().split(" ")))
	
	L = []
	for i, val in enumerate(A):
		if i == 0:
			L.append(val)
			continue
		tmp = gcd(val, L[i-1])
		L.append(tmp)
		
	R = []
	for i, val in enumerate(reversed(A)):
		if i == 0:
			R.append(val)
			continue
		tmp = gcd(val, R[i-1])
		R.append(tmp)
		
	ans = 0
	for i, val in enumerate(A):
		if i == 0:
			tmp = R[len(A)-2]
		elif i == len(A)-1:
			tmp = L[len(A)-2]
		else:
			tmp = gcd(L[i-1], R[len(A)-i-2])
			
		if tmp > ans:
			ans = tmp
		
	print(ans)
	
def gcd(a, b):
	if a > b:
		a, b = b, a
		
	while a > 0:
		a, b = b % a, a
	return b
	
if __name__ == "__main__":
	main()
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
ans=''
for i in range(n):
    if n%2==0:
        if i%2!=0:
            if i==1:
                ans=str(a[i])+ans
            else:
                ans=str(a[i])+' '+ans
        else:
            ans+=' '+str(a[i])
    else:
        if i%2==0:
            if i==0:
                ans=str(a[i])+ans
            else:
                ans=str(a[i])+' '+ans
        else:
            ans+=' '+str(a[i])
if n==1:
    ans=a[0]
print(ans)

### FIXED CODE ###

import sys
input=sys.stdin.readline
n=int(input())
a=input().split()
ans=[]

for i in range(n):
    if n%2==0:
        if i%2==0:
            ans.append(a[i])
    else:
        if i%2!=0:
            ans.append(a[i])
ans=ans[::-1]
for i in range(n):
    if n%2==0:
        if i%2!=0:
            ans.append(a[i])
    else:
        if i%2==0:
            ans.append(a[i]) 
ans=ans[::-1]
ans=' '.join(ans)
if n==1:
    ans=a[0]
'''
for j in range(n):
    ans.insert(0,a[j])
'''
print(ans)
### BUGGY CODE ###

import numpy as np

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

A = np.array(A)

cnt = 0

for day in range(1,M+1):
    B = A[A[:,0]<=day,1]
    if list(B):
        ind = np.argmax(B)
        cnt += B[ind]
        A[ind,1] = 0
print(cnt)


### FIXED CODE ###

from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*10**5
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v[:M]:
	for j in i:heappush(q,-j)
	z-=heappop(q)
print(z)
### BUGGY CODE ###

s=input()
n=len(s)
mod=10**9+7
dp=[[0]*13 for _ in range(n+1)]
dp[0][0]=1
mul=1

for i in range(n):
    x=s[-(i+1)]  # 後ろから見ていく
    if x=='?':
        for k in range(10):
            for j in range(13):
                dp[i+1][(mul*k+j)%13]+=dp[i][j]
                dp[i+1][(mul*k+j)%13]%=mod
    else:
        k=int(x)
        for j in range(13):
            dp[i+1][(mul*k+j)%13]+=dp[i][j]
            dp[i+1][(mul*k+j)%13]%=mod
    mul=mul*10%13
# print(dp)
print(dp[n][5])


### FIXED CODE ###

s=input()
n=len(s)
mod=10**9+7
dp=[[0]*13 for _ in range(n+1)]
dp[0][0]=1
mul=1

for i in range(n):
    x=s[-(i+1)]  # 後ろから見ていく
    if x=='?':
        for k in range(10):
            for j in range(13):
                dp[i+1][(mul*k+j)%13]+=dp[i][j]
                dp[i+1][(mul*k+j)%13]%=mod
    else:
        k=int(x)
        for j in range(13):
            dp[i+1][(mul*k+j)%13]+=dp[i][j]
            dp[i+1][(mul*k+j)%13]%=mod
    mul=mul*10%13
# print(dp)
print(dp[n][5])
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from fractions import gcd
from functools import reduce
 
N,*A = map(int,read().split())
 
MOD = 10 ** 9 + 7
 
lcm = reduce(lambda x,y: x*(y//gcd(x,y)), A)
 
answer = sum((lcm//x) % MOD for x in A)
answer %= MOD
print(answer)

### FIXED CODE ###

from fractions import gcd
from functools import reduce
 
N = int(input())
A = list(map(int, input().split()))
 
MOD = 10 ** 9 + 7
 
lcm = reduce(lambda x,y: x*(y//gcd(x,y)), A)
 
answer = sum(lcm//x for x in A)
answer %= MOD
print(answer)