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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(dead_code)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(dead_code)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        h: i64,
        n: usize,
        magic: [(i64, i64); n],
    }

    // magic.sort_by(|&(la, lb), &(ra, rb)| {
    //     use std::cmp::Ordering::*;
    //     let lv = (la as f64) / (lb as f64);
    //     let rv = (ra as f64) / (rb as f64);
    //     // ダメージ効率が高い順
    //     match rv.partial_cmp(&lv).expect("error: l: ({}, {}), r: ({}, {})") {
    //         Equal => {
    //             // ダメージ効率が同じなら、コストが低い順
    //             rb.cmp(&lb)
    //         },
    //         others => others
    //     }
    // });
    // println!("ordered magic: {:?}", magic);

    // // 動的計画法前にボコボコにする（いらないきがする）
    // let max_power = magic.iter().map(|&(dmg, _cost)| dmg).max().unwrap();
    // println!("over");
    // let (dmg1, mp1) = magic[0];
    // let k = max((h - max_power) / dmg1, 0);
    // let used = (mp1 * k) as i64;
    // h -= dmg1 * k;
    // println!("k: {}, used: {}, h: {}", k, used, h);

    // DP
    // とりあえずでかい数
    let mut dp = vec![1_000_000_000; 1 + h as usize];
    // ダメージ0でいいならMP0
    dp[0] = 0;
    for v in 1..(h + 1) {
        // let mut mv = vec![1_000_000_000; n + 2];
        let mut min_mp = 1_000_000_000;
        // i番目の魔法を使うときの最小MPをresultにもとめて、それがvダメージ与える最小MPならmin_mpを更新する
        for i in 0..n {
            let current_magic = magic[i];
            let (cma, cmb) = (current_magic.0, current_magic.1);
            // let before = mv[i];
            // let mut vmut = v;
            // let mut mp = 0;
            let others = if v > cma {
                dp[(v - cma) as usize]
            } else {
                0
            };
            min_mp = min(min_mp, cmb + others);
        }
        // let result = mv.into_iter().min().unwrap();
        dp[v as usize] = min_mp;
    }

    // DEBUG
    // for (i, &d) in dp.iter().enumerate() {
    //     println!("i: {:3}, d: {:6}", i, d);
    // }
    // DEBUG END

    // println!("{}", used + dp[h as usize]);
    println!("{}", dp[h as usize]);
}