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

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = list(input())
q = I()
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        s = s[::-1]
    else:
        if query[1] == "1":
            s = [query[2]] + s
        else:
            s.append(query[2])
ans = ''
for i in s:
    ans += i
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = list(input())
q = I()
front = []
back = []
cnt = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cnt += 1
    else:
        if query[1] == "1":
            if cnt % 2 == 0:
                front.append(query[2])
            else:
                back.append(query[2])
        else:
            if cnt % 2 == 0:
                back.append(query[2])
            else:
                front.append(query[2])

if cnt % 2 == 0:
    print(''.join(front[::-1])+''.join(s)+''.join(back))
else:
    print(''.join(back[::-1])+''.join(s[::-1])+''.join(front))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
  h, w = map(int, input().split())
  A = [list(map(int, input().split())) for _ in range(h)]
  B = [list(map(int, input().split())) for _ in range(h)]
  M = 100*(h*w)
  dp = [[0]*w for i in range(h)]
  d = abs(A[0][0] - B[0][0])
  mask = (1 << (2*M)) - 1
  dp[0][0] |= 1 << (M-d)
  dp[0][0] |= 1 << (M+d)
  for i in range(h):
    for j in range(w):
      d = abs(A[i][j] - B[i][j])
      if i:
        dp[i][j] |= (dp[i-1][j] << d) & mask
        dp[i][j] |= dp[i-1][j] >> d
      if j:
        dp[i][j] |= (dp[i][j-1] << d) & mask
        dp[i][j] |= dp[i][j-1] >> d
  t = dp[-1][-1] >> M
  ans = (t & (-t)).bit_length() - 1
  print(ans)
  
if __name__ == "__main__":
  main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
  h, w = map(int, input().split())
  A = [list(map(int, input().split())) for _ in range(h)]
  B = [list(map(int, input().split())) for _ in range(h)]
  M = 80*(h+w)
  dp = [[0]*w for i in range(h)]
  d = abs(A[0][0] - B[0][0])
  mask = (1 << (2*M)) - 1
  dp[0][0] |= 1 << (M-d)
  dp[0][0] |= 1 << (M+d)
  for i in range(h):
    for j in range(w):
      d = abs(A[i][j] - B[i][j])
      if i:
        dp[i][j] |= (dp[i-1][j] << d) & mask
        dp[i][j] |= dp[i-1][j] >> d
      if j:
        dp[i][j] |= (dp[i][j-1] << d) & mask
        dp[i][j] |= dp[i][j-1] >> d
  t = dp[-1][-1] >> M
  ans = (t & (-t)).bit_length() - 1
  print(ans)
  
if __name__ == "__main__":
  main()
### BUGGY CODE ###

