Problem:


Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
h,n=list(map(int,input().split()))
#a=list(map(int,input().split()))
ma=[]
for _ in range(n):
    tmp=list(map(int,input().split()))
    ma.append(tmp)
ma=sorted(ma,key=lambda x:x[1])
ma.reverse()
dp=[[float("inf") for _ in range(h+1)] for _ in range(n)]
for i in range(n):
    dp[i][0]=0
for i in range(1,h+1):
    dp[0][i]=math.ceil(i/ma[0][0])*ma[0][1]
#print(np.array(ma))
for i in range(1,n):
    #print("number:{},magic:{}".format(i,ma[i]))
    for j in range(1,h+1):
        if j<ma[i][0]:
            cal=ma[i][1]
        else:
            #print("sansyo before")
            cal=ma[i][1]+dp[i][j-ma[i][0]]
        #print("j:{},cal:{}".format(j,cal))
        dp[i][j]=min(dp[i-1][j],cal)
    #print()


print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
Q=[None for i in range(N)]
for i in range(N):
	A,B=map(int,input().split())
	C=A/B
	Q[i]=[A,B]
	
P=[1+H*N*100001 for i in range(H+1)]
P[0]=0
for k in range(1,H+1):
	for i in range(N):
		P[k]=min(P[max(0,k-Q[i][0])]+Q[i][1],P[k])
print(P[H])
#print(P)

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(N)]
dp = [float('inf')] * (H + 1)
dp[0] = 0
for h in range(H + 1):
    for a, b in AB:
        h_new = min(h + a, H)
        dp[h_new] = min(dp[h] + b, dp[h_new])
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]

dp = [[10**10, 0] for i in range(H+1)]
dp[0][0] = 0
# i 減らすのに最小で必要なMP

for i in range(1, H+1):
    if dp[i-1][1] > 0:
        dp[i][0] = dp[i-1][0]
        dp[i][1] = dp[i-1][1] - 1
    else:
        for A, B in AB:
            if A > i:
                if dp[i][0] > B:
                    dp[i][0] = B
                    dp[i][1] = A - i
            else:
                if dp[i][0] > dp[i - A][0] + B:
                    dp[i][0] = dp[i-A][0] + B
                    dp[i][1] = dp[i-A][1] + A - i

print(dp[H][0])

Submission status: Time Limit Exceeded

Solution code:
h, n = list(map(int, input().split()))

hp_and_magic = [list(map(int, input().split())) for _ in range(n)]

arr = [10 ** 9] * (h+1)
# arr[k] := hpがkの敵を倒すのに必要な最小魔力
arr[0] = 0
arr[1] = min([ele[1] for ele in hp_and_magic])

for i in range(2, h+1):
    # arr[i] = 10 ** 9
    for ele in hp_and_magic:
        if i-ele[0] >= 0:
            arr[i] = min(arr[i], arr[i-ele[0]] + ele[1])
        else:
            arr[i] = min(arr[i], ele[1])

print(arr[h])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for ii in range(N)]
C = [10**10]*(H+1)
C[0] = 0
for ii in range(H+1):
    for jj in AB:
        if ii>=jj[0]:
            C[ii] = min(C[ii], C[ii-jj[0]]+jj[1])
        else:
            C[ii] = min(C[ii], jj[1])
print(C[H])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

### TESTCASE
test = ""
#test = \
"""
9 3
8 3
4 2
2 1
ans 4
"""

"""
5 2
4 4
2 3
ans 7
"""

"""
100 6
1 1
2 3
3 9
4 27
5 81
6 243
ans 100
"""

"""
9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461
ans 139815
"""
############
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
####### MAIN
        
h, n = map(int, input2().split())    
ab = [tuple(map(int, input2().split())) for _ in range(n)]

dp = [0] * (h + 1)

for i in range(1, h+1):
    kouho = [10**15]
    for c in ab:     
        kouho.append(dp[max(i-c[0], 0)] + c[1])
    dp[i] = min(kouho)
    #print(i, kouho)
        
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# dp[i+1][j] i番目までの魔法を使ってモンスターへの攻撃jにする魔力の最小値
# (j=0の時まだ攻撃していない)
H, N = lr()
INF = 10 ** 10
dp = [INF] * (H+10**4+10)
dp[0] = 0
for i in range(N):
    a, b = lr()
    for j in range(a, H+a+1):
        if dp[j-a] == INF:
            continue
        dp[j] = min(dp[j], dp[j-a] + b)

