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:
H, N  = map(int, input().split())
# hitpoint H
# waza N
As = [] # damage
Bs = [] # MP comsumptions
for i in range(N):
    a,b = map(int, input().split())
    As.append(a)
    Bs.append(b)

"""
dp[i][j] = i番目までの魔法を見たとき、ダメージがちょうどjになる最小値
"""
INF = 10**10
M = 21000
dp = [[INF]*M for i in range(N+1)]
dp[0][0] = 0
for i,(a,b) in enumerate(zip(As,Bs)):
    dp[i+1][:] = dp[i][:]
    for j in range(H):        
        dp[i+1][j+a] = min(dp[i][j+a], dp[i+1][j]+b)
        # print(i,j,"ab",a,b,"dp",dp[i][j],dp[i][j+1],dp[i][j]+b)
print(min(dp[N][H:]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**10
MAX_LEN = 2*10**4

h, n = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
dp = [INF] * (MAX_LEN * (n + 1))
dp[0] = 0
for i in range(1, n+1):
    v, w = ab[i-1]
    offset = i * MAX_LEN
    offset_before = offset - MAX_LEN
    for j in range(MAX_LEN):
        if j < v:
            dp[offset+j] = dp[offset_before+j]
        elif dp[offset_before+j] > dp[offset+j-v] + w:
            dp[offset+j] = dp[offset+j-v] + w
        else:
            dp[offset+j] = dp[offset_before+j]
ans = min(dp[n*(MAX_LEN) + h:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

max_size = 2*10**4 + 1
h, n = map(int, input().split())
DP = [float('INF')] * max_size
DP[0] = 0

for _ in range(n):
    damage, cost = map(int, input().split())
    for idx in range(damage, max_size):
        DP[idx] = min(DP[idx], DP[idx-damage] + cost)

print(min(DP[h:]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**10
MAX_LEN = 2*10**4

h, n = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
dp = [INF] * (MAX_LEN * (n + 1))
dp[0] = 0
for i in range(1, n+1):
    v, w = ab[i-1]
    for j in range(MAX_LEN):
        if j < v:
            dp[i*MAX_LEN+j] = dp[(i-1)*MAX_LEN+j]
        elif dp[(i-1)*MAX_LEN+j] > dp[i*MAX_LEN + j-v] + w:
            dp[i*MAX_LEN+j] = dp[i*MAX_LEN+j-v] + w
        else:
            dp[i*MAX_LEN+j] = dp[(i-1)*MAX_LEN+j]
ans = min(dp[n*(MAX_LEN) + h:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, n = [int(v) for v in input().split()]

inf = 10 ** 10
magic = []
max_a = 0

for i in range(n):
    ab = [int(v) for v in input().split()]
    max_a = max(ab[0], max_a)
    magic.append(ab)

dp = [0] * (h + max_a)

for i in range(1, h+1):
    v = dp[i]
    if v == inf:
        continue

    a0, b0 = magic[0]
    v = dp[max(i-a0, 0)]+b0
    for (a, b) in magic:
        v = min(v, dp[max(i-a, 0)]+b)
    dp[i] = v

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10000000)

h, n = map(int, input().split())
m_list = []
for i in range(n):
  a, b = map(int, input().split())
  m_list.append((a, b))
  
@lru_cache(maxsize=h+1000)
def dp_attack(h):
  res_list = []
  if h <= 0:
    return 0
  for a, b in m_list:
    base_b = dp_attack(h-a)
    res_list.append(base_b+b)
  return min(res_list)
for i in range(h+100):
  dp_attack(i)
res = dp_attack(h)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    H,N = map(int,input().split())
    # AB = [list(map(int,input().split())) for _ in range(N)]
    A = []
    B = []
    for i in range(N):
        temp = list(map(int,input().split()))
        A.append(temp[0])
        B.append(temp[1])

    A = np.array(A)
    B = np.array(B)

    maxA = max(A)
    INF = 10 ** 4 + 1

    dp = [[INF for j in range(H+maxA+1)] for i in range(N)]
    dp[0][0] = 0
    for j in range(1,H+maxA+1):
        time = (j+A[0]-1)//A[0]
        dp[0][j] = B[0] * time

    for i in range(N-1):
        for j in range(H+maxA+1):
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-A[i+1]]+B[i+1])

    res = min(dp[N-1][H:])
    print(res)





main()

Submission status: Time Limit Exceeded

Solution code:
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 =[0]*(H+1)

for i in range(1,H+1):
    dp = []
    for j in range(len(As)):
        
        if i-As[j]>=0:
            dp.append(DP[i-As[j]] + Bs[j])
        else:
            dp.append(Bs[j])
    DP[i] = min(dp)        

print(DP[-1])

Submission status: Time Limit Exceeded

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

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


main()

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 8)

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


@lru_cache(maxsize=None)
def dp(h):
    if h <= 0:
        return 0
    else:
        return min(b + dp(h - a) for a, b in AB)


print(dp(H))

Submission status: Time Limit Exceeded

Solution code:
H, N  = map(int, input().split())
dp = [float('inf')] * (H + 1)
dp[0] = 0
for i in range(N):
    a, b = map(int, input().split())
    for j in range(H):
        nj = min(j+a, H)
        dp[nj] = min(dp[nj], dp[j]+b)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
Damage = []
Cost = []


DP = [0] * (H+1)



for i in range(N):
    a,b = map(int,input().split())
    Damage.append(a)
    Cost.append(b)
    
#print(DP)


# DP[i] = best magic points to do i damage

for i in range(1,len(DP)):
    best = pow(10,9)
    for spell in range(N):
#        print(spell,i, Damage[spell], Cost[spell])
        best = min(best,DP[max(0,i-Damage[spell])]+Cost[spell])
    DP[i] = best




print (DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10000000)

h, n = map(int, input().split())
m_list = []
for i in range(n):
  a, b = map(int, input().split())
  m_list.append((a, b))
  
@lru_cache(maxsize=h+1000)
def dp_attack(h):
  res_list = []
  if h <= 0:
    return 0
  for a, b in m_list:
    base_b = dp_attack(h-a)
    res_list.append(base_b+b)
  return min(res_list)

res = dp_attack(h)
print(res)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    a, b = map(int, input().split())
    for j in range(h):
        nj = min(j+a, h)
        dp[nj] = min(dp[nj], dp[j]+b)

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**10

h, n = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
dp = [[INF] * (2 * 10**4) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(1, n+1):
    v, w = ab[i-1]
    for j in range(2 * 10**4):
        if j < v:
            dp[i][j] = dp[i-1][j]
        elif dp[i-1][j] > dp[i][j-v] + w:
            dp[i][j] = dp[i][j-v] + w
        else:
            dp[i][j] = dp[i-1][j]
ans = min(dp[n][h:])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read

H, N, *AB = map(int, read().split())

INF = 10 ** 10
dp = [INF] * (H + 1)
r = INF
dp[0] = 0

for a, b in zip(*[iter(AB)] * 2):
    for i in range(a, H + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
    r = min(r, min(dp[-a:]) + b)

print(min(r, dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    
    H, N = map(int, input().split())

    INF = 1001001001
    dp = [INF]*(H+1)

    dp[0] = 0
    for i in range(N):
        a, b = map(int, input().split())
        for j in range(H):
            nj = min(j+a, H)
            dp[nj] = min(dp[nj], dp[j] + b)

    print(dp[H])


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import sys

# スペース区切りの整数入力
h,n = map(int, input().split())

#指定数分の３つの整数を順番に受けてリストに入力
a = [0]*n
b = [0]*n
for i in range(n):
    a[i],b[i] = map(int, input().split())

e = []
for i in range(n):
    e.append([i,a[i]/b[i],a[i],b[i]])

e.sort(key=itemgetter(1),reverse=True)
mea = e[0][1]
asum = 0
bsum = 0
min_bsum = 9999999999999999999

path = [0]*(10**4+1)
#path = [0]*(40)

depth = 0
while 1:
    #攻撃番号を得る
    anum = path[depth]

    #有効な攻撃か確認
    if anum > n-1:
        #攻撃ネタ切れなので１つ戻る
        path[depth] = 0
        depth -= 1
        if depth < 0: #深さ０に候補無しなので終了
            break
        anum = path[depth]
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
        continue

    #攻撃実施
    asum = asum + e[anum][2]
    bsum = bsum + e[anum][3]
#    print("途中",h,asum,bsum,min_bsum,path[:depth])

    #攻撃後の状況チェック
    if asum >= h:
        if bsum < min_bsum:
            min_bsum = bsum
            #print("min更新",h,asum,bsum,min_bsum,path[:depth])
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
#    elif bsum >= min_bsum:    #積めないので 次の攻撃に行く
    elif (h-asum)/mea + bsum >= min_bsum:    #最大効率で積んだとしてオーバーなので次の攻撃に行く
        #print("オーバー",h,asum,bsum,min_bsum,path[:depth])
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] = 0
        depth -= 1
        if depth < 0: #深さ０に候補無しなので終了
            break
        anum = path[depth]
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
        '''
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
        continue
        '''
        continue
    else:
        depth += 1

print(min_bsum)

Submission status: Time Limit Exceeded

Solution code:
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  mi_a,mi_b = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [0]+[mi_b]*mi_a+[INF]*H
  for i in range(mi_a+1,H+1):
    mi = float('inf')
    for a,b in AB:
      mi = min(mi,dp[max(0,i-a)]+b)
    if dp[i]>mi:
      dp[i] = mi

  print(dp[H])
  
main()

Submission status: Time Limit Exceeded

Solution code:
#!python3

from heapq import heappush, heappop, heapify

iim = lambda: map(int, input().rstrip().split())

def resolve():
    H, N = iim()
    AB = [tuple(iim()) for i in range(N)]

    AB.sort(key=lambda a: (a[0]/a[1], a[0]), reverse=True)

    S = []
    for a, b in AB:
        n = H // a + 1
        h = H
        i = 1
        while n >= i:
            S.append((a*i, b*i))
            n -= i
            i <<= 1
        if n:
            S.append((a*n, b*n))

    N2 = len(S)
    def ubound(b, a, i):
        for j in range(i, N2):
            aj, bj = S[j]
            if a + aj >= H:
                return (b+bj, b + (H-a) * bj / aj)
            a += aj
            b += bj

        return (inf, inf)

    q = []
    u0, u1 = ubound(0, 0, 0)

    inf = float("inf")
    heappush(q, (u1, u0, 0, 0, 0))
    um = u0
    #print(AB)
    #print(H, S)
    while q:
        #print(q)
        uq0, uq1, aq, bq, i = heappop(q)

        if uq0 >= um:
            break

        if i >= N2: continue

        ai, bi = S[i]
        if aq + ai < H:
            heappush(q, (uq0, uq1, aq + ai, bq + bi, i + 1))

        u0, u1 = ubound(bq, aq, i + 1)
        if u1 <= um:
            if u0 < um:
                um = u0
            heappush(q, (u1, u0, aq, bq, i + 1))

    print(um)



if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def solve(H, N, AB):
    INF = 10e10
    dp = [[INF] * (H+1) for i in range(N+1)]
    dp[0][0] = 0
    A, B = AB[0]
    for j in range(H+1):
        dp[0][j] = min(dp[0][j], dp[0][max(0, j-A)]+B)
    for i in range(1,N):
        A, B = AB[i]
        for h in range(H+1):
            dp[i][h] = min(dp[i-1][h], dp[i][max(0, h-A)]+B)
        #print(i, *dp[i], sep="\t")
    
    return(dp[N-1][H])


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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
#S = str(input())
H, N = map(int, input().split())
M = []
for _ in range(N) :
  M.append(list(map(int, input().split())))
#print (M)

Hmax = (10**4)*2+1
#Hmax = 100
dp = [[sys.maxsize for _ in range(Hmax)] for _ in range(H+1)]

#初期処理
for mi in range(N) :
  dp[0][M[mi][0]] = min(dp[0][M[mi][0]], M[mi][1])
#print (dp)
#for i in range(1, H) :
minH = 0
i = 1
while minH < H :
  #print (i)
  #print (minH)
  minH = sys.maxsize
  for val in range(Hmax) :
    if dp[i-1][val] != sys.maxsize :
      minH = min(minH, val)
      for mi in range(N) :
        dp[i][val + M[mi][0]] = min(dp[i][val + M[mi][0]], dp[i-1][val + M[mi][0]], dp[i-1][val] + M[mi][1])
  i += 1
  #print (i)

#print (i)
ans = sys.maxsize 
for a in range(H, Hmax) :
  #print (dp[i-1][a])
  ans = min(ans, dp[i-2][a])
print (ans)
#for i in range(H) :
#  print (dp[i])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
a = []
b = []

for i in range(n):
    a_e, b_e = map(int, input().split())
    a.append(a_e), b.append(b_e)

h_dp = h + max(a)

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

for i in range(n):
    dp[i][0] = 0
    for j in range(h_dp+1):
        if a[i] > h_dp:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-a[i]] + b[i])

ans = float("inf")
for i in range(h, h_dp+1):
    ans = min(ans, dp[n][i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      mi = float('inf')
      for a,b in AB:
        mi = min(mi,dp[max(0,i-a)]+b)
      if dp[i]>mi:
        dp[i] = mi

  print(dp[H])
  
main()

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read

H, N, *AB = map(int, read().split())

INF = 10 ** 10
dp = [INF] * (H + 2)
dp[0] = 0

for a, b in zip(*[iter(AB)] * 2):
    for i in range(a, H + 1):
        dp[i] = min(dp[i], dp[i - a] + b)

    dp[-1] = min(dp[-1], min(dp[-a - 1:]) + b)

print(min(dp[-2], dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    H, N = map(int, readline().split())
    ab = map(int, read().split())
    AB = zip(ab, ab)
    
    dp = [float("inf")] * (H+1)
    dp[0] = 0

    for a, b in AB:
        for j in range(H+1):
            if j + a <= H:
                dp[j+a] = min(dp[j+a], dp[j]+b)
            else:
                dp[H] = min(dp[H], dp[j]+b)

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter

input = sys.stdin.readline


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

    dp = [0] * (H + 1)

    for i in range(1, H + 1):
        mi = float("inf")
        for a, b in AB:
            mi = min(mi, dp[max(i - a, 0)] + b)
        dp[i] = mi

    print(dp[H])



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def dc(n, m):
    return -(-n // m)

@mt
def slv(H, N, AB):

    dp = [INF] * (H+1)
    dp[0] = 0
    for a, b in AB:
        for h, bb in enumerate(dp):
            if bb == INF:
                continue
            n = min(H, h+a)
            dp[n]  = min(dp[n], bb+b)
    return dp[-1]


def main():
    H, N = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(H, N, AB))
    # H = 10**4
    # N = 10
    # AB = [[random.randint(1, 100), random.randint(1, 100)] for _ in range(N)]
    # print(slv(H, N, AB))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      for a,b in AB:
        tmp = max(0,i-a)
        new = dp[tmp]+b
        if dp[i]>new:
          dp[i] = new

  print(dp[H])
  
main()

Submission status: Time Limit Exceeded

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

INF = 10**9

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

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

Submission status: Time Limit Exceeded

Solution code:
import math


H,N = map(int,input().split())
Damage = []
Cost = []


DP = [0] * (H+1)



for i in range(N):
    a,b = map(int,input().split())
    Damage.append(a)
    Cost.append(b)
    
#print(DP)


# DP[i] = best magic points to do i damage

for i in range(1,len(DP)):
    best = pow(10,9)
    for spell in range(N):
#        print(spell,i, Damage[spell], Cost[spell])
        best = min(best,DP[max(0,i-Damage[spell])]+Cost[spell])
    DP[i] = best




print (DP[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# インデックスはライフ 値はそれを以下にする最小MP
dp = [10**10 for i in range(H+1)]
# dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    # quietus_MP = np.inf
    for a, b in AB:
        # if i-a >= 0:
        dp[i] = min(dp[max(i-a, 0)] + b, dp[i])
        # 一回で倒せて一番MP使わない魔法
        # if a > i:
        #     quietus_MP = min(b, quietus_MP)
    # 一回で倒す場合
    # dp[i] = min(quietus_MP, dp[i])

print(dp[-1])

Submission status: Time Limit Exceeded

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

dp = [10 ** 18] * (2 * 10 ** 4 + 1)
dp[0] = 0
for a, b in AB:
    for i in range(H):
        dp[i + a] = min(dp[i + a], dp[i] + b)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    H, N, *ab = map(int, read().split())
    A = []
    B = []
    for a, b in zip(*[iter(ab)] * 2):
        A.append(a)
        B.append(b)
    
    dp = [float("inf")] * (H+1)
    dp[0] = 0

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

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# No DP No LIFE
# 配るDP, 要復習
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    H, N = MAP()
    L = [LIST() for i in range(N)]
    A = []
    B = []
    for i in range(N):
        A.append(L[i][0])
        B.append(L[i][1])

    dp = [INF] * 20010
    dp[0] = 0
    for i in range(10010):
        for j in range(N):
            dp[i+A[j]] = min(dp[i+A[j]], dp[i]+B[j])
    ans = INF
    for i in range(H, 20010):
        ans = min(ans, dp[i])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

h, n = map(int, input().split())
a = ["" for _ in range(n)]
b = ["" for _ in range(n)]

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

inf = 10**12
dp = [[inf for j in range(h+1)] for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0
#print("dp", dp)
 
for i in range(n):
    for j in range(1,h+1):
        #print("-"*50)
        #print("i, j", i, j)
        if j <= a[i]:
            #print(str(j)+" <= a["+str(i)+"]", a[i])
            #print("b[i]", b[i])
            #print("dp[i][j]", dp[i][j])
            #print("dp[i+1][j] = min(): ", min(b[i], dp[i][j]))
            dp[i+1][j] = min(b[i], dp[i][j])
            
        else:
            #print(str(j)+" > a["+str(i)+"]", a[i])
            #print("dp[i][j]", dp[i][j])
            #print("dp[i+1][j - a[i]]+b[i]", dp[i+1][j - a[i]]+b[i])
            #print("dp[i+1][j] = min(): ", min(dp[i][j], dp[i+1][j - a[i]]+b[i]))
            dp[i+1][j] = min(dp[i][j], dp[i+1][j - a[i]]+b[i])
    #print(dp)
 
#print(dp)
print(dp[n][h])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# インデックスはライフ 値はそれを以下にする最小MP
dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    quietus_MP = np.inf
    for a, b in AB:
        # if i-a >= 0:
        dp[i] = min(dp[max(i-a, 0)] + b, dp[i])
        # 一回で倒せて一番MP使わない魔法
        # if a > i:
        #     quietus_MP = min(b, quietus_MP)
    # 一回で倒す場合
    # dp[i] = min(quietus_MP, dp[i])

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:

import numpy as np

H,N=map(int,input().split())
weights=[list(map(int,input().split())) for _ in range(N)]
INF=100100101
#print(weight)
#print(value)

#print(weights)
dp=[INF for i in range(H+1)]
for i in range(H+1):
    if i==0:
        dp[i]=0
    else:
        for j in range(len(weights)):
            a=weights[j][0]
            b=weights[j][1]
            piv=max(0,i-a)  #use i-a's dp[] value as a pivot (if not exits(i-a<0), return dp[0])
            dp[i]=min(dp[i],dp[piv]+b) #the part which forcely treated as piv=0, returns min([weights]), other is treated as normal dp
    #print(dp)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      for k in AB:
        a = k[0]
        b = k[1]
        tmp = max(0,i-a)
        dp[i] = min(dp[i],dp[tmp]+b)

  print(dp[H])
  
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# インデックスはライフ 値はそれを以下にする最小MP
dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    quietus_MP = np.inf
    for a, b in AB:
        if i-a >= 0:
            dp[i] = min(dp[i-a]+b, dp[i])
        # 一回で倒せて一番MP使わない魔法
        if a > i:
            quietus_MP = min(b, quietus_MP)
    # 一回で倒す場合
    dp[i] = min(quietus_MP, dp[i])

print(dp[-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import sys

# スペース区切りの整数入力
h,n = map(int, input().split())

#指定数分の３つの整数を順番に受けてリストに入力
a = [0]*n
b = [0]*n
for i in range(n):
    a[i],b[i] = map(int, input().split())

e = []
for i in range(n):
    e.append([i,a[i]/b[i],a[i],b[i]])

e.sort(key=itemgetter(1),reverse=True)
mea = e[0][1]
asum = 0
bsum = 0
min_bsum = 9999999999999999999

path = [0]*(10**4+1)
#path = [0]*(40)

depth = 0
while 1:
    #攻撃番号を得る
    anum = path[depth]

    #有効な攻撃か確認
    if anum > n-1:
        #攻撃ネタ切れなので１つ戻る
        path[depth] = 0
        depth -= 1
        if depth < 0: #深さ０に候補無しなので終了
            break
        anum = path[depth]
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
        continue

    #攻撃実施
    asum = asum + e[anum][2]
    bsum = bsum + e[anum][3]
#    print("途中",h,asum,bsum,min_bsum,path[:depth])

    #攻撃後の状況チェック
    if asum >= h:
        if bsum < min_bsum:
            min_bsum = bsum
#            print("min更新",h,asum,bsum,min_bsum,path[:depth])
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
#    elif bsum >= min_bsum:    #積めないので 次の攻撃に行く
    elif (h-asum)/mea + bsum >= min_bsum:    #最大効率で積んだとしてオーバーなので次の攻撃に行く
#        print("オーバー",h,asum,bsum,min_bsum,path[:depth])
        asum = asum - e[anum][2]
        bsum = bsum - e[anum][3]
        path[depth] += 1
        continue
    else:
        depth += 1

print(min_bsum)

Submission status: Time Limit Exceeded

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

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

for _ in range(n):
    damage, mp = map(int, input().split())
    for i in range(h+1):
        if i <= damage:
            dp[i] = min(dp[i], mp)
        else:
            dp[i] = min(dp[i], dp[i-damage] + mp)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

magic = np.array(magic)

dp = np.ones(H + magic[:,0].max() + 1) * np.inf
dp[0] = 0 

for m in magic:
    for i in range(H):
        if dp[i] != np.inf:
            idx2 = i+m[0]
            dp[idx2] = min(dp[idx2], dp[i] + m[1])

print(int(dp[H:].min()))

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
ab = min(AB,key = lambda x:x[1])
INF = float('inf')
dp = [INF]*(H+1)
mi_a = ab[0]
mi_b = ab[1]
dp[0] = 0
for i in range(1,H+1):
  if i<=mi_a:
    dp[i]=mi_b
  else:
    for k in AB:
      a = k[0]
      b = k[1]
      tmp = max(0,i-a)
      dp[i] = min(dp[i],dp[tmp]+b)
    
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

magic = np.array(magic)

dp = np.ones(H + magic[:,0].max() + 1) * np.inf
dp[0] = 0 

for m in magic:
    for i in range(H):
        idx2 = i+m[0]
        dp[idx2] = min(dp[idx2], dp[i] + m[1])

print(int(dp[H:].min()))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**10

h, n = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
dp = [[INF] * (2 * 10**4) for _ in range(n + 1)]
dp[0][0] = 0
dp[1][0] = 0
for i in range(n):
    v = ab[i][0]
    w = ab[i][1]
    for j in range(10**4):
        if dp[i][j] == INF:
            continue
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][j + v] = min(dp[i][j + v], dp[i][j] + w)
        dp[i][j+v] = min(dp[i][j+v], dp[i][j] + w)
ans = min(dp[n][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)]

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

d = [0] + [10 ** 18] * H

for hp in range(1, H + 1):
    for ab in AB:
        # print(ab)
        hp_remain = hp - ab[0]
        if hp_remain < 0: hp_remain = 0
        # print(hp_remain)
        c = d[hp_remain] + ab[1]
        if c < d[hp]: d[hp] = c

print(d[H])