import numpy as np
import itertools
def p(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
n=int(input())
p=p(n)
if len(p)==1:
    print(p[0]-1)
    exit()
a=10**12
b=1
for i in p:
    b*=i
for k in range(1,len(p)):    
    for i in itertools.combinations(p, k):
        c=np.prod(i)
        a=min(a,c+b//c)
print(a-2)

### FIXED CODE ###

n=int(input())
a=10**12
for i in range(1,int(n**0.5)+2):
    if n%i==0:
        a=min(a,i+n//i-2)
print(a)
### BUGGY CODE ###

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

n_li = sorted(li)


dup = [x for x in set(li) if li.count(x) > 1]

if not dup:
    print('YES')
else:
    print('NO')

### FIXED CODE ###

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

is_break = False
seen = set()
for a in A:
    if a not in seen:
        seen.add(a)
    else:
        is_break=True
        break

if is_break:
    print('NO')
else:
    print('YES')
### BUGGY CODE ###

N, M, Q = map(int,input().split())
Mlist = [input().split() for l in range(M)]
Qlist = [input().split() for l in range(Q)]
Mlist.sort()
for i in range(len(Mlist)):
    for j in range(len(Mlist[i])):
        Mlist[i][j] = int(Mlist[i][j])
for i in range(len(Qlist)):
    for j in range(len(Qlist[i])):
        Qlist[i][j] = int(Qlist[i][j])
p = [[0 for i in range(N)] for j in range(N)]
q = 0
r = [[0 for i in range(N)] for j in range(N)]
def count(k,l):
    s = k
    j = l
    p[s-1][j-1] = p[s-1][j-1] + 1
for i in range(len(Mlist)):
    count(Mlist[i][0],Mlist[i][1])
for i in range(N):
    for j in range(N):
        for k in range(j+1):
            r[i][j] = r[i][j] + p[i][k]
for i in range(len(Qlist)):
    q = 0
    for j in range(Qlist[i][0],Qlist[i][1]+1):
        q = q + r[j-1][Qlist[i][1]-1]
    print(q)

### FIXED CODE ###

N, M, Q = map(int,input().split())
Mlist = []
for i in range(M):
    array = list(map(int, input().strip().split()))
    Mlist.append(array)
Qlist = []
for i in range(Q):
    array = list(map(int, input().strip().split()))
    Qlist.append(array)
p = [[0 for i in range(N)] for j in range(N)]
q = 0
r = [[0 for i in range(N)] for j in range(N)]
def count(k,l):
    s = k
    j = l
    p[s-1][j-1] = p[s-1][j-1] + 1
for i in range(len(Mlist)):
    count(Mlist[i][0],Mlist[i][1])
for i in range(N):
    for j in range(N):
        if i == 0:
            r[i][j] = p[i][j] + r[i][j-1]      
        elif j == 0:
            r[i][j] = p[i][j] +r[i-1][j]
        else:
            r[i][j] = r[i-1][j] + r[i][j-1] - r[i-1][j-1] + p[i][j]
for i in range(len(Qlist)):
    q = 0
    if Qlist[i][0] == 1:
        b = r[Qlist[i][1]-1][Qlist[i][1]-1]
    else:
        b = r[Qlist[i][1]-1][Qlist[i][1]-1] - r[Qlist[i][0]-2][Qlist[i][1]-1]
    print(b)
### BUGGY CODE ###

import sys
#input = sys.stdin.readline

def chr2ord(c):
  return ord(c) - ord('a')

N = int(input())
S = []
for i in range(N):
  wk = list(input())
  S.append(wk)

ANS = 0
# A=0として、0<=B<Nをためす。
# こたえはN倍。
for b in range(N):
  cnt = 0
  B = [['X' for _ in range(N)] for _ in range(N)]
  for i in range(N):
    for j in range(N):
      B[i][j] = S[i][(j+b)%N]
  #print(B)
  for i in range(N):
    for j in range(N):
      if B[i][j] == B[j][i]:
        cnt += 1
  
  if cnt == N*N:
    ANS += N
print(ANS)

### FIXED CODE ###

#import sys
#input = sys.stdin.readline

def chr2ord(c):
  return ord(c) - ord('a')

N = int(input())
S = []
for i in range(N):
  wk = list(input())
  S.append(wk)

ANS = 0
# A=0として、0<=B<Nをためす。
# こたえはN倍。
for b in range(N):
  cnt = 0
  B = [['X' for _ in range(N)] for _ in range(N)]
  for i in range(N):
    for j in range(N):
      B[i][j] = S[i][(j+b)%N]
  #print(B)
  for i in range(N):
    for j in range(N):
      if B[i][j] == B[j][i]:
        cnt += 1
  
  if cnt == N*N:
    ANS += N
print(ANS)
### BUGGY CODE ###

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

# 足場 i にたどり着くまでのコストの総和の最小値
dp = [1e14] * n

dp[0] = 0
upper = min(k, n)
for i in range(1, upper):
    dp[i] = abs(hs[0] - hs[i])

if n > k:
    for i in range(k, n):
        min_cost = 1e9
        for j in range(1, k + 1):
            min_cost = min(min_cost, abs(hs[i] - hs[i - j]) + dp[i - j])
        dp[i] = min_cost

print(dp[n - 1])

### FIXED CODE ###

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

dp = [0] * n

for i in range(1, n):
    costs = []
    for j in range(1, k + 1):
        if i - j >= 0:
            cost = dp[i - j] + abs(hs[i] - hs[i - j])
            costs.append(cost)
    dp[i] = min(costs)

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

from collections import defaultdict
from heapq import heappush, heappop

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    dist = {v: INF for v in V}
    heap = []
    heappush(heap, (0, s))
    while heap:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]:
            continue
        dist[v] = d
        for u, c in E[v].items():
            temp = d + c
            if temp < dist[u]:
                heappush(heap, (temp, u))
    return dist

MON = 10**15
n, m, s, t = map(int, input().split())
V = list(range(1, n+1))
E1, E2 = defaultdict(dict), defaultdict(dict)
for _ in range(m):
    u, v, a, b = map(int, input().split())
    E1[u][v] = a
    E1[v][u] = a
    E2[u][v] = b
    E2[v][u] = b
ans = [MON]*n
dist_yen = dijkstra(V, E1, s)
dist_snuuk = dijkstra(V, E2, t)
temp = float('inf')
for v in V[::-1]:
    temp = min(temp, dist_yen[v] + dist_snuuk[v])
    ans[v-1] -= temp
print(*ans, sep='\n')

### FIXED CODE ###

from collections import defaultdict
from heapq import heappush, heappop

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    dist = {v: INF for v in V}
    heap = []
    dist[s] = 0; rem = len(V);
    heappush(heap, (0, s))
    while heap and rem:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]: continue
        rem -= 1;
        for u, c in E[v].items():
            temp = d + c
            if temp < dist[u]:
                dist[u] = temp
                heappush(heap, (temp, u))
    return dist

MON = 10**15
n, m, s, t = map(int, input().split())
V = list(range(1, n+1))
E1, E2 = {v: {} for v in V}, {v: {} for v in V}
for _ in range(m):
    u, v, a, b = map(int, input().split())
    E1[u][v] = a
    E1[v][u] = a
    E2[u][v] = b
    E2[v][u] = b
ans = [MON]*n
dist_yen = dijkstra(V, E1, s)
dist_snuuk = dijkstra(V, E2, t)
temp = float('inf')
for v in V[::-1]:
    temp = min(temp, dist_yen[v] + dist_snuuk[v])
    ans[v-1] -= temp
print(*ans, sep='\n')
### BUGGY CODE ###

def main():
    # rankを出力、modを使って実装
    def matrix_rank(a):
        n, m = len(a), len(a[0])
        M = [[j for j in i] for i in a]
        rank = min(n, m)
        i, j = 0, 0
        while i < n and j < m:
            if M[i][j] == 0:
                for k in range(i+1, n):
                    if M[k][j] != 0:
                        for l in range(j, m):
                            M[i][l] ^= M[k][l]
                        break
                else:
                    j += 1
                    continue
            M2 = M[i]
            for k in range(i+1, n):
                M3 = M[k][j]
                M[k][j] = 0
                for l in range(j+1, m):
                    M[k][l] ^= M3*M2[l]
            i += 1
            j += 1
        return i

    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        s = input()
        r = 0
        m = []
        m2 = []
        for i in range(n-1, -1, -1):
            ai = a[i]
            b = [[(ai >> j) & 1 for j in range(64)]]
            newr = matrix_rank(m+b)
            if s[i] == "0":
                if newr > r:
                    r += 1
                    m += b
                    m2 += [ai]
            else:
                if newr > r:
                    print(1)
                    break
        else:
            print(0)


main()

### FIXED CODE ###

def main():
    t = int(input())
    for _ in range(t):
        nn, aa, s, r, mm = int(input()), list(map(int, input().split())), input(), 0, []
        for ii in range(nn-1, -1, -1):
            ai = aa[ii]
            mm += [[(ai >> jj) & 1 for jj in range(64)]]
            n, i, j = r+1, 0, 0
            while i < n and j < 64:
                if mm[i][j] == 1 and mm[n-1][j] == 0:
                    i += 1
                    j += 1
                    continue
                if mm[i][j] == 0:
                    if mm[n-1][j] != 0:
                        for l in range(j, 64):
                            mm[i][l] ^= mm[n-1][l]
                    else:
                        j += 1
                        continue
                M2 = mm[i]
                for k in range(i+1, n):
                    M3 = mm[k][j]
                    mm[k][j] = 0
                    for l in range(j+1, 64):
                        mm[k][l] ^= M3*M2[l]
                i += 1
                j += 1
    
            if s[ii] == "0":
                if i > r:
                    r += 1
                else:
                    mm.pop()
            else:
                mm.pop()
                if i > r:
                    print(1)
                    break
        else:
            print(0)


main()
### BUGGY CODE ###

# D - Ki

import queue

N, Q = map(int, input().split())

children = [[] for _ in range(N)]
points = [0] * N

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

for _ in range(Q):
    p, x = map(int, input().split())
    points[p - 1] += x

q = queue.Queue()
q.put(0)
visited = [False] * N
while not q.empty():
    key = q.get()
    for child in children[key]:
        if visited[child] == True:
            continue
        points[child] += points[key]
        visited[key] = True
        q.put(child)

print(*points)


### FIXED CODE ###

# D - Ki

N, Q = map(int, input().split())

children = [[] for _ in range(N)]
points = [0] * N

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

for _ in range(Q):
    p, x = map(int, input().split())
    points[p - 1] += x

stack = []
stack.append(0)
visited = [False] * N
while stack:
    key = stack.pop()
    for child in children[key]:
        if visited[child] == True:
            continue
        points[child] += points[key]
        visited[key] = True
        stack.append(child)

print(*points)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    N, M = LMIIS()
    dp = [0] * (M+1)

    DV = [[0] for _ in range(M+1)]

    for _ in range(N):
        A,B = LMIIS()
        if A > M:
            continue
        DV[A].append(B)

    for l in DV:
        l.sort()
    ans = 0
    tmpL = []
    for l in DV:

        tmpL += l
        tmpL.sort()
        ans += tmpL.pop()
    print(ans)
        
        



    pass
if __name__ == '__main__':
    main()

### FIXED CODE ###


import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    N, M = LMIIS()
    dp = [0] * (M+1)

    DV = [[0] for _ in range(M+1)]

    for _ in range(N):
        A,B = LMIIS()
        if A > M:
            continue
        DV[A].append(B)


    from heapq import heappush, heappop
    ans = 0
    h = []
    for l in DV:
        for item in l:
            heappush(h,-item)
        ans -= heappop(h)
    print(ans)
        
        



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

n1 = int(input())
l1 = [int(x) for x in input().split()]
l2 = [""] * n1

for i, v in enumerate(l1):
  l2[v -1] = i + 1

ans = ""
for i in l2:
  ans = ans + str(i) + " "
print(ans[:-1])

### FIXED CODE ###

n1 = int(input())
l1 = [int(x) for x in input().split()]
l2 = [""] * n1

for i, v in enumerate(l1):
  l2[v -1] = i + 1

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

import sys

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

if a == 0:
    print(0)
    sys.exit()

ans = 0
while n > 0:
    if n - a <= 0:
        for i in range(a):
            ans += 1
            n -= 1
            if n <= 0: break
    else:
        ans += a
        n -= a
    n -= b
    
print(ans)

### FIXED CODE ###

import sys

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

if a == 0:
    print(0)
    sys.exit()

syou = n // (a+b)
amari = n % (a+b)

if amari <= a:
    print(syou*a + amari)
else:
    print(syou*a + a)
### BUGGY CODE ###

H,W,D=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
mapper={}
for i in range(H):
    for j in range(W):
        mapper[A[i][j]]=(i,j)
Q=int(input().strip())
L=[]
R=[]
for i in range(Q):
    cst=0
    l,r=map(int,input().split())
    L.append(l)
    R.append(r)
    pos=mapper[l]
    while l!=r:
        l+=D
        npos=mapper[l]
        cst+=abs(pos[0]-npos[0])+abs(pos[1]-npos[1])
        pos=npos
    print(cst)

### FIXED CODE ###

# 解説読後
import sys
sys.setrecursionlimit(100000)
H,W,D=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
mapper={}
for i in range(H):
    for j in range(W):
        mapper[A[i][j]]=(i,j)

Q=int(input().strip())
dist=[0]*(H*W+1)
for i in range(1,H*W+1):
    if i<=D:
        dist[i] = 0
    else:
        pos1=mapper[i]
        pos2=mapper[i-D]
        dist[i]=dist[i-D]+abs(pos1[0]-pos2[0])+abs(pos1[1]-pos2[1])
for i in range(Q):
    cst=0
    l,r=map(int,input().split())
    print(dist[r]-dist[l])
### BUGGY CODE ###

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

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(0,n):
  b[a[i]-1]=i+1
ans=''
for i in range(n):
  ans+=str(b[i])+' '
print(ans)
### BUGGY CODE ###

x = int(input())
flag = 0
for a in range(x+1):
  if flag == 1:
    break
  for b in range(x+1):
    if a**5 - b**5 == x:
      print(a, b)
      flag += 1
      break
    elif a**5 + b**5 == x:
      print(a, -1*b)
      flag += 1
      break

### FIXED CODE ###

x = int(input())
def make_divisors(n):
    divisors = [0]
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
    return divisors

flag = 0
a_s = 0
if x == 1:
  print(1, 0)
else:
  l = make_divisors(x)
  for a in range(a_s, x+1):
    if flag == 1:
      break
    for b_a in l:
      if b_a <= a:
        b = a - b_a
        if a**5 - b**5 == x or a**5 + b**5 == x:
          flag += 1
          print(a, b)
          break
      if b_a > a:
        b = b_a - a
        if a**5 + b**5 == x:
          flag += 1
          print(a, -b)
          break
### BUGGY CODE ###

from bisect import bisect_right

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for i in S:
  a=A[0]
  j = bisect_right(A,a)
  B.append(j)
  del A[:j]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))


