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: Wrong Answer

Solution code:
import math

h, n = map(int, input().split())
a_list = []
b_list = []
for _ in range(n):
    a, b = map(int, input().split())
    a_list.append(a)
    b_list.append(b)
    
cost_list = 10 ** 9
for i in range(n):
    for j in range(n):
        cost_list = min(cost_list, (h // a_list[i]) * b_list[i] + math.ceil((h % a_list[i]) / a_list[j]) * b_list[j])
print(cost_list)

Submission status: Wrong Answer

Solution code:
import math

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

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

    
index = 0
index2 = 0
m = 10**15
m2 = 10**15
for i in range(N):
    if L[i][1]/L[i][0] < m:
        m = L[i][1]/L[i][0]
        index = i

K0 = L[index][0]
K1 = L[index][1]

        
        
if H%K0 == 0:
    print(int((K1*H)/K0))
else:
    for i in range(N):
        if (H%K0)<= L[i][0]:
            if L[i][1] <= m2:
                m2 = L[i][1]
                index2 = i
    print(int(K1*int(H/K0)+L[index2][1]))

Submission status: Wrong Answer

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

ab_input = [[int(j) for j in input().split()] for i in range(n)]
#配るdp
dp =[10**10]*(h+1)
dp[0]=0
for i in range(1,h):
    for a,b in ab_input:
        next_index=min(h,i+a)
        dp[next_index]=min(dp[next_index],dp[i]+b)
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc153/tasks/abc153_e
# 個数無制限ナップサック問題

H, N = read_ints()
A, B = read_col(N)


'''
dp[i][j] ... 与えられるダメージの最大。[0,i)の魔法で、魔力j以下使うとき。
更新則
dp[i+1][j] = max(dp[i][j],dp[i+1,j-B[i]] + A[i])
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[0][0] = 0 #なくても大丈夫そう


これだとjが大きすぎてTLEする
'''

'''
dp[i][j] ... 魔力の最小値。[0,i)の魔法で、ちょうどダメージjにできるとき。ちょうどjにできなければINF
更新則

dp[i+1][j] = min(dp[i][j],dp[i+1,j-A[i]] + B[i]) で良さそうじゃない？
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[i][0] = 0
'''


dp = [[INF] * (2 * H + 1) for _ in range(N + 1)]  # Hの二倍も取れば十分かな？
for i in range(N + 1):
    dp[i][0] = 0


for i in range(N):
    for j in range(2 * H + 1):
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
        if j - A[i] >= 0:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - A[i]] + B[i])

ans = INF
for x in dp[N][H:]:
    ans = min(ans, x)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    h, n = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(n)]

    dp = [f_inf] * (h + 1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, h + 1):
            if i - a >= 0:
                dp[i] = min(dp[i], dp[i - a] + b)
            else:
                dp[i] = dp[0] + b

    print(dp[h])


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
dp = [float("inf")] * (H + 1)
dp[H] = 0
for _ in range(N):
  a, b = map(int, input().split())
  for x in range(H + 1):
    dp[max(0, x - a)] = min(dp[max(0, x - a)], dp[x] + b)

print(dp[0])

Submission status: Wrong Answer

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

M = {}
max_atk_idx = 0
min_mjk_idx = 0
for i in range(N):
    A, B = map(int, input().split())
    M[i] = {'ATK': A, 'MP': B, 'kouritsu': A / B}

    if i == 0:
        continue

    if M[max_atk_idx]['kouritsu'] < M[i]['kouritsu']:
        max_atk_idx = i

    if M[min_mjk_idx]['ATK'] > M[i]['ATK'] and M[min_mjk_idx]['kouritsu'] > M[i]['kouritsu']:
        min_mjk_idx = i

lost_mp = 0
max_m_count = H // M[max_atk_idx]['ATK']

h = H - M[max_atk_idx]['ATK'] * max_m_count
if h == 0:
    min_m_count = 0
elif h < M[min_mjk_idx]['ATK']:
    min_m_count = 1
else:
    min_m_count = h // M[min_mjk_idx]['ATK']

print(M[max_atk_idx]['MP'] * max_m_count + M[min_mjk_idx]['MP'] * min_m_count)

