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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) > 0 ? (a) : (-1 * (a)))
#define rep(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define rep2(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)

void arg_marge(int *, int *, int);

int main(void)
{

    int h, n, i, j;
    int *a, *a2, *b, *b2, *ab, *c;
    int *dp, *dp2;

    scanf("%d %d", &h, &n);
    a = (int *)malloc(n * sizeof(int));
    b = (int *)malloc(n * sizeof(int));
    a2 = (int *)malloc(n * sizeof(int));
    b2 = (int *)malloc(n * sizeof(int));
    ab = (int *)malloc(n * sizeof(int));
    c = (int *)malloc(n * sizeof(int));
    rep(i, n)
    {
        scanf("%d %d", a2 + i, b2 + i);
        ab[i] = (h * b2[i]) / a2[i];
        c[i] = i;
    }
    arg_marge(ab, c, n);
    rep(i, n)
    {
        a[i] = a2[c[i]];
        b[i] = b2[c[i]];
    }
    free(a2);
    free(b2);
    free(ab);
    free(c);
    int mx = ((h / a[0] + 1) * b[0]) + 1;
    dp = (int *)malloc(mx * sizeof(int));
    dp2 = (int *)malloc(mx * sizeof(int));
    rep(i, mx)
    {
        dp[i] = 0;
        dp2[i] = 0;
    }
    //rep(j, mx) printf("%d ", dp[j]);
    rep(i, n)
    {
        //printf("\n");
        rep(j, b[i]) dp2[j] = dp[j];
        rep2(j, b[i], mx)
        {
            dp2[j] = max(dp2[j - b[i]] + a[i], dp[j]);
            //if (dp2[j] >= h)
            //{
            //    printf("break\n");
            //    mx = i + 1;
            //    break;
            //}
        }
        rep(j, mx) dp[j] = dp2[j];
        //rep(j, mx) printf("%d ", dp[j]);
    }
    rep(i, mx) if (dp[i] >= h) break;
    printf("%d", i);
}

void arg_marge(int *a, int *b, int n)
{

    if (n == 1)
        return;

    int *l2, *r2, *tmp, *arg_l2, *arg_r2, *arg_tmp;
    l2 = (int *)malloc((n / 2) * sizeof(int));
    r2 = (int *)malloc((n - n / 2) * sizeof(int));
    tmp = (int *)malloc((n) * sizeof(int));
    arg_l2 = (int *)malloc((n / 2) * sizeof(int));
    arg_r2 = (int *)malloc((n - n / 2) * sizeof(int));
    arg_tmp = (int *)malloc((n) * sizeof(int));

    for (int i = 0; i < n / 2; i++)
    {
        l2[i] = a[i];
        arg_l2[i] = b[i];
    }
    for (int i = 0; i < n - n / 2; i++)
    {
        r2[i] = a[i + n / 2];
        arg_r2[i] = b[i + n / 2];
    }

    arg_marge(l2, arg_l2, n / 2);
    arg_marge(r2, arg_r2, n - n / 2);

    for (int i = 0; i < n / 2; i++)
    {
        tmp[i] = l2[i];
        arg_tmp[i] = arg_l2[i];
    }
    for (int i = n / 2; i < n; i++)
    {
        tmp[i] = r2[n - i - 1];
        arg_tmp[i] = arg_r2[n - i - 1];
    }

    int lp = 0, rp = n - 1, p = 0;
    int l3, r3;
    while (p < n)
    {
        l3 = tmp[lp];
        r3 = tmp[rp];
        if (l3 < r3)
        {
            a[p] = l3;
            b[p] = arg_tmp[lp];
            lp++;
            p++;
        }
        else
        {
            a[p] = r3;
            b[p] = arg_tmp[rp];
            rp--;
            p++;
        }
    }

    return;
}