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: Runtime Error

Solution code:
H,N = map(int,input().split())

AB = []

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

dp = [0]*(H+1)

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

int main(){
    int H, N;
    cin >> H >> N;
    vi A(N), B(N);
    rep(i, N) cin >> A[i] >> B[i];
    vl dp(H+1, INF);
    dp[0] = 0;
    rep(i, H) {
        rep(j, N) {
            int k = min(i + A[j], H);
            chmin(dp[k], dp[i] + B[j]);
        }
    }
    cout << dp[H] << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
 
h,n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]

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

for a,b in AB:
    for i in range(h):
        if dp[i]!=float("inf"):
            dp[min(i+a,h)]=min(dp[min(i+a,h),dp[i]+b])
print(dp[-1])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
 
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=[float("inf")]*h
dp[0]=0
answer = float("inf")

for i in range(h+1):
    for j in range(n):
      if i + A[j] < h:
        dp[i+A[j]]=min(dp[i+A[j]],dp[i]+B[j])
      else:
        answer = min(answer, dp[i]+B[j])
print(answer)

Submission status: Runtime Error

Solution code:
h, n = map(int, input().split())
ablis = []
for j in range(n):
    a, b = map(int, input().split())
    ablis.append([a, b])
 
def mp(i, x):
    if x <= 0:
        return 0
    else:
        return min(mp(i-1, x), mp(i, x-ablis[i-1][1])+ablis[i-1][2])
 
print(mp(n, h))

Submission status: Runtime Error

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

# 最強の魔法の特定
a_max = max(a for a, b in ab)
# 動的計画法用の配列の定義及び初期化
dp = [0]*(h + a_max)

for i in range(h + a_max):
  if i > 0:
	dp[i] = min(dp[i - a] + b for a, b in ab)

print(min(dp[h:]))

Submission status: Runtime Error

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

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)

Ama = max(A)

dp=[float("inf")]*(h+Ama)
dp[0]=0

for i in range(n):
    for j in range(A[i],h+Ama+1):
        dp[j]=min(dp[j],dp[j-A[i]+B[i]])
print(min(dp[h:]))

Submission status: Runtime Error

Solution code:
import numpy as np
from math import ceil
from copy import copy
 
H, N = list(map(int, input().split()))
 
def func(magic):
    dp = np.ones(H + magic[:,0].max() * 2 + 1) * np.inf
    dp[0] = 0
 
    for i in range(0, H+1):
        if dp[i] == np.inf:
            continue
        
        for m in magic:
            dp[i+m[0]] = min(dp[i+m[0]], dp[i] + m[1])
 
    print(int(dp[H:].min()))
 
 
 
def main():
    magic = []
    for i in range(N):
        a, b = list(map(int, input().split()))
        magic.append([a, b])
 
    magic = np.array(magic)
    func(magic)
 
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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 = [float("inf")] * (h + 1010)
dp[0] = 0

for i in range(h + 1):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])
print(min(dp[h:]))

Submission status: Runtime Error

Solution code:
h, n = map(int,input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int,input().split())
    a.append(ai)
    b.append(bi)

dp = [0]*10001

for i in range(10001):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Submission status: Runtime Error

Solution code:
from sys import stdin
readline = stdin.readline
H,N=map(int,readline().split())
INF=int(10**7)
dp=[INF]*(H+1)
dp[0]=0
for _ in [0]*N:
    a,b=map(int,readline().split())
    for k in range(0,H):
        if k+a<H:
            nj=k+a
        else:nj=H
        if dp[nj]<dp[j]+b:
            dp[nj]=dp[nj]
        else:dp[nj]=dp[j]+b
print(dp[H])

Submission status: Runtime Error

Solution code:
# ダイナミック・プログラミング(DP)

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

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


dp_memo = {} # メモ化再帰
def dp_magic_2(HP):
    mp_list = []
    if HP<=0:
        return 0
    elif HP in dp_memo:
        return dp_memo[HP]
    else:
        for m in magic_list:
            mp_list.append(dp_magic_2(HP-m[0]) + m[1])
        result_mp = sorted(mp_list)[0]
        dp_memo[HP] = result_mp
        return result_mp