Submission status: Wrong Answer

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

M = {}
max_atk_idx = 0
min_mjk_idx = 0
for i in range(N):
    A, B = map(int, input().split())
    M[i] = {'ATK': A, 'MP': B, 'kouritsu': A / B}

    if i == 0:
        continue

    if M[max_atk_idx]['kouritsu'] < M[i]['kouritsu']:
        max_atk_idx = i

    if M[min_mjk_idx]['ATK'] > M[i]['ATK'] and M[min_mjk_idx]['MP'] > M[i]['MP']:
        min_mjk_idx = i

lost_mp = 0
max_m_count = H // M[max_atk_idx]['ATK']

h = H - M[max_atk_idx]['ATK'] * max_m_count
if h == 0:
    min_m_count = 0
elif h < M[min_mjk_idx]['ATK']:
    min_m_count = 1
else:
    min_m_count = h // M[min_mjk_idx]['ATK']

print(M[max_atk_idx]['MP'] * max_m_count + M[min_mjk_idx]['MP'] * min_m_count)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    h, n = map(int, stdin.readline().split())
    ab = []
    max_a = 0
    for _ in range(n):
        a, b = map(int, stdin.readline().split())
        if a > max_a:
            max_a = a
        ab.append([a, b])

    dp = [0] * (h + max_a)

    for i in range(h + max_a):
        dp[i] = (i // ab[0][0] + 1) * ab[0][1]

    for i in range(h):
        for j in range(n):
            dp[i + ab[j][0]] = min(dp[i + ab[j][0]], dp[i] + ab[j][1])

    print(min(dp[h - 1:]))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    h, n = map(int, stdin.readline().split())
    ab = []

    for _ in range(n):
        a, b = map(int, stdin.readline().split())
        ab.append([a / b, a, b])

    ab.sort(reverse=True, key=lambda x: {x[0], -x[2]})

    sum_b = 0

    while h > 0:
        i = 0
        while ab[i][1] >= h:
            if i == n - 1:
                break
            i += 1
        count = h // ab[i][1]
        if count == 0:
            count += 1
        sum_b += ab[i][2] * count
        h -= ab[i][1] * count

    print(sum_b)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    h, n = map(int, stdin.readline().split())
    ab = []

    for _ in range(n):
        a, b = map(int, stdin.readline().split())
        ab.append([a / b, a, b])

    ab.sort(reverse=True, key=lambda x: {x[0], -x[2]})

    sum_b = 0

    while h > 0:
        i = 0
        while ab[i][1] >= h:
            if i == n - 1:
                break
            i += 1
        count = h // ab[i][1]
        if count == 0:
            count += 1
        sum_b += ab[i][2] * count
        h -= ab[i][1] * count

    print(sum_b)

Submission status: Wrong Answer

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

inf = 10**9
magic = []
c = 0
for i in range(n):
	a, b = map(int, input().split())
	magic.append([a,b])
	c = max(c,a)

dp = [inf] * (h+c)

for i, j in magic:
	dp[i] = j

for i in range(len(dp)):
	for j in range(n):
		if i - magic[j][0] <= 0 or dp[i-magic[j][0]] == inf:
			continue
		else:
			dp[i] = min(dp[i],dp[i-magic[j][0]] + magic[j][1])

print (min(dp[h:]))

Submission status: Wrong Answer

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

magic = []
c = 0
for i in range(n):
	a, b = map(int, input().split())
	magic.append([a,b])
	c = max(c,a)

dp = [10**7] * (h+c)
#dp[0] = 0

for i, j in magic:
	dp[i] = j

for i in range(len(dp)):
	for j in range(n):
		if i - magic[j][0] <= 0 or dp[i-magic[j][0]] == 10**7:
			continue
		else:
			dp[i] = min(dp[i],dp[i-magic[j][0]] + magic[j][1])

print (min(dp[h:]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


l = []
for i in range(n):
  a, b = map(int, input().split())
  l.append((a, b))
m = max(l, key=lambda x: x[0])[0]
dp = [1]*(h+m+1)
dp[0] = 0
for i in range(1, h+m+1):
  dp[i] = min([dp[i-l[j][0]]+l[j][1] for j in range(n)])

Submission status: Wrong Answer

Solution code:
H,N=[int(x) for x in input().rstrip().split()]
l=[]
val=0
mae_val=0
for i in range(N):
  A,B=[int(x) for x in input().rstrip().split()]
  val=B/A
  l.append([A,B,val])

l=sorted(l,key=lambda x :x[2])

cons=0
for i in range(N):
  if H//l[i][0]>0:
    cons+=l[i][1]*(H//l[i][0])
    H-=l[i][0]*(H//l[i][0])



if H!=0:
  cons+=l[N-1][1]
print(cons)

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
Attack_Magic = []
max_a = 0
for i in range(N):
    a, b = map(int, input().split())
    Attack_Magic.append((a, b))
    max_a = max(max_a, a)

Point = [1000000] * (H + max_a + 10)
Point[0] = 0
for i in range(H):
    for j, k in Attack_Magic:
        next = i + j
        cost = Point[i] + k
        Point[next] = min(Point[next], cost)


print(min(Point[H:]))

Submission status: Wrong Answer

Solution code:
H,N =map(int, input().split())
a = [] 
M = 0 
for i in range(N):
	t = list(map(int, input().split()))
	a.append(t)
	M = max(M,t[0])
dp = [10000]*(H+1+M)
dp[0] = 0
for i in range(H):
	for j,k in a:
		dp[i+j] = min(dp[i+j],dp[i]+k)

print(min(dp[H:]))

Submission status: Wrong Answer

Solution code:
h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
  aa,bb=map(int,input().split())
  a.append(aa)
  b.append(bb)
inf=float("inf")
dp=[[inf]*(h+1) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h):
    dp[i+1][j]=min(dp[i+1][j],dp[i][j])
    dp[i+1][min(h,j+a[i])]=min(dp[i+1][j]+b[i],dp[i+1][min(h,j+a[i])])
    
print(dp[n][h])

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
F=[]
G=[]
for i in range(N):
    a,b=map(int,input().split())
    F.append([b/a,a,b])

F.sort()
dsf=float('inf')

if H%F[0][1]==0:
    print((H//F[0][1])*F[0][2])
else:
    ans=(H//F[0][1])*F[0][2]
    #print(ans)
    H-=(H//F[0][1])*F[0][1]
    #print(H)
    for i in range(N):
        if H%F[i][1]==0:
            fg=ans+H//F[i][1]*F[i][2]
        else:
            fg=ans+(H//F[i][1]+1)*F[i][2]
        dsf=min(dsf,fg)
    print(dsf)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,n=map(int, input().split())
que = deque()
pe = []
dp = [1000000]*10002
#HP i 削る時の最小MP dp

for i in range(n):
    a,b = map(int, input().split())
    pe.append([a,b])
dp[0]=0
for i in range(10**4):
    for j in pe:
        if i-j[0]>-1:
            dp[i]=min(dp[i], dp[i-j[0]]+j[1])

print(min(dp[h:]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(30000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

#dp_map = []
#for i in range(N+1):
#    temp_arr = [-1 for _ in range(H+1)]
#    dp_map.append(temp_arr)
dp_map = [[-1]*(H+1)]*(N+1)

def DP(i, j):
    if j<=0:
        return 0
    if dp_map[i][j]!=-1:
        return dp_map[i][j]
    
    if i==1:
        dp_map[i][j] = DP(i,j-As[i-1])+Bs[i-1]
        return dp_map[i][j]

    dp_map[i][j] = min(DP(i-1,j),DP(i,j-As[i-1])+Bs[i-1])
    return dp_map[i][j]

print(DP(N,H))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(30000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

dp_map = [[False]*(H+1)]*(N+1)
#for i in range(N+1):
#    temp_arr = [False for _ in range(H+1)]
#    dp_map.append(temp_arr)

def DP(i, j):
    if j<=0:
        return 0
    if dp_map[i][j]!=False:
        return dp_map[i][j]
    
    if i==1:
        dp_map[i][j] = DP(i,j-As[i-1])+Bs[i-1]
        return dp_map[i][j]

    dp_map[i][j] = min(DP(i-1,j),DP(i,j-As[i-1])+Bs[i-1])
    return dp_map[i][j]

print(DP(N,H))

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
ATK, MP = zip(*(tuple(map(int, input().split())) for _ in range(N)))
table = [float("inf")] * (H + 1 + max(ATK))
table[0] = 0
for damage in range(1, H + 1):
    table[damage] = min(table[damage - ATK[magic]] + MP[magic] for magic in range(N))
print(table[H])

Submission status: Wrong Answer

Solution code:
l = input().split()
H, N = int(l[0]), int(l[1])
M = []
for i in range(N):
    M.append([int(x) for x in input().split()])
Magic = [0, 0, 0]
for i in range(N):
    M[i].append(M[i][0] / M[i][1])

MP = 0
while H >= 1:
    Magic = [0, 0, 0]
    for i in range(N):        
        if M[i][0] > H:
            M[i][2] = H / M[i][1]
        if M[i][2] > Magic[2]:
            Magic = M[i]
            print(Magic)
    
    H = H - Magic[0]
    MP += Magic[1]
print(MP)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


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

    INF = 1000000005
    dp = [[INF for _ in range(H + 1)] for _ in range(N + 1)]
    for j in range(H + 1):
        dp[1][j] = math.ceil(j / A[0]) * B[0]

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

    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


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

    INF = 1000000005
    dp = [[INF for _ in range(H + 1)] for _ in range(N + 1)]
    for j in range(H + 1):
        dp[1][j] = math.ceil(j / A[0]) * B[0]

    for i in range(1, N):
        for j in range(H + 1):
            k = 0
            while k * A[i] <= j:
                dp[i + 1][j] = min(dp[i + 1][j], dp[i]
                                   [j - k * A[i]] + k * B[i])
                k += 1

    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, n = map(int, input().split())
magics = []
for _ in range(n):
    a, b = map(int, input().split())
    magics.append((a, b))
print(magics)

# 体力をn削るために必要なmpの最小値
dp = [0] * (H+1)
for h in range(1, H+1):
    dp[h] = min([dp[max(h-a, 0)] + b for (a, b) in magics])

print(dp[-1])

Submission status: Wrong Answer

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

cospa = [[0,0]]*N
for i in range(N):
    cospa[i] = [AB[i][0]/AB[i][1], i]
cospa.sort(key=lambda x: x[0], reverse=True)

replic = [0]*N
alpha = [0]*N

alpha[0] = H
dpzero = 0
for i in range(N-1):
    replic[i] = alpha[i] // AB[cospa[i][1]][0]
    alpha[i+1] = alpha[i] - (replic[i] * AB[cospa[i][1]][0])
    dpzero += replic[i] * AB[cospa[i][1]][1]

if alpha[N-1] % AB[cospa[N-1][1]][0] != 0:
    replic[N-1] = (alpha[N-1] // AB[cospa[N-1][1]][0]) + 1
else:
    replic[N-1] = (alpha[N-1] // AB[cospa[N-1][1]][0])
dpzero += replic[N-1] * AB[cospa[N-1][1]][1]
"""
print(dpzero)
print(replic)
print(alpha)
print(cospa)
print(AB)
"""

dp = [0]*N
dp[0] = dpzero
for i in range(1, N):
    dp[i] = min( dp[i-1], (dp[i-1] - AB[cospa[N-i][1]][1] * replic[N-i] + AB[cospa[N-i-1][1]][1]) )
print(dp[N-1])

Submission status: Wrong Answer

Solution code:
def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = [-1]*(H + 1)
    dp[0] = 0
    for i in range(1, H + 1):
        ans = dp[i]
        for j in range(N):
            a = AB[2 * j + 0]
            b = AB[2 * j + 1]
            k = i - a
            if k < 0:
                continue
            if dp[k] < 0:
                continue
            v = dp[k] + b
            if ans == -1:
                ans = v
            else:
                ans = min(ans, v)
        dp[i] = ans
    print(dp[H])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,n=map(int,input().split())
a=[0]
b=[0]
for i in range(n):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
dp=[[10**9 for j in range(h+max(a)+1)] for i in range(n+1)]
for i in range(n+1):
    dp[i][0]=0
for i in range(n):
    for j in range(h+max(a)+1):
        if j>=a[i+1]:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i+1]]+b[i+1])
ans=10**9
for j in range(h,h+max(a)+1):
    if ans>dp[n][j]:
        ans=dp[n][j]
print(ans)

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n)]
A = [a for a, b in AB]
B = [b for a, b in AB]
inf = 10**10
# DP[i][j]=i個以内で体力j以上削るための消費魔力の最小値
DP = [[inf for j in range(h+1)] for i in range(n+1)]
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][max(0, j-A[i])]+B[i])
print(DP[n][h])

Submission status: Wrong Answer

Solution code:
import math

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

C = 0
Y = 10**4
l = []
for i in range(N):
  A, B = map(int, input().split())
  l.append([A, B])
  if (A / B > C) or ((A / B == C) and (B <= Y)):
    C = A / B
    X = A
    Y = B
    n = H // X

R = H - n*X
if R == 0:
  print(n*Y)
if R > 0:
  Z = 10**4
  for A, B in l:
    m = math.ceil(R / A)
    if m*B <= Z:
      Z = m*B
  print(n*Y + Z)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    inf = 10**17
    #mod = 10**9 + 7

    h,n = map(int, input().split())
    h = 10**4
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp = [inf] * (3*h)
    dp[0] = 0
    for i in range(3*h):
        for a,b in ab:
            if i-a >= 0:
                dp[i] = min(dp[i], dp[i-a]+b)
    res = inf
    for i in range(h, 3*h):
        res = min(res, dp[i])
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    inf = 10**17
    #mod = 10**9 + 7

    h,n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp = [inf] * (3*h)
    dp[0] = 0
    for i in range(3*h):
        for a,b in ab:
            if i-a >= 0:
                dp[i] = min(dp[i], dp[i-a]+b)
    res = inf
    for i in range(h, 3*h):
        res = min(res, dp[i])
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

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

data = []

for i in range(N):
    a, b = map(int, input().split())
    data.append({'a': a, 'b': b})

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

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

for j in range(H+1):
    dp[0][j] = math.ceil(j / data[0]['a']) * data[0]['b']

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

        power = data[i]['a']
        magic = data[i]['b']
        temp = math.ceil(j / power)

        for m in range(1, temp):
            if dp[i][j] > dp[i-1][j - m * power] + m * magic:
                dp[i][j] = dp[i-1][j - m * power] + m * magic

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

Submission status: Wrong Answer

Solution code:
temp = input().split()
hp = int(temp[0])
n = int(temp[1])
inf = 10 ** 3
 
damage_arr = [0]*n
cost_arr = [0]*n
 
for i in range(n):
    temp = input().split()
    damage_arr[i] = int(temp[0])
    cost_arr[i] = int(temp[1])
 
dp = [[inf for j in range(hp+1)] for i in range(n+1)]
dp[0][0] = 0;
for i in range(n):
    for j in range(hp+1):
        if dp[i+1][j] > dp[i][j]:
            dp[i+1][j] = dp[i][j]
        if dp[i+1][min(j+damage_arr[i],hp)] > dp[i+1][j] + cost_arr[i]:
            dp[i+1][min(j+damage_arr[i],hp)] = dp[i+1][j] + cost_arr[i]
 
print(dp[n][hp])

Submission status: Wrong Answer

Solution code:
temp = input().split()
hp = int(temp[0])
n = int(temp[1])
inf = 10 ** 3

damage_arr = [0]*n
cost_arr = [0]*n

for i in range(n):
    temp = input().split()
    damage_arr[i] = int(temp[0])
    cost_arr[i] = int(temp[1])

dp = [[inf for j in range(hp+1)] for i in range(n+1)]
dp[0][0] = 0;
for i in range(n):
    for j in range(hp):
        if dp[i+1][j] > dp[i][j]:
            dp[i+1][j] = dp[i][j]
        if dp[i+1][min(j+damage_arr[i],hp)] > dp[i+1][j] + cost_arr[i]:
            dp[i+1][min(j+damage_arr[i],hp)] = dp[i+1][j] + cost_arr[i]

print(dp[n][hp])

Submission status: Wrong Answer

Solution code:
import decimal

temp = input().split()
hp = int(temp[0])
n = int(temp[1])

magic_array = [[0 for j in range(2)] for i in range(n)]

damage_arr = [0]*n
cost_arr = [0]*n

for i in range(n):
    temp = input().split()
    damage_arr[i] = int(temp[0])
    cost_arr[i] = int(temp[1])

dp = [[decimal.Decimal('inf') for j in range(hp+1)] for i in range(n+1)]
dp[0][0] = 0;
for i in range(n):
    for j in range(hp):
        if dp[i+1][j] > dp[i][j]:
            dp[i+1][j] = dp[i][j]
        if dp[i+1][min(j+damage_arr[i],hp)] > dp[i+1][j] + cost_arr[i]:
            dp[i+1][min(j+damage_arr[i],hp)] = dp[i+1][j] + cost_arr[i]

print(dp[n][hp])

Submission status: Wrong Answer

Solution code:
#E
#入力
H,N=map(int,input().split())
A=[]
B=[]
X=[list(map(int,input().split())) for i in range(N)]
X.sort(key=lambda x:x[0])

for i in X:
    A.append(i[0])
    B.append(i[1])
#デバッグ用関数
def pront(*n):
    if debug==1:
        print(n)


#デバッグ
debug=0#0:本番,1:デバッグ

#処理

P=[B[i]/A[i] for i in range(N)]

min_P=min(P)
min_P_index=P.index(min_P)

max_A=max(A)
min_A=min(A)

min_B=min(B)
min_B_index=B.index(min_B)


pront(P,min_P_index)

magic=0

while H>0:
    t=0
    if H>max_A:
        t=H//A[min_P_index]
        magic+=B[min_P_index]*t
        H-=A[min_P_index]*t
        
        pront(A[min_P_index],t,H,B[min_P_index])
        
    elif H<min_A:
        H-=A[min_B_index]
        magic+=min_B
        pront(A[min_B_index],1,H,B[min_B_index])
    else:
        for i in range(N):
            if A[i]>H:
                P[i]=B[i]/H
        min_P=min(P)
        min_P_index=P.index(min_P)
        magic+=B[min_P_index]
        H-=A[min_P_index]
        pront(A[min_P_index],1,H,B[min_P_index])

        
        
print(magic)

Submission status: Wrong Answer

Solution code:
W,N = list(map(int,input().split()))
table = []
for i in range(N):
    x = list(map(int,input().split()))
    table.append(x)    
dp = [[-1]*(W+1) for i in range (N+1)]
for i in range(W):
    dp[N][i] = 0
for i in range(N-1,-1,-1):
    for j in range(W+1):
        if j < table[i][1]:
            dp[i][j] = dp[i+1][j]
        else:
            dp[i][j] = max(dp[i+1][j],dp[i+1][j-table[i][1]]+table[i][0])
print(dp[0][W])

Submission status: Wrong Answer

Solution code:
W,N = list(map(int,input().split()))
table = []
for i in range(N):
    x = list(map(int,input().split()))
    table.append(x)    
dp = [[-1]*(W+1) for i in range (N+1)]
for i in range(W):
    dp[N][i] = 0
for i in range(N-1,-1,-1):
    for j in range(W+1):
        if j < table[i][1]:
            dp[i][j] = dp[i+1][j]
        else:
            dp[i][j] = max(dp[i+1][j],dp[i][j-table[i][1]]+table[i][0])
print(dp[0][W])

Submission status: Wrong Answer

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

lm=[[0 for i in range(h)] for j in range(n)]

for i in range(h):
    lm[0][i]=[-(-(i+1)//l[0][0])*l[0][1],-(-(i+1)//l[0][0])*l[0][0]] #[消費,与ダメージ]
print(lm)
def mi(j,i):
    ll=[]
    ll.append(lm[j-1][i])
    for p in range(1-(-(i+1)//l[j][0])):
        if i-p*l[j][0]>=0:
            ll.append([lm[j-1][i-p*l[j][0]][0]+p*l[j][1],lm[j-1][i-p*l[j][0]][1]+l[j][0]])
        else:
            ll.append([p*l[j][1],p*l[j][0]])
    print(j,i,ll)
    return sorted(ll)[0]

for j in range(1,n):
    for i in range(h):
        lm[j][i]=mi(j,i)
print(lm[n-1][h-1])

Submission status: Wrong Answer

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)
minA = min(A)
minB = min(B)
F = []
for i in range(h+1):
    if i == 0:
        F.append(0)
    else:
        f = 1 << 20
        for a, b in zip(A, B):
            if i <= a:
                f = min(f, b)
            else:
                f = min(f, F[i-a]+b)
        F.append(f)
print(F[h])

Submission status: Wrong Answer

Solution code:
h,n = map(int,input().split())
A,B = [0],[0]
for i in range(n):
  a,b = map(int,input().split())
  A.append(a)
  B.append(b)
max_a = max(A)
dp = [[0]+[float('inf')]*(h+max_a) for _ in range(n+1)]

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

Submission status: Wrong Answer

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)
minA = min(A)
minB = min(B)
F = []
for i in range(h+1):
    if i == 0:
        F.append(0)
    elif i <= minA:
        F.append(minB)
    else:
        f = 10**9
        for a, b in zip(A, B):
            try:
                f = min(f, F[i-a]+b)
            except:
                pass
        F.append(f)
print(F[h])

Submission status: Wrong Answer

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+1) for i in range(n+1)]
for i in range(n+1):
    dp[i][0]=0
for i in range(n):
    for j in range(h+1):
        if j-a[i]>=0:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i]]+b[i])
        else:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j])
print(dp[n][h])

Submission status: Wrong Answer

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+1) for i in range(n+1)]
for i in range(n+1):
    dp[i][0]=0
for i in range(n):
    for j in range(h+1):
        if j-a[i]>=0:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i]]+b[i])
        else:
            dp[i+1][j]=dp[i][j]