### FIXED CODE ###

from collections import Counter

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

dic = dict(Counter(A))
ls = list(dic.items())
ls.sort(key=lambda x: x[1])

i=0
ans=0
res=len(ls)
while 1:
  if res <= K or i >= N:
    break
  ans += ls[i][1]
  i+=1
  res-=1
print(ans)
### BUGGY CODE ###

from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce


def input():
    return sys.stdin.readline().strip()


def iinput():
    return int(input())


def tinput():
    return input().split()


def rinput():
    return map(int, tinput())


def rlinput():
    return list(rinput())


mod = int(1e9)+7


def factors(n):
    return set(reduce(list.__add__,
                      ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))


# ----------------------------------------------------


def solve(item, weight):
    if weight < 0:
        return -9999999999999999999
    if item == 0:
        return 0

    # print(item, weight)
    if dp[item][weight] != -1:
        # print('hello')
        return dp[item][weight]

    solution1 = solve(item-1, weight)
    solution2 = solve(item-1, weight-w_v[item-1][0])+w_v[item-1][1]
    # print(solution1,solution2)
    dp[item][weight] = max(solution1, solution2)
    # print(dp[item][weight])
    return dp[item][weight]


if __name__ == "__main__":
    n, w = rinput()
    w_v = []
    for i in range(n):
        w_v.append(rlinput())
    dp = [[-1 for i in range(w+1)]for j in range(n+1)]
    print(solve(n, w))
    # print(dp)