print(dp_magic_2(H))

Submission status: Runtime Error

Solution code:
# ダイナミック・プログラミング(DP)

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

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


dp_memo = {} # メモ化再帰
def dp_magic_2(HP):
    mp_list = []
    if HP<=0:
        return 0
    elif HP in dp_memo:
        return dp_memo[HP]
    else:
        for m in magic_list:
            mp_list.append(dp_magic_2(HP-m[0]) + m[1])
        result_mp = min(mp_list)
        dp_memo[HP] = result_mp
        return result_mp

print(dp_magic_2(H))

Submission status: Runtime Error

Solution code:
import numpy as np

H , N = map(int,input().split())
magic = []
for i in range(N):
    m = list(map(int,input().split()))
    c = m[0] / m[1]
    m.append(c)
    magic.append(m)

#magic = sorted(magic,key=lambda x: (x[2],x[0]) )[::-1]
times = list(np.argmax(magic , axis= 1))[0]
dp = [10000000000]* (H + magic[int(times)][0])
dp[0] = 0 

for i in magic:
    for j in range(H + int(times) ):
        total = dp[j] + i[1]
        if j + i[0] > (H + magic[int(times)][0] - 1):
            break
        if dp[j + i[0]] > total:
            dp[j + i[0] ] = total

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

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10000)

H, N = [int(n) for n in input().split()]

A = [0] * N
B = [0] * N

ami = 0
for i in range(N):
    A[i], B[i] = [int(n) for n in input().split()]
    if A[i] > A[ami]:
        ami = i

i_s = list(range(N))
lmpis = sorted(i_s, key=lambda i: B[i])

M = {}
mi = 100000000
def m(h):
    global mi
    if h in M:
        return M[h]
    if h == 0:
        return 0
    for i in range(N):
        j = lmpis[i]
        if A[j] <= h:
            c = m(h-A[j])
            if c is not None:
                b = B[j] + c
                if h not in M or (M[h] and M[h] > b and b < mi):
                    M[h] = b
                    if h >= H:
                        mi = b
    if h in M:
        return M[h]
    else:
        M[h] = None

for h in range(H, H + A[ami]):
    mm = m(h)
    if mm:
        break
print(mm)

Submission status: Runtime Error

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

A = [0] * N
B = [0] * N

ami = 0
for i in range(N):
    A[i], B[i] = [int(n) for n in input().split()]
    if A[i] > A[ami]:
        ami = i

i_s = list(range(N))
lmpis = sorted(i_s, key=lambda i: B[i])

M = {}
mi = 100000000
def m(h):
    global mi
    if h in M:
        return M[h]
    if h == 0:
        return 0
    for i in range(N):
        j = lmpis[i]
        if A[j] <= h:
            c = m(h-A[j])
            if c is not None:
                b = B[j] + c
                if h not in M or (M[h] and M[h] > b and b < mi):
                    M[h] = b
                    if h >= H:
                        mi = b
    if h in M:
        return M[h]
    else:
        M[h] = None

for h in range(H, H + A[ami]):
    mm = m(h)
    if mm:
        break
print(mm)

Submission status: Runtime Error

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

A = [0] * N
B = [0] * N

ami = 0
for i in range(N):
    A[i], B[i] = [int(n) for n in input().split()]
    if A[i] > A[ami]:
        ami = i

i_s = list(range(N))
lmpis = sorted(i_s, key=lambda i: B[i])

M = {}
mi = 100000000
def m(h):
    if h in M:
        return M[h]
    if h == 0:
        return 0
    for i in range(N):
        j = lmpis[i]
        if A[j] <= h:
            c = m(h-A[j])
            if c is not None:
                b = B[j] + c
                if h not in M or (M[h] and M[h] > b):
                    M[h] = b
    if h in M:
        return M[h]
    else:
        M[h] = None

for h in range(H, H + A[ami]):
    mm = m(h)
    if mm and mi > mm:
        mi = mm
