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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using static System.Math;
using static Math2;
using static io;


class Program
{
    static void Main(string[] args)
    {
        var wrong_answer = new wrong_answer();
        wrong_answer.Solve();
    }
}

public class wrong_answer
{
    const int INF = 1 << 29;
    const long INFL = 1L << 60;
    const int MOD = 1000000007;
    const int MOD2 = 998244353;



    public void Solve()
    {
        var o = intm;
        var h = o[0];
        var n = o[1];
        var dp = new int[h + 1];
        dp = dp.Select(x => x = INF).ToArray();
        dp[0] = 0;
        for (int i = 0; i < n; ++i)
        {
            var oo = intm;
            var a = oo[0];
            var b = oo[1];
            for (int j = 0; j <= h; ++j)
            {
                dp[Math.Min(h,j+a)] = Math.Min(dp[j] + b, dp[Math.Min(h, j + a)]);
            }
            
        }
        write(dp[h]);

    }

}






public static class Math2
{
    public static long pow(long i, long n, long MOD = 1000000007)
    {
        long res = 1;
        while (n > 0)
        {
            if ((n & 1) != 0) res = res * i % MOD;
            i = i * i % MOD;
            n >>= 1;
        }
        return res;
    }

    public static long gcd(long i, long y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public static long lcm(long i, long y)
    {
        return i * y / gcd(i, y);
    }

    public static BigInteger bgcd(BigInteger i, BigInteger y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public static BigInteger blcm(BigInteger i, BigInteger y)
    {
        return i * y / bgcd(i, y);
    }

    public static Dictionary<long, int> primefactorization(long N)
    {

        var ret = new Dictionary<long, int>();

        for (long i = 2; i * i <= N; ++i)
        {
            int cnt = 0;
            while (N % i == 0)
            {
                cnt++;
                N /= i;
            }
            if (cnt != 0) ret[i] = cnt;

        }
        if (N >= 2) ret[N] = 1;

        return ret;

    }

    public static List<long> divisorenumrate(long N)
    {
        var ret = new List<long>();

        for (long i = 1; i * i <= N; ++i)
        {

            if (N % i == 0)
            {
                ret.Add(i);
                ret.Add(N / i);
            }

        }

        return ret;

    }

    public static void swap<T>(ref T a, ref T b)
    {
        var i = a;
        a = b;
        b = i;
    }
    public static void chmin<T>(ref T a, T b) where T : IComparable
    {
        if (a.CompareTo(b) > 0) a = b;
    }
    public static void chmax<T>(ref T a, T b) where T : IComparable
    {
        if (a.CompareTo(b) < 0) a = b;
    }

}

public static class io
{
    //in
    public static string str => Console.ReadLine();

    public static string[] strm => str.Split(' ');

    public static long[] longm => strm.Select(long.Parse).ToArray();
    public static int[] intm => strm.Select(int.Parse).ToArray();
    public static char[] charm => str.ToArray();
    public static double[] doublem => strm.Select(double.Parse).ToArray();
    public static long long1 => longm[0];
    public static int int1 => intm[0];
    public static char char1 => charm[0];
    public static double double1 => doublem[0];
    public static void write(params object[] i) => Console.WriteLine(string.Join(" ", i));
    public static void write(object i) => Console.WriteLine(i);
    public static void yn(bool i) { if (i) write("Yes"); else write("No"); }

}