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 sys
input = sys.stdin.readline


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

    dp = [[float("inf") for j in range(H+1)] for i in range(N+1)]
    for j in range(H+1):
        n = -(-j//A[0][0])
        dp[0][j] = n * A[0][1]
    for i in range(N):
        for j in range(H+1):
            if j < A[i][0]:
                dp[i+1][j] = A[i][1]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i+1][j-A[i][0]] + A[i][1])
#    [print(d) for d in dp]
    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
AB = []
 
for i in range(N):
    a,b = map(int,input().split())
    AB.append([a,b])
    
INF = 10**15
M = 2*10**4

dp = [[INF]*(M+1) for i in range(N+1)]

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

for i in range(N):
    A,B = AB[i]
    for j in range(A):
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
    for j in range(H):
        dp[i+1][j+A] = min(dp[i][j+A], dp[i+1][j]+B, dp[i+1][j+A])
        
ans = INF
for i in range(H,M):
    ans = min(ans,dp[N][i])
print(ans)

Submission status: Wrong Answer

Solution code:
import math
a,b = map(int,input().split())
arr = []
for i in range(b):
    x,y = map(int,input().split())
    arr.append((x,y))
arr.sort(key = lambda x : (x[1],-x[0]))
x,y = arr[0]
print(int((a/x)*y))

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
A = [0] * N
B = [0] * N 
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

# dp[i][h] := i-1 番目の魔法の中から，減らす体力が h を達成するように選んだ時の魔法の最小値
# DP テーブル
max_h = H + max(A)
dp = [[float("inf")] * (H + 1) for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for h in range(H + 1):
        if h < A[i]:
            dp[i+1][h] = dp[i][h]
        else:
            dp[i+1][h] = min(dp[i+1][h - A[i]] + B[i], dp[i][h])
##print(dp)
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, b = map(int, input().split())
    A[i] = a
    B[i] = b

# dp[i][h] := i-1 番目の魔法の中から，減らす体力が h を達成するように選んだ時の魔法の最小値
# DP テーブル
max_h = H + max(A)
dp = [[float("inf")] * (H + 1) for _ in range(N+1)]
dp[0][0] = 0

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

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()

h, n = map(int, inputs[0].split())
AB = []
for s in inputs[1:]:
    a, b = map(int, s.split())
    AB.append((a, b))
AB.sort(key=lambda ab:ab[1])

AB_ = [AB[0]]
x = AB[0][0] / AB[0][1]
for a, b in AB[1:]:
    if a/b > x:
        x = a/b
        AB_.append((a, b))
AB, n = AB_, len(AB_)

def calc_min_magic(h):
    val = float('inf')
    for j in range(n):
        a, b = AB[j]
        val = min(val, dp[h-a]+b if a <= h else b)
    return val

dp = [0] * (h+1)
for i in range(1, h+1):
    dp[i] = calc_min_magic(i)
print(dp[h])

Submission status: Wrong Answer

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


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

    dp = [[float("inf") for j in range(H+1)] for i in range(N+1)]
    for j in range(H+1):
        n = -(-j//A[0][0])
        dp[0][j] = n * A[0][1]
    for i in range(N):
        for j in range(H+1):
            if j < A[i][0]:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i+1][j-A[i][0]] + A[i][1])

    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h, n = map(int, input().split())
ab = []
ma = 0
for i in range(n):
    a, b = map(int, input().split())
    ma = max(ma, a)
    ab.append([a, b])

INF = 10 ** 9
dp = [[INF for i in range(h + ma + 1)] for j in range(n+1)]

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

Submission status: Wrong Answer