print(mi)

Submission status: Runtime Error

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

abKeys = set([])
result = [10**9] * (h+1)
for _ in range(n):
    a, b = map(int, input().split())
    result[a] = min(result[a],b)

for i in range(1,h+1):
    j = 1
    while j <= i // 2:
        result[i] = min(result[i], result[j] + result[i - j])

        j += 1


print(result[-1])

Submission status: Runtime Error

Solution code:
def main()
    import sys
    input = sys.stdin.readline

    INF = 10 ** 18

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

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

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

    # for d in dp:
    #     print (d)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

a=[]
b=[]
INF=10**9
dp=[INF for i in range(h+1)]

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


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

if flag==0:
  for i in range(1,h+1):
    if dp[i]==INF:
      #dp[i]=min(dp[i],dp[i-1])
      #print(dp)
      continue
    for j in range(n):
      if i+a[j]<=h:
        dp[i+a[j]]=min(dp[i+a[j]],dp[i]+b[j])
        #print(dp)
      else:
        dp[h]=min(dp[h],dp[i]+b[j])
        #print(dp)
    #dp[i]=min(dp[i],dp[i-1])
    #print(dp)
print(dp[h])

Submission status: Runtime Error

Solution code:
h, n = map(int,input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int,input().split())
    a.append(ai)
    b.append(bi)

m = 0
minc = 10**9
newi = -1
oldi = -2
cost = 0
while not newi == oldi:
    oldi = newi
    for i in range(len(n)):
        cost = b[i]*int((h-1)/a[i]+1)
        if cost < minc:
            newi = i
    h -= a[newi]*int((h-1)/a[newi])
    m += b[newi]*int((h-1)/a[newi])
    if newi == oldi:
        m += b[newi]

print(m)

Submission status: Runtime Error

Solution code:
import numpy as np

h, n = map(int, input().split())
ab = list(tuple(map(int, input().split())) for _ in range(n))

sa, sb = 0, 0
for _, a, b in sorted((a/b, a, b) for a, b in ab, reversed=True):
    sa += a
    sb += b
    if sa >= h:
        print(sb)
        break

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import math
h,n = map(int, input().split())
aa = [None]
bb = [None]
mp = []
for _ in range(h + 1):
  	mpp = []
	for __ in range(n+1):
      mpp.append(None)
    mp.append(mpp)
# 体力i 技の種類j
for j in range(1, n+1):
  a, b = map(int, input().split())
  aa.append(a)
  bb.append(b)

for i in range(1, h+1):
    mp[i][0] = float("inf")
for j in range(1, n+1):
    for i in range(1, h+1):
        if i - aa[j] >= 1:
          # print(i, j, i - aa[j])
          mp[i][j] = min(mp[i][j-1], mp[i - aa[j]][j] + bb[j])
        else:
          mp[i][j] = min(mp[i][j-1], bb[j])
print(mp[h][n])

Submission status: Runtime Error

Solution code:
h, k = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(k)]
dp = [0 for i in range(h+1)]
for i in range(1, h+1):
    dp[i] = min(dp[i - a]+b for a,b in ab)
print(min(dp[h:]))

Submission status: Runtime Error

Solution code:
import numpy as np
h,n=map(int,input().split())
a,b=[],[]
for _ in range(n):
    aa,bb=map(int,input().split())
    a.append(aa)
    b.append(bb)
a=np.array(a)
b=np.array(b)
dp=np.full((n+1,h+1),int(1e10),dtype=int)
dp[0,0]=0
for i in range(n):
    for j in range(h+1):
        dp[i+1][j] = min(dp[i,j], dp[i+1,max(0,j-a[i])]+b[i])
print(dp[n][h])

Submission status: Runtime Error

Solution code:
h, k = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(k)]
dp = [0 for i in range(10**3+1)]
for i in range(1, h+1):
    dp[i] = min(dp[i - a]+b for a,b in ab)
print(dp[h])

Submission status: Runtime Error

Solution code:
h, k = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(k)]
dp = [0 for i in range(h+1)]
for i in range(1, h+1):
    dp[i] = min(dp[i - a]+b for a,b in ab)
