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

Solution in PyPy3 (2.4.0) (judge verdict: Time Limit Exceeded)

import unittest


class TestE(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(9, [[8, 3], [4, 2], [2, 1]]), 4)

    def test_2(self):
        self.assertEqual(think(100, [[1, 1], [2, 3], [3, 9], [4, 27], [5, 81], [6, 243]]), 100)

    def test_3(self):
        self.assertEqual(think(
            9999,
            [
              [540, 7550], [691, 9680], [700, 9790],
              [510, 7150], [415, 5818], [551, 7712],
              [587, 8227], [619, 8671], [588, 8228],
              [176, 2461]
            ]
        ), 139815)


def solve():
    h, list_of_a_and_b = read()
    result = think(h, list_of_a_and_b)
    write(result)


def read():
    h, n = read_int(2)
    list_of_a_and_b = []
    for _ in range(n):
        list_of_a_and_b.append(read_int(2))
    return h, list_of_a_and_b


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(h, list_of_a_and_b):
    min_of_a = 1
    max_of_b = 10 ** 4
    max_of_h = 10 ** 4
    inf = (max_of_h // min_of_a) * max_of_b + 1

    dp = [[inf for _ in range(h + 2)] for _ in range(max_of_h + 1)]
    dp[0][0] = 0

    # dp[i][max]; i is count of usage of magic; max is max of damage
    # dp[i][max] = minimum sum of magic power

    min_cost = inf

    for i in range(1, max_of_h + 1):
        for j in range(h + 2):
            if dp[i - 1][j] == inf:
                continue
            for a, b in list_of_a_and_b:
                if j + a >= len(dp[0]):
                    dp[i][h + 1] = min(dp[i][h + 1], dp[i - 1][j] + b)
                else:
                    dp[i][j + a] = min(dp[i][j + a], dp[i - 1][j] + b)
                min_cost = min(min_cost, dp[i][h], dp[i][h + 1])

    return min_cost


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()