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)

namespace AtCoderCS
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Linq;
    using static System.Console;
    using static System.Math;
    using static Macro;
    using static Ex;
    using static Library;

    class Program
    {
        void Solve()
        {
            var H = IN.Int;
            var N = IN.Int;
            var A = new int[N];
            var B = new int[N];
            REP(N, i =>
            {
                A[i] = IN.Int;
                B[i] = IN.Int;
            });
            var dp = new int[H + 1];
            REP(dp.Length, i => dp[i] = Int32.MaxValue);
            dp[H] = 0;
            for (var i = H; i >= 0; i--)
            {
                if(dp[i] == Int32.MaxValue) continue;
                for (var j = 0; j < N; j++)
                {
                    var idx = Max(0, i - A[j]);
                    dp[idx] = Min(dp[idx], dp[i] + B[j]);
                }
            }
            WriteLine(dp[0]);
        }

        private SimpleInput IN = new SimpleInput();
        private SimpleOutput OUT = new SimpleOutput();

        void Stream()
        {
            Solve();
            OUT.Flush();
        }

        static void Main(string[] args)
            => new Program().Stream();
    }


    #region IO

    class SimpleInput
    {
        private string[] _nextBuffer;
        private int _bufferCnt;
        private readonly char[] _cs = new char[] {' ', '"', ','};

        public SimpleInput()
        {
            _nextBuffer = new string[0];
            _bufferCnt = 0;
        }

        string Next()
        {
            if (_bufferCnt < _nextBuffer.Length) return _nextBuffer[_bufferCnt++];
            var st = ReadLine();
            while (st == string.Empty) st = ReadLine();
            if (st != null) _nextBuffer = st.Split(_cs, StringSplitOptions.RemoveEmptyEntries);
            _bufferCnt = 0;
            return _nextBuffer[_bufferCnt++];
        }

        public string String => Next();
        public char Char => char.Parse(String);
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
    }

    class SimpleOutput
    {
        private readonly StreamWriter _sw = new StreamWriter(OpenStandardOutput())
        {
#if DEBUG
            AutoFlush = true
#else
            AutoFlush = false
#endif
        };

        public SimpleOutput()
        {
            SetOut(_sw);
        }

        public void Flush() => Out.Flush();
    }

    #endregion

    #region MACRO

    static class Macro
    {
        public static void FOR(int a, int b, int n, Action<int> f)
        {
            for (int i = a; i < b; i += n) f(i);
        }

        public static void FOR(int a, int b, Action<int> f) => FOR(a, b, 1, f);
        public static void REP(int a, Action<int> f) => FOR(0, a, 1, f);
        public static IEnumerable<TResult> ZIP<T1, T2, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys,
            Func<T1, T2, TResult> f = null)
        {
            using (var e1 = xs.GetEnumerator())
            using (var e2 = ys.GetEnumerator())
                while (e1.MoveNext() && e2.MoveNext())
                    yield return f(e1.Current, e2.Current);
        }
 
        public static IEnumerable<Tuple<T1, T2>> ZIP<T1, T2>(IEnumerable<T1> xs, IEnumerable<T2> ys)
            => ZIP(xs, ys, (x, y) => new Tuple<T1, T2>(x, y));
 
        public static IEnumerable<TResult> ZIP<T1, T2, T3, TResult>(IEnumerable<T1> xs, IEnumerable<T2> ys,
            IEnumerable<T3> zs, Func<T1, T2, T3, TResult> f)
        {
            using (var e1 = xs.GetEnumerator())
            using (var e2 = ys.GetEnumerator())
            using (var e3 = zs.GetEnumerator())
                while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext())
                    yield return f(e1.Current, e2.Current, e3.Current);
        }
 
        public static IEnumerable<Tuple<T1, T2, T3>> ZIP<T1, T2, T3>(IEnumerable<T1> xs, IEnumerable<T2> ys,
            IEnumerable<T3> zs) => ZIP(xs, ys, zs, (x, y, z) => new Tuple<T1, T2, T3>(x, y, z));
    }

    #endregion

    #region Ex

    static class Ex
    {
        public static string AsString(this IEnumerable<char> xs) => new string(xs.ToArray());
        public static void Print<T>(this T x) => WriteLine(x);
        public static string Yn(this bool x, string y = "Yes", string n = "No") => x ? y : n;
        public static string YN(this bool x) => x.Yn("YES", "NO");
        public static IEnumerable<T[]> Windowed<T>(this IEnumerable<T> xs, int size)
        {
            if (size <= 0) throw new ArgumentException();
            var w = new T[size];
            var r = size - 1;
            var i = 0;
            using (var e = xs.GetEnumerator())
            {
                while (e.MoveNext())
                {
                    w[i] = e.Current;
                    i = (i + 1) % size;
                    if (r == 0)
                    {
                        var ret = new T[size];
                        Array.Copy(w, i, ret, 0, size - i);
                        Array.Copy(w, 0, ret, size - i, i);
                        yield return ret;
                    }
                    else r--;
                }
            }
        }
    }
    #endregion
    
    #region Library

    static class Library
    {
        public static int BinarySearch(int ng, int ok, Func<int, bool> f)
        {
            while (Math.Abs(ok - ng) > 1)
            {
                var mid = (ok + ng) / 2;
                if (f(mid)) ok = mid;
                else ng = mid;
            }

            return ok;
        }

        public static int LowerBound<T>(IReadOnlyList<T> a, T x) where T : IComparable<T> =>
            BinarySearch(-1, a.Count, (m) => a[m].CompareTo(x) >= 0);

        public static int UpperBound<T>(IReadOnlyList<T> a, T x) where T : IComparable<T> =>
            BinarySearch(-1, a.Count(), (m) => a[m].CompareTo(x) > 0);
    }

    #endregion
}