print(dp[h])

Submission status: Runtime Error

Solution code:
h, k = map(int, input().split())
a = []
b = []
for i in range(k):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)
dp = [0 for _ in range(h+1)]

for i in range(1, h+1):
    m0 = dp[i-a[0]] + b[0]
    for j in range(1, len(a)):
        m = dp[i-a[j]] + b[j]
        m0 = min(m, m0)
    dp[i] = m0

print(dp[h])

Submission status: Runtime Error

Solution code:
h, k = map(int, input().split())
a = []
b = []
for i in range(k):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)
dp = [0 for _ in range(h+1)]

for i in range(h+1):
    m0 = dp[i-a[0]] + b[0]
    for j in range(1, len(a)):
        m = dp[i-a[j]] + b[j]
        m0 = min(m, m0)
    dp[i] = m0

print(dp[h])

Submission status: Runtime Error

Solution code:
import numpy as np
H, N = map(int, input().split())
A = []
B = []
for _ in range(N):
  ai, bi = map(int, input().split())
  A.append(ai)
  B.append(bi)

# process
dp = np.zeros(H+1, dtype=np.int64)
dp[0] = 0

for i in range(1, H + 1):
    dp[i] = np.amin(dp[np.maximum(i - A, 0)] + B)

# output
print(dp[H])

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
import fractions
import random
from heapq import heappop,heappush
import time
import sys
readline = sys.stdin.readline
#import numpy as np
mod=int(10**9+7)
inf=int(10**20)
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def comb(n,r):return math.factorial(n)//math.factorial(n-r)//math.factorial(r)
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
 

 
#main
h,n=map(int,input().split())
AB=[list(map(int,input().split())) for a in range(n)]
dp=[[inf for b in range(h+1)] for a in range(n+1)]
dp[0][0]=0

for a in range(1,n+1):
    for b in range(0,h+1):
        key=min(h,b+AB[a-1][0])
        dp[key]=min(dp[key],dp[b]+AB[a-1][1])
print(dp[n][h])

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include<limits.h>
#include<iomanip>
#include <cassert>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define REP(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(),v.end()
#define RALL(v) v.rbegin(),v.rend()
#define Cout(x) cout << x << endl;
#define check(v) rep(i,v.size()) cout << v[i] << endl;
#define vint(x) vector<int> x
#define vll(x) vector<long long> x
#define vstr(x) vector<string> x
const long long LLINF = 1LL<<60;
const int INF = 1e9;
const long long MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
#define pint pair<int,int>
using namespace std;
//オーバーフローに気をつけろよおおおおおお
//確認忘れるなよおおおおおお

int a[1005], b[1005];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int h, n; cin >> h >> n;
    rep(i,n) {
        cin >> a[i] >> b[i];
    }

    vector<int> dp(10005,INF);
    dp[0] = 0;

    rep(i,h+1) {
        if(dp[i]==INF) continue;
        rep(j,n) {
            int A = a[j], B = b[j];
            int ni = min(h,i+A);
            dp[ni] = min(dp[ni],dp[i] + B);
        }
    }
    cout << dp[h] << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
import fractions
import random
from heapq import heappop,heappush
import time
import sys
readline = sys.stdin.readline
#import numpy as np
mod=int(10**9+7)
inf=int(10**20)
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def comb(n,r):return math.factorial(n)//math.factorial(n-r)//math.factorial(r)
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
 

 
#main
h,n=map(int,input().split())
AB=[list(map(int,input().split())) for a in range(n)]
dp=[[inf for b in range(h+1)] for a in range(n+1)]
dp[0][0]=0

for a in range(1,n+1):
    for b in range(0,h+1):
        dp[a][b]=min(dp[a][b],dp[a-1][b])
        key=min(h,b+AB[a-1][0])
        dp[a][key]=min(dp[a][key],dp[a][b]+AB[a-1][1])
print(min(dp[n][h]))

Submission status: Runtime Error

Solution code:
INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())))