Solution code:
def main():
    h,n=map(int,input().split())
    L=[list(map(int,input().split())) for _ in range(n)]
    M=[l[0]/l[1] for l in L]
    cospa = M.index(max(M))
    ans=-(-h//L[0][0])*L[0][1]
    r = -(-h//L[cospa][0])
    for l in L:
        if l == L[cospa]:
            ans = min(ans, -(-h//l[0])*l[1])
        elif l[0]>h:
            ans = min(ans, l[1])
        else:
            for i in range(r+1):
                if l[0]*i<=h:
                    ans = min(ans, -(-(h-l[0]*i)//L[cospa][0])*L[cospa][1]+l[1]*i)
                else:
                    break
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
AB = []
 
for i in range(N):
    a,b = map(int,input().split())
    AB.append([a,b])
    
INF = 10**15
M = 2*10**4

dp = [[INF]*(M+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):
        A,B = AB[i]
        dp[i+1][j+A] = min(dp[i][j+A], dp[i+1][j]+B, dp[i+1][j+A])
        
ans = INF
for i in range(H,M):
    ans = min(ans,dp[N][i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h, n = map(int, input().split())
ab = []
ma = 0
for i in range(n):
    a, b = map(int, input().split())
    ma = max(ma, a)
    ab.append([a, b])

INF = 10 ** 9
dp = [[INF for i in range(h + ma + 1)] for j in range(n+1)]

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

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
MAXH = H+10000
DP = [float("inf")]*MAXH
DP[0] = 0
for i in range(N):
    a,b = map(int,input().split())
    for j in range(MAXH):
        if j+a>MAXH-1:break
        if j+a>H+j*j:break
        DP[j+a] = min(DP[j+a],DP[j]+b)
print(min(DP[H:]))

Submission status: Wrong Answer

Solution code:
def run():
  H, N = map(int, input().split())
  ab_li = [tuple(map(int, input().split())) for n in range(N)]
  dp = [float('inf')]*(H+1)
  for i in range(1, H+1):
    for n in range(N):
      a, b = ab_li[n]
      if i == a:
        dp[i] = min(dp[i], b)
      if (i < a) or (dp[i-a] == float('inf')):
        continue
      dp[i] = min(dp[i], dp[i-a]+b)
  ans = float('inf')
  if dp[-1] == float('inf'):
    for h in range(H, -1, -1):
      if dp[h] != float('inf'):
        ans = dp[h]+min(ab_li, key=lambda x:x[1])[1]
        break
  else:
    ans = dp[-1]
  for n in range(N):
    a, b = ab_li[n]
    if a > H:
      ans = min(ans, b)
  print(ans)
  
if __name__ == '__main__':
  run()

Submission status: Wrong Answer

Solution code:
h,n=map(int,input().split())
ans=[10**10 for i in range(h+1)]
ans[0]=0
for i in range(n):
    a,b=map(int,input().split())
    tmp=a
    while tmp<=h:
        ans[tmp]=min(ans[tmp-a]+b,ans[tmp])
        tmp+=1
print(ans[h])

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
ablist=[[0]*3 for _ in range(N)]
for i in range(N):
    ablist[i][0],ablist[i][1]=map(int,input().split())
for i in range(N):
    ablist[i][2]=ablist[i][0]/ablist[i][1]
    ablist.sort(key=lambda x: x[2] ,reverse=True)
m=0
i=0
while True:
    if H>=ablist[i][0]:
        H-=ablist[i][0]
        m+=ablist[i][1]
        c=i
    else:
        i+=1

    if i==N-1:
        break

box1=10001
if H>0:
    ablist.sort(key=lambda x:x[1])
for i in range(N):
    if ablist[i][0]>H:
        m+=ablist[i][1]
        break


print(m)

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 = [0 for i in range(H+1)]

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

print(dp[H])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter

h,n=map(int,input().split())
abc=[]
b_min=10000
for i in range(n):
    ab=list(map(int,input().split()))
    c=ab[0]/ab[1]
    ab+=[c]
    abc.append(ab)
    b_min=min(b_min,ab[1])
abc.sort(key=itemgetter(2,0),reverse=True)

ans=0
while h>0:
    if abc==[]:
        ans+=b_min
        break
    if h<abc[0][0]:
        del abc[0]
    else:
        h-=abc[0][0]
        ans+=abc[0][1]

print(ans)

Submission status: Wrong Answer

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

def dp(h,n,ls):
    table=[[0]*(h+1) for i in range(n)]
    atk,cost=ls[0][0],ls[0][1]
    for x in range(h+1):
        cost_of_this = cost*((x-1)//atk+1) if x!=0 else cost
        table[0][x]=cost_of_this
    for i in range(1,n):
        atk,cost=ls[i][0],ls[i][1]
        for j in range(h+1):
            cost_of_this=cost*((x-1)//atk+1) if j!=0 else cost
            if atk>=j:
                table[i][j]=table[i-1][j]
            else:
                table[i][j]=min(table[i-1][j], table[i][j-atk]+cost)
    return(table[-1][-1])

ans=dp(h,n,ls)
print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter

h,n=map(int,input().split())
abc=[]
for i in range(n):
    ab=list(map(int,input().split()))
    c=ab[0]/ab[1]
    ab+=[c]
    abc.append(ab)
abc.sort(key=itemgetter(2,0),reverse=True)

ans=0
while h>0:
    #print(abc[0],h)
    if h<abc[0][0] and abc[0]!=abc[-1]:
        del abc[0]
    else:
        h-=abc[0][0]
        ans+=abc[0][1]

print(ans)

Submission status: Wrong Answer

Solution code:
#E
#TLE、無駄が多い

h, n = list(map(int,input().split()))
lst = []
for i in range(n):
    a,b = list(map(int,input().split()))
    lst.append([a,b,a/b])

lst.sort(key=lambda x: x[2])
asn = 0

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

#なぜかループに入らない、完全にbreakの位置をミスっていた(いらなかった、、)
for i in range(n-1,-1,-1):
    Flag =True
    a = lst[i][0]
    b = lst[i][1]
    for j in range(h-1,-1,-1):
        if Flag:
            if dp[j] != float("Inf"):
                Flag =False
                for k in range(j,h):
                    if dp[k] != float("Inf"):
                        if dp[k] + b < dp[min(k+a,h)]:
                            dp[min(k+a,h)] = dp[k] + b
print(dp[h])
#print(dp)

Submission status: Wrong Answer

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

def dp(h,n,ls):
    table=[[0]*(h+1) for i in range(n)]
    atk,cost=ls[0][0],ls[0][1]
    for x in range(h+1):
        cost_of_this = cost*((x-1)//atk+1) if x!=0 else cost
        table[0][x]=cost_of_this
    for i in range(1,n):
        atk,cost=ls[i][0],ls[i][1]
        for j in range(h+1):
            cost_of_this=cost*((x-1)//atk+1) if j!=0 else cost
            if atk>=j:
                table[i][j]=min(table[i-1][j], cost_of_this)
            else:
                table[i][j]=min(table[i-1][j], table[i][j-atk]+cost, cost_of_this)
    return(table[-1][-1])

ans=dp(h,n,ls)
print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    h, n = map(int, input().split())
    lab = []
    ma = 0
    while True:
        try:
            a, b = map(int, input().split())
            lab.append([a, b])
            if a > 0:
                ma = a
        except:
            break
    dp = []
    for i in range(n + 1):
        dp.append([10 ** 7] * (h + ma))
    dp[0][0] = 0
    for i in range(n):
        for j in range(h + ma):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            if j >= lab[i][0]:
                dp[i+1][j] = min(dp[i+1][j-lab[i][0]] + lab[i][1], dp[i+1][j])
    mdp = 10 ** 7
    for i in range(h, h + ma):
        if dp[n][i] < mdp:
            mdp = dp[n][i]
    print(mdp)
resolve()

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
  H, N = map(int, input().split())
  damage= []
  cost = []
  for i in range(N):
    a,b = map(int, input().split())
    damage.append(a)
    cost.append(b)

  damage=np.array(damage)
  cost=np.array(cost)
  E = damage/cost
  best = E.argmax() 

  ma= math.ceil(H/damage[best]) * cost[best]
  count = np.array([0]*N)
  count[best] = math.ceil(H/damage[best])

  def search(count, damage, cost, E, H):
    at= count.argmax()
    count[at] = count[at]-1
    n= len(count)
    tmp_c_lists =np.array([count]*n)
    sum_list = np.array([0]*n)

    for i in range(n):
      while True:
        tmp_c_lists[i][i] = tmp_c_lists[i][i]+1
        if np.sum(tmp_c_lists[i]* damage) >= H:
          break
      sum_list[i] = np.sum(cost*tmp_c_lists[i])
    arg = np.argmin(sum_list)

    return np.sum(tmp_c_lists[arg]* cost), tmp_c_lists[arg]

  while True:
    new, count = search(count, damage, cost, E, H)
    if ma <= new:
      break
    else:
      ma =new
  print(ma)
  
if __name__=="__main__":
  main()

Submission status: Wrong Answer

Solution code:
inf = 10**9
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 = [[inf] * (h+1) for i in range(n)]
dp[0][0] = 0
for c in range(1, h+1):
  if c <= a[0]:
    dp[0][c] = b[0]
  else:
    break
for i in range(1, n):
  dp[i][0] = 0
  for c in range(1, h+1):
    dp[i][c] = min(dp[i-1][c], dp[i-1][max(0, c-a[i])] + b[i])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
AB = []
 
for i in range(N):
    a,b = map(int,input().split())
    AB.append([a,b])
    
INF = 10**15

dp = [[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):
        A,B = AB[i]
        dp[i+1][min(j+A,H)] = min(dp[i][min(j+A,H)], dp[i+1][j]+B, dp[i+1][min(j+A,H)])
        
print(dp[N][H])

Submission status: Wrong Answer

Solution code:
import math
H,N=map(int,input().split())
A=[]
B=[]
eff=[]
mp=0

for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    eff.append(math.ceil(H/a)*b)

while H>0:
    eff_min_index=eff.index(min(eff))
    tmp=math.floor(H/A[eff_min_index])
    H-=A[eff_min_index]
    mp+=B[eff_min_index]
    for i in range(N):
        eff[i]=math.ceil(H/A[i])*B[i]
print(mp)

Submission status: Wrong Answer

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

AB.sort(reverse=True, key=lambda x: x[0] / x[1])
result = H // AB[0][0] * AB[0][1]
H -= H // AB[0][0] * AB[0][0]

dp = [100000000] * (10 ** 5)
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
result += min(dp[H:])
print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    H,N = map(int, input().split())
    A = [None]*N
    B = [None]*N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    
    dp = [float("inf")] * (H+1)
    dp[-1] = 0

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

    print(dp[0])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ans = 0
dp = []
for _ in range(H + 1):
    dp.append(10000000)
dp[0] = 0

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

print(dp[H])

Submission status: Wrong Answer

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

def dp(h,n,ls):
    table=[[0]*(h+1) for i in range(n)]
    atk,cost=ls[0][0],ls[0][1]
    for x in range(h+1):
        cost_of_this = cost*((x-1)//atk+1) if x!=0 else cost
        table[0][x]=cost_of_this
    for i in range(1,n):
        atk,cost=ls[i][0],ls[i][1]
        for j in range(h+1):
            cost_of_this=cost*((x-1)//atk+1) if j!=0 else cost
            if atk>j:
                table[i][j]=min(table[i-1][j], cost_of_this)
            else:
                table[i][j]=min(table[i-1][j], table[i-1][j-atk]+cost, cost_of_this)
    return(table[-1][-1])

ans=dp(h,n,ls)
print(ans)

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
efficiency = []
dic = {}
cost = 0
for i in range(N):
    a, c = map(int, input().split())
    efficiency.append(a / c)
    dic[a / c] = [a, c]
efficiency = sorted(efficiency)
while H > 0:
    for eff in efficiency[::-1]:
        if H >= dic[eff][0]:
            H -= dic[eff][0]
            cost += dic[eff][1]
            break
    else: 
        min_cost = dic[efficiency[0]][1]
        for each in dic.values():
            if min_cost > each[1]:
                min_cost = each[1]
        cost += min_cost
        H = 0
print(cost)

Submission status: Wrong Answer

Solution code:
import math
def A():
    temp = [int(i) for i in input().split()]
    print(int(math.ceil(temp[0]/temp[1])))

def B():
    temp = [int(i) for i in input().split()]
    H = temp[0]
    N = temp[1]
    A = [int(i) for i in input().split()]
    if H <= sum(A):
        print('Yes')
    else:
        print('No')

def C():
    temp = [int(i) for i in input().split()]
    N = temp[0]
    K = temp[1]
    H = [int(i) for i in input().split()]
    if N <= K:
        print(0)
        return
    H.sort(reverse=True)
    print(sum(H[K:]))

def D():
    H = int(input())
    factor = 0
    ans = 0
    while True:
        ans += 2**factor
        if H == 1:
            break
        H = math.floor(H/2)
        factor += 1
    print(ans)

def E():
    temp = [int(i) for i in input().split()]
    H = temp[0]
    N = temp[1]
    magic = []
    for i in range(N):
        magic.append(tuple([int(i) for i in input().split()]))
    magic.sort(key = lambda x: x[1])
    attack = [k for k, v in magic]
    cost = [v for k,v in magic]

    min_cost = [[0 for i in range(H+1+max(attack))] for j in range(N+1)] 
    
    for i in range(H+1+max(attack)): 
        min_cost[0][i] = float('inf')
  
    for i in range(1, N+1): 
        min_cost[i][0] = 0

    for i in range(1, N+1): 
        for j in range(1, H+1+max(attack)): 

            if (attack[i-1] > j): 
                min_cost[i][j] = min_cost[i-1][j] 
  
            else: 
                min_cost[i][j] = min(min_cost[i-1][j], 
                    min_cost[i][j-attack[i-1]] + cost[i-1]) 
    
    #print(min_cost)
    if(min_cost[N][H] == float('inf')): 
        i = 1
        while H+i < H+1+max(attack):
            if min_cost[N][H+i] < float('inf'):
                print(min_cost[N][H+i])
                return
    else: 
        print(min_cost[N][H])

E()

Submission status: Wrong Answer

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 = [[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):
        A,B = AB[i]
        dp[i+1][min(j+A,H)] = min(dp[i][min(j+A,H)], dp[i+1][j]+B, dp[i+1][min(j+A,H)])
        
print(dp[N][H])

Submission status: Wrong Answer

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

dp = [0]*(h+1)
A = []
B = []

for _ in range(n):
  a,b = map(int, input().split())
  A.append(a)
  B.append(b)

init_min_cost = min(B)  
init_ind = B.index(init_min_cost)

dp = [init_min_cost]*(A[init_ind]+1)+[0]*(h-A[init_ind])

for ind_i in range(A[init_ind]+1, h+1):
  min_cost = float('inf')
  for i in range(n):
    if ind_i-A[i]>0:
      min_cost = min(min_cost, dp[ind_i-A[i]]+B[i])
      dp[ind_i] = min_cost
  
print(dp[-1])

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
A, B = [], []
for i in range(N):
    tmp = list(map(int, input().split()))
    A.append(tmp[0])
    B.append(tmp[1])

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

for i in range(N):
    for w in range(H+5):
        dp[i+1][w] = min(dp[i + 1][w], dp[i][w])
        if w >= A[i]:
            dp[i + 1][w] = min(dp[i + 1][w], dp[i + 1][w - A[i]] + B[i])
            
# for i in range(N):
#     for w in range(H):
#         if w >= A[i]:
#             dp[(i + 1) & 1][w] = min(dp[i & 1][w], dp[(i + 1) & 1][w - A[i]] + B[i])
#         else:
#             dp[(i+1) & 1][w] = dp[i & 1][w]
        
ans = []
for i in range(N):
    for w in range(H, H+5):
        ans.append(dp[i][w])
print(min(ans))

Submission status: Wrong Answer

Solution code:
def resolve():
    import numpy as np
    h, n = map(int, input().split())
    lab = np.zeros((n, 2), dtype=np.int)
    i = 0
    while True:
        try:
            a, b = map(int, input().split())
            lab[i][0] = a
            lab[i][1] = b
        except:
            break
        i += 1
    ma = np.max(lab[:, 0])
    dp = np.ones((n + 1, h + ma), dtype=np.int) * 10**7
    dp[0][0] = 0
    for i in range(n):
        for j in range(h + ma):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            if j >= lab[i][0]:
                dp[i+1][j] = min(dp[i+1][j-lab[i][0]] + lab[i][1], dp[i+1][j])
    print(np.min(dp[n, h:]))
resolve()

Submission status: Wrong Answer

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 = [[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):
        A,B = AB[i]
        dp[i+1][min(j+A,H)] = min(dp[i][min(j+A,H)],dp[i+1][j]+B)
        
print(dp[N][H])

Submission status: Wrong Answer

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 = [[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):
        dp[i+1][min(j+AB[i][0],H)] = min(dp[i][min(j+AB[i][0],H)],dp[i+1][j]+AB[i][1],dp[i+1][min(j+AB[i][0],H)])
        
print(dp[N][H])

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
A = []
for i in range(N):
    a,b = map(int,input().split())
    A.append((a,b))
A.sort()
MAXH = H*2
DP = [float("inf")]*(MAXH)
DP[0] = 0
for i in range(MAXH):
    for j in range(N):
        if i+A[j][0]>MAXH-1:break
        DP[i+A[j][0]] = min(DP[i]+A[j][1],DP[i+A[j][0]])
print(min(DP[H:]))

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
A = [0 for _ in range(N)]
B = [0 for _ in range(N)]
C = [0 for _ in range(N)]
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = b/a
P = zip(A, B, C)
SP = sorted(P, key=lambda x: x[2])
MaxMP = SP[0][1]
L = [[] for _ in range(N)]
k = 0
for i in range(N):
    if SP[i][1] <= MaxMP:
        L[k] = SP[i]
        k += 1
SSP = list(zip(*L[:k]))
INF = 1001001001
MP = [0 for _ in range(H+1)]
tmp = [INF for _ in range(N)]
for h in range(1, H+1):
    for n in range(k):
        if SSP[0][n] >= h:
            tmp[n] = SSP[1][n]
        else:
            tmp[n] = MP[h-SSP[0][n]]+SSP[1][n]

    MP[h] = min(tmp)
print(SP)
print(L[:k])
print(MP[H])

Submission status: Wrong Answer

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

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

ans = float('inf')
for j in range(h, 2 * h + 1):
    ans = min(ans, dp[j])
print(ans)

Submission status: Wrong Answer

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

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

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

    print(dp[H])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

dp = [1000000] * (10 ** 5)
dp[0] = 0
for i in range(H):
    if dp[i] == 1000000:
        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:]))

Submission status: Wrong Answer

Solution code:
import heapq


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


H, N = acinput()
s = []

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


koho = []
heapq.heappush(koho, (0, 0))
dist = [10**8]*(H+H+1)
dist[0] = 0
vmaxval = H+111111
res = 10**9
while len(koho) != 0:
    d, mini = heapq.heappop(koho)
    if dist[mini] < d:
        continue
    #print(d, mini)
    for h, m in s:
        k = mini+h
        #print(k,dist)
        if k>2*H:
            continue
        if dist[k] > dist[mini]+m:
            dist[k] = dist[mini]+m
            heapq.heappush(koho, (dist[k], k))
            if k >= H:
                res = min(res, k)
            # prev[k]=mini



for i in range(H,2*H):
    if dist[i]<10**8:
        print(dist[i])
        break

Submission status: Wrong Answer

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

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

ans = float('inf')
for j in range(h, 2 * h + 1):
    ans = min(ans, dp[j])
print(ans)

Submission status: Wrong Answer

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


dp = [[10**9] * (H + 1) for _ in range(N + 1)]
for x in range(N + 1):
    dp[x][0] = 0
    #dp[0][0] = 0

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



#print(dp)
print(dp[N][H])
                    
#dp[i+1][j] = dp[i+1][j-1] + dp[i][j] - dp[i][j-1-a[i]]
#w = c(6,4,3,2)		# 重量
#v = c(19,15,12,8)	# 価値
#f = matrix(0, 5, 21)
#for(n in 1:4) {
#  for(T in 0:20) {
#    f[n+1,T+1] = f[n-1+1,T+1]
#    if(T/w[n] < 1) next          ### for 文はCと違い、1回は必ず実行される
#    for (j in 1:(T/w[n])) {
#       f[n+1,T+1] = max(f[n+1,T+1], j*v[n] + f[n-1+1,T-j*w[n]+1])
#    }
#  }
#}

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((b/a,a,b))

dp=[100000000 for i in range(h+1)]
dp[0]=0
for i in range(h):
    for q in range(n):
        power=L[q][1]
        magic=L[q][2]
        if i+power>h:
        	break
        nex=min(h,i+power)
        dp[nex]=min(dp[nex],dp[i]+magic)

print(dp[h])

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import bisect
import sys
sys.setrecursionlimit(10**6) 

h,n = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]
li.sort(key=lambda x:(x[0],x[1]))
mae = li[0][0]
lin = [li[0]]
for i in range(1,n):
    if li[i][0] != mae:
        mae = li[i][0]
        lin.append(li[i])
a = []
b = []
c = []
for i in lin:
    a.append(i[0])
    b.append(i[1])
    c.append(i[1]/i[0])
point = 0
while h > 0:
    if bisect.bisect_left(a,h) == 0:
        ima = 0
    else:
        ima = c.index(min(c[:bisect.bisect_left(a,h)]))
    h -= a[ima]
    point += b[ima]
print(point)

Submission status: Wrong Answer

Solution code:
H,N = list(map(int,input().split()))
A = []
B = []
C = []
for i in range(N):
    a,b = list(map(int,input().split()))
    c = a/b
    A.append(a)
    B.append(b)
    C.append(c)
Amax = a
#print(A,B,C)
mp = 0
while H>0:
    #print("-----------------------------")
    # すべてのAについてHを超えるものは、A = H とする
    for i in range(N):
        if A[i] > H:
            A[i] = H
            C[i] = A[i]/B[i]
    #print("A:",A)
    #print("B:",B)
    #print("C:",C)
    # コスパ最大の攻撃手法を決定
    idx = C.index(max(C)) 
    #print(idx)
    # 攻撃回数の計算
    n_attack = (H-max(A))//A[idx]+1
    #print("n_attack is",n_attack)
    H -= n_attack*A[idx]
    mp += n_attack*B[idx]
    #print("H={},mp={}".format(H,mp))
#print("H={},mp={}".format(H,mp))
print(mp)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

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

magics = []
costs = []
for i in range(N):
    strength, cost = [int(i) for i in input().split()]
    performance = strength / cost
    magics.append([strength, cost, performance])
    costs.append(cost)

magics = sorted(magics, key = itemgetter(2), reverse=True)

cnt = 0

for i in range(N):
    if magics[i][0] <= H:
        q = H // magics[i][0]
        H = H - (magics[i][0] * q)
        cnt += q * magics[i][1]
    if H <= 0:
        break
if H > 0:
    cnt += min(costs)
print(cnt)