print(dp[n][h])

Submission status: Wrong Answer

Solution code:
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())

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

Submission status: Wrong Answer

Solution code:
import math

H,N = map(int,input().split())
ab = [list(map(int,input().split()))for _ in range(N)]
#search the worst efficency magic and its efficency
worstIndex = -1
worst = float('INF')
for i in range(N):
    a = ab[i][0]
    b = ab[i][1]
    if a/b < worst:
        worst = a/b
        worstIndex = i

a = ab[worstIndex][0]
b = ab[worstIndex][0]
maxB = math.ceil(H/a)*b

dp=[[0 for _ in range(maxB+1)]for i in range(N+1)]
for i in range(1,N+1):
  a = ab[i-1][0]
  b = ab[i-1][1]
  for j in range(1,maxB+1):
    maxK = math.floor(j/b)+1
    for k in range(maxK):
        dp[i][j] = max(dp[i][j],dp[i-1][j-b*k]+a*k)
    if dp[i][j] >= H:
        maxB = j
        break

print(maxB)

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
C=[0 for i in range(H+(10**4)+1)]
d=0
for i in [0]*N:
 A,B=map(int,input().split())
 for i in range(H+A+1):
  if A==i :
   C[i]=B
  elif A<i :
   if C[i]==0 and C[i-A]>0:
    C[i]=C[i-A]+B
   elif C[i-A]>0:
    C[i]=min(C[i],C[i-A]+B)
 d=max(d,A)
e=10**9
for i in range(H+1,H+d+1):
 if C[i]>0:
  e=min(C[i],e)
print(e)

Submission status: Wrong Answer

Solution code:
import sys

h, n = map(int, input().split())
magic = []
a_max = 0
for i in range(n):
    a, b = map(int, input().split())
    a_max = max(a, a_max)
    magic.append((a, b))

dp = [float('inf')] * (h + a_max)
dp[0] = 0
for i in range(h):
    for a, b in magic:
        if i + a > h + a_max:
            continue
        dp[i + a] = min(dp[i] + b, dp[i + a])
        print(dp[i + a])

print(min(dp[h:]))