dp = [0] + [INT_MAX]* h
for i in range(1, h+1):
  v = INT_MAX
  for a, b in e:
    c = dp[max(0, i - a)] + b
    v = min(v, c)
  dp[i] = v
print(dp[h])

Submission status: Runtime Error

Solution code:
INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())))

dp = [0] + [INT_MAX]* h
for i in range(1, h+1):
  for a, b in e:
    c = dp[max(0, i - a)] + b
    dp[i] = min(dp[i], c)

print(dp[h])

Submission status: Runtime Error

Solution code:
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
for i in range(N):
  n,a=input().split()
  atk.append(int(n))
  MP.append(int(a))

@lru_cache(maxsize=1000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    b = fx(H - atk[i]) + MP[i]
    arr.append(b)   
  return min(arr)

a = fx(H)
print(a)

Submission status: Runtime Error

Solution code:
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  dp[0][0]= 0
  #DP
  for i in range(N):
    for w in range(W+1):
      if 0 <= w-weight[i]: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        if dp[i+1][w-1] != inf:
          a = dp[i+1][w-1]+value[i]
        else:
          a = inf
        dp[i+1][w]=min(dp[i+1][w-weight[i]]+value[i], dp[i][w],a)
      else:
        dp[i+1][w]=dp[i][w]
  return dp[N][W]
 
H,N = map(int,input().split())
 
weight=[]
value=[]
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
a = knapsack(N,H,weight,value)
 
print(int(a))

Submission status: Runtime Error

Solution code:
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  dp[0][0]= 0
  #DP
  for i in range(N):
    for w in range(W+1):
      if 0 <= w-weight[i]: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=min(dp[i+1][w-weight[i]]+value[i], dp[i][w],dp[i+1][w-1]+value[i])
      else:
        dp[i+1][w]=dp[i][w]
  return dp[N][W]
 
H,N = map(int,input().split())
 
weight=[]
value=[]
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
a = knapsack(N,H,weight,value)
 
print(int(a))

Submission status: Runtime Error

Solution code:
import numpy as np

H , N = map(int,input().split())
magic = []
for i in range(N):
    m = list(map(int,input().split()))
    c = m[0] / m[1]
    m.append(c)
    magic.append(m)

magic = sorted(magic,key=lambda x: (x[2],x[0]) )[::-1]
times = list(np.argmax(magic , axis= 1))[0]
dp = [10000000000]* (H + magic[int(times)][0])
dp[0] = 0 

for i in magic:
    for j in range(H + int(times) ):
        total = dp[j] + i[1]
        if j + i[0] > (H + magic[int(times)][0] - 1):
            break
        if dp[j + i[0]] > total:
            dp[j + i[0] ] = total

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

Submission status: Runtime Error

Solution code:
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=100000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    kaisuu = int(H // atk[i])
    if kaisuu >= 1:
      for j in range(1,kaisuu+1):
        c = int(H - atk[i]*j)
        b = int(fx(c) + MP[i]*j)
        arr.append(b)   
    else:
      b = int(fx(H - atk[i]) + MP[i])
      arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Submission status: Runtime Error

Solution code:
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=100000000000000000000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    c = int(H - atk[i])
    b = int(fx(c) + MP[i])
    arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Submission status: Runtime Error

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

max_a=max(a for a,_ in ab)
dp = [0] * (h+max_a)

for i in range(0,h+max_a):
    if i==0:
        d[i]=0
    else:
        dp[i]=min(dp[i-a]+b for a,b in ab)
print(min(dp[h:]))

Submission status: Runtime Error

Solution code:
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=1000000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    c = int(H - atk[i])
    b = int(fx(c) + MP[i])
    arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Submission status: Runtime Error

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

max_a=max(a for a,_ in ab)
dp = [0] * (h+max_a)

for i in range(0,h+max_a):
    if i==0:
        d[i]=0
    else:
        dp[i]=min(dp[i-a]+b for a,b in ab)
print(min(dp[h:])

Submission status: Runtime Error

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

max_a=max(a for a,_ in ab)

for i in range(0,h+max_a):
    if i==0:
        d[i]=0
    else:
        dp[i]=min(dp[i-a]+b for a,b in ab)
print(min(dp[h:])

Submission status: Runtime Error

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

max_a=max(a for a,_ in ab)

for i in range(0,h+max_a):
    if i==0:
        d[i]=0
    else:
        dp[i]=min(dp[i-a]+b for a,b in ab)
print(min(dp[h:])

Submission status: Runtime Error

Solution code:
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=[10**4+1]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho>DP[dpi-Ai]+Bi:
                kouho=DP[dpi-Ai]+Bi
    DP[dpi]=kouho


print(min(DP[H:]))

Submission status: Runtime Error

Solution code:
import numpy as np

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=[10**4+1]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho>DP[dpi-Ai]+Bi:
                kouho=DP[dpi-Ai]+Bi
    DP[dpi]=kouho


print(min(DP[H:]))

Submission status: Runtime Error

Solution code:
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=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    #kouho=[]
    #kouho.append(DP[dpi])
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho<DP[dpi-Ai]+Bi:
            #kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=kouho


print(min(DP[H-1:]))

Submission status: Runtime Error

Solution code:
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=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    #kouho=[]
    #kouho.append(DP[dpi])
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho<DP[dpi-Ai]+Bi
            #kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=kouho


print(min(DP[H-1:]))

Submission status: Runtime Error

Solution code:
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=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=[]
    kouho.append(DP[dpi])
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=min(kouho)


print(min(DP[H-1:]))

Submission status: Runtime Error

Solution code:
import math
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=[0]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

DP=[math.inf]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai in A:#すでにあるものを埋める
    DP[Ai-1]=Ai 

for dpi in range(H+max(A)-1):
    kouho=[]
    for Ai,Bi in zip(A,B):
        kouho.append(DP[dpi])
        if dpi-Ai>=0:
            kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=min(kouho)


print(min(DP[H-1:]))

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())

magic = []

for _ in range(N):
    magic.append(list(map(int, input().split())))

memo = [-1 for _ in range(10000)]
    
def rec(hp):
    
    if hp <= 0:
        return 0
    
    if memo[hp] != -1:
        return memo[hp]
    
    res = hp
    
    for m in magic:
        res = min(res, rec(hp - m[0]) + m[1])
    
    memo[hp] = res
    return res

print(rec(H))

Submission status: Runtime Error

Solution code:
import math
import copy
H,N=map(int,input().split())

dp=[0]+[None]*H

for i in range(N):
    A,B=map(int,input().split())
    ndp=[0]+[None]*H
    for j in range(A,H+1):
        if dp[j]==None:
            if ndp[j-A]!=None:
                ndp[j]=ndp[j-A]+B
            continue
        
        if ndp[j-A]==None:
            if dp[j]!=None:
                ndp[j]=dp[j]
            
            continue

        ndp[j]=min(ndp[j-A]+B,dp[j])

    for j in range(max(H-A+1,0),H+1):
        if ndp[j]==None:
            continue

        if ndp[-1]!=None:
            ndp[-1]=min(ndp[j]+B,ndp[-1])
        else:
            ndp[-1]=ndp[j]+B
    
    if A >= H:
        ndp[-1]=min(B,dp[-1])

    dp=ndp

print(dp[-1])

Submission status: Runtime Error

Solution code:

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


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




H,N=acinput()
s=[]

mmp=-1
for i in range(N):
    tmp = acinput()
    #tmp.append(tmp[0]/tmp[1])
    s.append(tmp)
    mmp=max(mmp,tmp[0])
#s=sorted(s,reverse=True,key= lambda x:x[2])

dp=[[10**6 for h in range(H+1)]for i in range(N)]

for i in range(1,H+1):
    dp[0][i]=int(np.ceil(i/s[0][0]))*s[0][1]

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

for i in range(1,N):
    a,b=s[i]
    for j in range(1,H+1):
        dp[i][j]=min(dp[i-1][j],dp[i][j-a]+b)

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