### FIXED CODE ###

from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce


def input():
    return sys.stdin.readline().strip()


def iinput():
    return int(input())


def tinput():
    return input().split()


def rinput():
    return map(int, tinput())


def rlinput():
    return list(rinput())


mod = int(1e9)+7


def factors(n):
    return set(reduce(list.__add__,
                      ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))


# ----------------------------------------------------

if __name__ == "__main__":
    n, weigh = rinput()
    w, v = [], []
    for i in range(n):
        wi, vi = rinput()
        w.append(wi)
        v.append(vi)

    mxv = sum(v)
    dp = [[99999999999 for i in range(mxv+1)]for j in range(n+1)]
    for i in range(n+1):
        dp[i][0]=0
    # print(w_v)
    for item in range(1, n+1):
        for val in range(1, mxv+1):
            if v[item-1] <= val:
                dp[item][val] = min(
                    dp[item-1][val-v[item-1]]+w[item-1], dp[item-1][val])
            else:
                dp[item][val] = dp[item-1][val]
    # print(dp)
    ans=0
    for i in range(mxv+1):
        if dp[n][i]<=weigh:
            ans=i
    print(ans)
### BUGGY CODE ###

H, N = map(int, input().split())
da, cost = [0] * N, [0] * N
for i in range(N):
    da[i],cost[i]=map(int, input().split())
max_da=max(da)  
dp = [float("inf") for i in range(H*1000)]
dp[0]=0

for j in range(N):
    for i in range(H*1000):
        if i-da[j]>=0:
            dp[i]=min(dp[i],dp[i-da[j]]+cost[j])

ans=0
for i in range(H,H*1000):
    if dp[i]!=float("inf"):
        ans=dp[i]
        break
print(ans)

### FIXED CODE ###

H, N = map(int, input().split())
da, cost = [0] * N, [0] * N
for i in range(N):
    da[i],cost[i]=map(int, input().split())

max_da=max(da) 
dp = [float("inf") for i in range(H+max_da)]
dp[0]=0

for i in range(H+max_da):
    for j in range(N):
        if i-da[j]>=0:
            dp[i]=min(dp[i],dp[i-da[j]]+cost[j])

print(min(dp[H:]))