answer = min(dp[H:])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
mg = [list(map(int,input().split())) for i in range(n)]
DP = [[10**18 for i in range(h+1)] for j in range(n+1)]
for i in range(n+1):
  DP[i][0] = 0
for i in range(1,n+1):
  for j in range(1,h+1):
    DP[i][j] = min(DP[i][j],DP[i-1][j])
    DP[i][j] = min(DP[i][j],DP[i][max(0,j-mg[i-1][0])]+mg[i-1][1])
print(DP[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
M=[list(map(int,input().split())) for _ in range(n)]
res=[0]
f=0
for j in range(1,h+1):
    ff=[]
    for i in range(n):

        if 0>=j-M[i][0]:
            ind=0
        else:
            ind=j-M[i][0]
        kari=res[ind]+M[i][1]
        
        ff.append(kari)
    res.append(min(ff))
print(res[-1])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int,input().split())
A = []
B = []
dp = [None for i in range(2*10**4)]
dp[0] = 0
for i in range(N):
    tmp_A, tmp_B = map(int,input().split())
    A.append(tmp_A)
    B.append(tmp_B)
for i in range(2*10**4):
    for j in range(N):
        if i-A[j]<0:
            pass
        elif dp[i-A[j]]!=None:
            if dp[i]==None:
                dp[i]=dp[i-A[j]]+B[j]
            else:
                dp[i]=min(dp[i],dp[i-A[j]]+B[j])

ans = 10**16
for i in range(H,2*10**4):
    if dp[i]!=None:
        if dp[i]<ans:
            ans = dp[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a, b = x[0]
dp = [-(-i // a) * b for i in range(h + 1)]
for j in range(1, n):
    a, b = x[j]
    for i in range(h + 1):
        if i >= a:
            dp[i] = min(dp[i], dp[i - a] + b)
        else:
            dp[i] = min(dp[i], b)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

def main():
    H,N = LI()
    M = []
    for i in range(N):
        M.append(LI())

    dp = {0:H}
    h = []
    heapq.heappush(h,0)
    nxt = -1

    for i in range(10**9):
        if dp.get(i,-1) == -1:
            continue
        if dp[i] == 0:
            print(i)
            return
        for j in range(N):
            dp[i+M[j][1]] = min(dp.get(i+M[j][1],H),max(0,dp[i]-M[j][0]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import count

H, N = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]

dp = [10 ** 9 for _ in range(H + 1)]
dp[0] = 0
for hp in range(1, H + 1):
    # print(dp)
    for A, B in ABs:
        if A <= hp:
            dp[hp] = min(dp[hp], dp[hp - A] + B)
        else:
            dp[hp] = min(dp[hp], B)

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
h,n= map(int, input().split())
m = {}
l = {}
for i in range(n):
  a,b = map(int, input().split())
  if b in m:
    if m[b] < a:
      m[b] = a
      l[b] = a
  else:
    m[b] = a
    l[b] = a
i = 1
d = 0
while(d<h):
  d = 0
  if i in l:
    d = l[i]
  else:
    l[i] = 0
  for j in m.keys():
    if i+j in l:
      l[i+j] = max(l[i+j],l[i]+m[j])
    else:
      l[i+j] = l[i]+m[j]
  i += 1
print(i-1)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys

H, N = list(map(int,input().split()))
AB = [list(map(int,input().split())) for i in range(N)]


cost = [100000000 for _ in range(H)]
min_cost = 100000000

cost[0] = 0

attack_num = 0
for k in range(H):
    for j in range(N):
        if k + AB[j][0] >= H:
            if min_cost > cost[k] + AB[j][1] :
                min_cost = cost[k] + AB[j][1]
        else:
            if cost[k+AB[j][0]] > cost[k] + AB[j][1]:
                cost[k+AB[j][0]] = cost[k] + AB[j][1]
print(min_cost)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(500000)

def input() -> str:
    return sys.stdin.readline().rstrip()

def f(i, j):
    if j <= 0:
        return 0
    if i < 0 and j > 0:
        return float("inf")
    if dp.get((i, j)) is None:
        dp[(i, j)] = min([f(i - 1, j - k * ab[i][0]) + k * ab[i][1] for k in range((h - 1) // ab[i][0] + 2)])
    return dp[(i, j)]

h, n = map(int, input().split())
ab = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x: -x[0])
dp = dict()
print(f(n - 1, h))

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)



def acinput():
    return list(map(int, input().split(" ")))



directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def search(x):
    global s,dp
    if x<=0:
        return 0
    if dp[x]>=0:
        return dp[x]
    
    tmp=10**9
    for h,m in s:
        a=search(x-h)
        tmp=min(tmp,a+m)
        
    dp[x]=tmp

    return tmp


H,N=acinput()
s=[]
hp=[]
for i in range(N):
    tmp = acinput()
    #tmp.append(tmp[0]/tmp[1])
    s.append(tmp)
    #hp.append(tmp[0])



#s=sorted(s,reverse=True,key= lambda x:x[2])

dp=[-1]*(H+H)
dp[0]=0

print(search(H))

Submission status: Time Limit Exceeded

Solution code:
#  --*-coding:utf-8-*--

MAX = 10**9

H,N = map(int, input().split())
AB = [map(int, input().split()) for _ in range(N)]

X = [MAX]*(H+1)
X[0] = 0

for a, b in AB:
    for j in range(1, H+1):
        x = (X[j-a] if j>=a else 0) + b
        
        if X[j] > x:
            X[j] = x

print(X[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# dp[i+1][j] i番目までの魔法を使ってモンスターへの攻撃jにする魔力の最小値
# (j=0の時まだ攻撃していない)
H, N = lr()
INF = 10 ** 10
dp = [[INF] * (H+10**4+10) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
    a, b = lr()
    dp[i+1] = dp[i]
    for j in range(1, H+a+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i+1][j-a] + b)

answer = min(dp[N][H:])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 6)
Hitpoint, N = map(int, input().split())
magic_damage = []
magic_point = []
dp = [[0] * (N + 1) for _ in range(N)]
for _ in range(N):
    A, B = map(int ,input().split())
    magic_damage.append(A)
    magic_point.append(B)

@lru_cache(maxsize=None)
def solve(i , now_HP):
    if now_HP <= 0:
        retval = 0
    elif i == N:
        retval = float('inf')
    else:
        retval = min(solve(i + 1 , now_HP) , solve(i, now_HP - magic_damage[i]) + magic_point[i])

    return retval
ans = solve(0, Hitpoint)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
dp = defaultdict(lambda: 10**10)
h,n = [int(j) for j in input().split()]
l = [] 
for i in range(n):
    a,b = [int(j) for j in input().split()]
    l.append((b,a))
l.sort()
# print(l)
for i in range(n):
    for j in range(1,l[0][1]+1):
        dp[i, j] = l[0][0]
for i in range(n):
    for j in range(1, h+1):
        # print(i,j, dp[i-1, j])
        dp[i,j] = dp[i-1, j]
        if j>l[i][1]:
            dp[i,j] = min(dp[i-1, j], dp[i, j-l[i][1]]+l[i][0])
        else:
            dp[i,j] = min(dp[i-1, j], l[i][0])

print(dp[n-1, h])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
H, N= map(int,input().split())
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)

dp = defaultdict(lambda : 10**10)
dp[0] = 0

for i in range(0,N):
    ndp = dp.copy()
    a = A[i]
    b = B[i]
    for h,m in dp.items():
        cnt = 0
        while True:
            damage = h+a*cnt
            if damage >= H:
                ndp[H] = min(ndp[H], ndp[h] + b*cnt)
                break
            ndp[damage] = min(ndp[damage], ndp[h] + b*cnt)
            cnt += 1
    dp = ndp

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]

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

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

for n in range(1,N+1):
    for h in range(H+1):
        if h >= AB[n-1][0]:
            dp[h][n] = min(dp[h-AB[n-1][0]][n]+AB[n-1][1] , dp[h][n-1])
        else:
            dp[h][n] = dp[h][n-1]

# for i in range(H+1):
#     print(*dp[i])
        
ans = dp[H][N]
for n in range(N):
    for h in range(H):
        if (H-AB[n][0])<=h:
            ans = min(ans,dp[h][N]+AB[n][1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
H,N=map(int,input().split())
AB=[[0,0]]
for i in range(N):
    AB.append(list(map(int,input().split())))

INF=10**9

dp=[[INF for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0]=0


for i in range(1,N+1):
    for j in range(1,H+1):
        dp[i][j]=min(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])
        #print(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])
        


print(dp[N][H])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for i in range(N)]

dp = [(i + M[0][0] - 1) // M[0][0] * M[0][1] for i in range(H + 1)]
for i in range(N):
    Ai, Bi = M[i]
    ndp = [0] * (H + 1)
    if Ai >= H:
        ndp[0:H + 1] = [min(dp[j], Bi) for j in range(H + 1)]
    else:
        ndp[0:Ai + 1] = [min(dp[j], Bi) for j in range(Ai + 1)]
        for j in range(Ai + 1, H + 1):
            ndp[j] = min(dp[j], ndp[j - Ai] + Bi)
    dp = ndp
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    H, N = map(int, input().split())
    AB = [tuple(map(int, input().split())) for _ in range(N)]

    dp = [10**9+1] * (H+1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, H+1):
            if i - a >= 0:
                v = dp[i-a] + b
            else:
                v = b
            if dp[i] > v:
                dp[i] = v

    print(dp[H])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding=utf-8
# Date: 2020-01-08
# Author: lee215


get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]


H, N = gets()
AB = getss(N)
AB.sort(key=lambda a_b: -a_b[0] * 1.0 / a_b[1])

a0, b0 = AB[0]
dp = [0] + [float('inf')] * (H + a0)

cur = 0
while cur <= H + a0:
    dp[cur] = cur / a0 * b0
    cur += a0


for a, b in AB[1:]:
    for i in xrange(a, H + a0 + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
print min(dp[H:])

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
ls = [list(map(int,input().split())) for _ in range(n)]


ls = sorted(ls,key = lambda x: min(x[0],h)/x[1], reverse = True)
ans = 0

t = ls[0]
ans += (h//t[0])*t[1]
h %= t[0]

while h>0:
  ls = sorted(ls,key = lambda x: min(x[0],h)/x[1], reverse = True)
  t = ls[0]
  ans += (h//t[0])*t[1]
  h %= t[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
a,b=map(int,input().split())
list1=[]
m=0
for i in range(b):
  list1.append(list(map(int,input().split())))
list2=sorted(list1,key=lambda x:-(x[0]/x[1]))
while a>0:
  for i in list2:
    if i[0]<a:
      a-=i[0]
      m+=i[1]
      break
print(m)

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
X = []
for _ in range(N):
    a,b = map(int,input().split())
    X.append([a,b])
dp = [10000000000000 for i in range(H+1)] 
dp[0] = 0
for i in range(1,H+1):
    for j in range(N):
        a = X[j][0]
        b = X[j][1]
        dp[i] = min(dp[i],dp[max(0,i-a)] + b)
#print(dp)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H,n = map(int,input().split())
A = [0 for _ in range(n)]
B = [0 for _ in range(n)]
min_eff = 9999.
for i in range(n):
    A[i],B[i] = map(int,input().split())
    min_eff = min(min_eff,A[i]/B[i])

dp = np.zeros((n+1,H+1))+H/min_eff+1
dp[:,0] = 0

for i in range(n):
    for h in range(H+1):
        idx = max(0,h-A[i])
        dp[i+1][h] = min(dp[i+1][idx] + B[i],dp[i][h])

print(int(dp[n,H]))

Submission status: Time Limit Exceeded

Solution code:
import math


memo = dict()


def solve(i, A, B, hp):
    if hp <= 0:
        return 0
    if i < 0:
        return float("infinity")

    key = (i, hp)
    if key in memo:
        return memo[key]

    a = A[i]
    b = B[i]
    res = []
    for n in range(math.ceil(hp / a) + 1):
        cand = b*n + solve(i-1, A, B, hp-a*n)
        res.append(cand)
    ans = min(res)
    memo[key] = ans
    return ans


H, N = [int(x) for x in input().split()]
A = []
B = []
for _ in range(N):
    a, b = [int(x) for x in input().split()]
    A.append(a)
    B.append(b)

ans = solve(N-1, A, B, H)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]

dp=[[0]*(H+1) for _ in range(N+1)]
dp[0]=[float("inf")]*(H+1)
dp[0][0]=0

for i in range(1,N+1):
  for j in range(1,H+1):
    if j-AB[i-1][0]>=0:
      dp[i][j]=min(dp[i][j-AB[i-1][0]]+AB[i-1][1],dp[i-1][j])
    else:
      dp[i][j]=min(AB[i-1][1],dp[i-1][j])
      
print(dp[N][H])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
dp = [0] * (H+1)
A, B = map(int, input().split())
for j in range(1, H+1):
    if j>A:
        dp[j] = max(dp[j-A] + B, dp[j])
    else:
        dp[j] = max(dp[0] + B, dp[j])
for i in range(1,N):
    A, B = map(int, input().split())
    for j in range(1, H+1):
        if j>A:
            dp[j] = min(dp[j-A] + B, dp[j])
        else:
            dp[j] = min(dp[0] + B, dp[j])
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())

ab_list = [0]*n

for i in range(n):
    ab_list[i] = list(map(int, input().split()))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H,N = list(map(int, input().split()))
As = []
Bs = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    As.append(a)
    Bs.append(b)

dp = np.full((N+1, H+1), np.inf)
dp[:, 0] = 0

for r in range(1,N+1):
    for c in range(1,H+1):
        if c >= As[r-1]:
            dp[r, c] = min(dp[r-1, c], dp[r, c-As[r-1]] + Bs[r-1])
        else:
            dp[r, c] = min(dp[r-1, c], Bs[r-1])

# print(dp)
print(int(dp[-1, -1]))

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split(" "))

pairs = []
ratios = []
a_list = []
inputs = []
for i in range(n):
    a, b = map(int, input().split(" "))
    a_list.append(a)
    #ratio = a/b
    #ratios.append(ratio)
    #pairs.append([a, b, ratio])
    inputs.append([a, b])

#pairs = sorted(pairs, key=lambda x: -x[2])
#print(pairs)

max_a = max(a_list)

min_val = -1-max_a
costs = {}
for i in range(min_val, h+1):
    costs[i] = None
costs[h] = 0
#costs = [None for i in range(h)] + [0]

for val in range(h, min_val-1, -1):
    for a, b in inputs:
        if val+a > h:
            continue
        if costs[val+a] is None:
            continue

        if costs[val] is not None:
            costs[val] = min(costs[val+a]+b, costs[val])
        else:
            costs[val] = costs[val+a]+b

out = min([costs[val] for val in range(min_val, 1) if costs[val] is not None])
#print(costs)
print(out)

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]

AB.append((0, 1))

dp = [float("inf")] * (10 ** 4 * 2 + 1)
dp[0] = 0
for i in range(1, len(dp)):
    for a, b in AB:
        if i - a >= 0:
            dp[i] = min(dp[i], dp[i - a] + b)

ans = min(dp[H:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#H = int(input())
H,N = list(map(int,input().split()))
#H = sorted(list(map(int,input().split())))
AB = [list(map(int,input().split())) for i in range(N)]
dp = [[float('inf') for i in range(H)] for j in range(N)]
for i in range(N):
    dp[i][0] = 0
for j in range(H):
    if j-AB[0][0] >= 0:
        dp[0][j] = dp[0][j-AB[0][0]]+AB[0][1]
for i in range(1,N):
    for j in range(H):
        if j-AB[i-1][0] >= 0:
            dp[i][j] = min(dp[i-1][j],dp[i-1][j-AB[i-1][0]]+AB[i-1][1])
        if j-AB[i][0] >= 0:
            dp[i][j] = min(dp[i-1][j],dp[i][j-AB[i][0]]+AB[i][1])

ans = float('inf')
for i in range(N):
    for j in range(max(0,H-AB[i][0]),H):
        if dp[-1][j] != float('inf') and j+AB[i][0] >= H:
            ans = min(ans,dp[-1][j]+AB[i][1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, threading
sys.setrecursionlimit(10 ** 6)  # max depth of recursion
threading.stack_size(2 ** 27)
from bisect import bisect_left, bisect_right
INF = 10**10
def main():
    #print = out.append
    ''' Cook your dish here! '''
    h, n = get_list()
    wepons = dict()
    for _ in range(n):
        a,b = get_list()
        if a in wepons:
            wepons[a] = min(b, wepons[a])
        else: wepons[a] = b

    dp = [-1]*(h+1)
    dp[0] = 0
    #print(wepons)
    def calc(n):
        if n<0: return 0
        if dp[n]!=-1: return dp[n]
        #print(n)
        cst = INF
        for wepon in wepons:
            #print(n-wepon)
            cst = min(cst, calc(n-wepon) + wepons[wepon])
        dp[n] = cst
        #print(n, cst)
        return cst
    print(calc(h))


import sys
#rom collections import defaultdict, Counter
#from bisect import bisect_left, bisect_right
#from functools import reduce
#import math
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
threading.Thread(target=main).start()

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)], key = lambda x: x[0])

dp = [0] + [float("inf")]*(H + AB[-1][0])
for j in range(H+1):
    for i in range(N):
        if dp[j] < float("inf"):
            try:
                dp[j + AB[i][0]] = min(dp[j + AB[i][0]], dp[j] + AB[i][1])
            except IndexError:
                pass

print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    dp = [[(10**8) for i in range(H + 1)] for j in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0

    for i in range(1, N + 1):
        for h in range(H + 1):
            dp[i][h] = min(dp[i][max(h - AB[i - 1][0], 0)] +
                           AB[i - 1][1], dp[i - 1][h])
    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
ab = []
for i in range(n):
    ab.append(list(map(int, input().split())))
num = [float("inf")] * (h + 1)
num[0] = 0
for i in range(h):
    mp = []
    for j in range(n):
        if (i + 1) - ab[j][0] < 0:
            mp.append(ab[j][1])
        else:
            mp.append(num[i + 1 - ab[j][0]] + ab[j][1])
    num[i + 1] = min(mp)
print(num[h])

Submission status: Time Limit Exceeded

Solution code:
#H = int(input())
H,N = list(map(int,input().split()))
#H = sorted(list(map(int,input().split())))
AB = [list(map(int,input().split())) for i in range(N)]
dp = [[float('inf') for i in range(H)] for j in range(N)]
for i in range(N):
    dp[i][0] = 0
for j in range(H):
    if j-AB[0][0] >= 0:
        dp[0][j] = dp[0][j-AB[0][0]]+AB[0][1]
for i in range(1,N):
    for j in range(H):
        if j-AB[i-1][0] >= 0:
            dp[i][j] = min(dp[i-1][j],dp[i][j],dp[i-1][j-AB[i-1][0]]+AB[i-1][1])
        if j-AB[i][0] >= 0:
            dp[i][j] = min(dp[i-1][j],dp[i][j],dp[i][j-AB[i][0]]+AB[i][1])
ans = float('inf')
for i in range(N):
    for j in range(max(0,H-AB[i][0]),H):
        if dp[i][j] != float('inf') and j+AB[i][0] >= H:
            ans = min(ans,dp[i][j]+AB[i][1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)

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

AB = []
for i in range(N):
  A, B = map(int, input().split())
  AB.append((A, B))

threshold = sum(a for a, b in AB)
bestA, bestB = max(AB, key=lambda ab: ab[0] / ab[1])

@lru_cache(maxsize=None)
def f(h):
    if h <= 0:
        return 0
    times = (h - threshold) // bestA
    if times > 0:
        return bestB * times + f(h - bestA * times)

    return min(b + f(h - a) for a, b in AB)

print(f(H))

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
magiclist = [0]*N
dp = [0] * (N+1)
for s in range(N+1):
    dp[s] = [10**9] * (H + 1)
    dp[s][0] = 0
for i in range(N):
    magiclist[i] = [0]*2
    damage, mp = map(int, input().split())
    magiclist[i][0], magiclist[i][1] = damage, mp
    for j in range(H + 1):
        if j - damage < 0:
            dp[i + 1][j] = dp[i][j]
        else:
            dp[i + 1][j] = min(dp[i][j], (mp + dp[i + 1][j - damage]))
kouho = dp[N][H]
for h in range(N):
    kouho = min(kouho, min(dp[N][H-magiclist[h][0]:H])+magiclist[h][1])
print(kouho)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

def main():
    h_and_n_s = sys.stdin.readline().strip().split(' ')
    h, n = [int(s) for s in h_and_n_s]
    a = [-1] * n
    b = [-1] * n

    for i in range(n):
        a[i], b[i] = [int(s) for s in sys.stdin.readline().strip().split(' ')]

    dp = [float('inf')] * (h+1)

    for point in range(h+1):
        for i in range(n):
            if point <= a[i]:
                dp[point] = min(dp[point], b[i])
                continue

            dp[point] = min(dp[point], dp[point-a[i]] + b[i])

    print(dp[h])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
A = [0 for _ in range(N)]
B = [0 for _ in range(N)]
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b
MP = [0 for _ in range(H+1)]
tmp = [0 for _ in range(N)]
for h in range(1, H+1):
    for n in range(N):
        if A[n] >= h:
            tmp[n] = B[n]
        else:
            tmp[n] = MP[h-A[n]]+B[n]
    MP[h] = min(tmp)
print(MP[H])

Submission status: Time Limit Exceeded

Solution code:
import collections

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

INF = 10 ** 20

dp = collections.defaultdict(lambda : INF)
dp[0] = 0

magic = []

for i in range(n):
    magic.append(list(map(int, input().split())))



que = [0]
cc = [0]
while que:
    next_que = set()
    for i in que:
        now_dam = i
        for a, b in magic:
            dam = now_dam + a
            dp[dam] = min([dp[dam], dp[now_dam] + b])
            if dam < h:
                next_que.add(dam)
    que = list(next_que)

ans = INF

for k, v in dp.items():
    if k >= h:
        ans = min([v, ans])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]


dp = [0] * (10 ** 8 + 1)
for i in range(1, len(dp)):
    for a, b in AB:
        if i - b >= 0:
            dp[i] = max(dp[i], dp[i - b] + a)
    if dp[i] >= H:
        break

ans = bisect_left(dp[: i + 1], H)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
# mahous = [(a, b) for a, b in list(map(int, input().split()))]

mahous = []
for i in range(N):
    a, b = map(int, input().split())
    mahous.append((a, b))

dp = [float("inf")] * (H + 1)
dp[0] = 0

for h in range(1, H + 1):
    for n in range(N):
        a, b = mahous[n]
        tairyoku = max(0, h - a)
        # if h - a >= 0:
        dp[h] = min(dp[h], dp[tairyoku] + b)
    # print(dp)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H,N = [int(zz) for zz in input().split()]
li = [[int(zz) for zz in input().split()] for _ in range(N)]

dp = [[0]*(H+1) for _ in range(N)]

for j in range(1,H+1):
    if j <= li[0][0]:
        dp[0][j] = li[0][1]
    else:
        dp[0][j] = dp[0][j-li[0][0]]+li[0][1]

for i in range(1,N):
    for j in range(1,H+1):
        if j <= li[i][0]:
            dp[i][j] = min(li[i][1],dp[i-1][j])
        else:
            dp[i][j] = min(dp[i][j-li[i][0]]+li[i][1],dp[i-1][j])

print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import numpy as np


DEBUG = True
dprint = print if DEBUG else lambda *x : x

H, N = [int(str) for str in input().strip().split()]
AB = [[int(str) for str in input().strip().split()] for _ in range(N)]

cosp = [i[0] / i[1] for i in AB]

bestshot = AB[cosp.index(max(cosp))]
rem = H % bestshot[0]

dp = [0]
for h in range(1, H + 1):
    dp.append(min([i[1] + dp[max(0, h - i[0])] for i in AB if AB[0]]))
    
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,n = nm()
v = [nl() for _ in range(n)]
dp = [1<<30]*(h+10020)
dp[0] = 0
for a,b in v:
    for i in range(h+1):
        if dp[i+a] > dp[i] + b:
            dp[i+a] = dp[i] + b
print(min(dp[h:]))