Problem p02787 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Og,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
template <typename T>
using os =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef double ld;
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define umi unordered_map<ll, ll, custom_hash>
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define make make_pair
#define ff first
#define ss second
#define osl os<ll>
#define ospii os<pair<ll, ll>>
#define endl "\n"
#define n_l '\n'
#define dbg(...)                                                               \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
  return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
  string t = "";
  for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
       __iii__ <= __jjj__; ++__iii__) {
    t += b[__iii__] + '0';
  }
  return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(const pair<A, B> &p) {
  l_v_l_v_l++;
  string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
  l_v_l_v_l--;
  return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
  int rnk = rank<A>::value;
  string tab(t_a_b_s, ' ');
  string res = "";
  bool first = true;
  if (l_v_l_v_l == 0)
    res += n_l;
  res += tab + "[";
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e)) {
    if (!first) {
      res += ", ";
    }
    first = false;
    l_v_l_v_l++;
    if (e != l) {
      if (rnk > 1) {
        res += n_l;
        t_a_b_s = l_v_l_v_l;
      };
    } else {
      t_a_b_s = 0;
    }
    res += to_string(*e, coords...);
    l_v_l_v_l--;
  }
  res += "]";
  if (l_v_l_v_l == 0)
    res += n_l;
  return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) {
  cout << to_string(H) << " | ";
  dbgm(T...);
}
#define dbgm(...)                                                              \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  dbgm(__VA_ARGS__);                                                           \
  cout << endl
clock_t time_p = clock();
void Auriel() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {

    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <typename R> void println(R r) {
  for (auto i : r)
    cout << i << " ";
  cout << endl;
}
template <typename R> void print(R r) {
  int e = 0;
  for (auto i : r)
    cout << i << endl;
}
template <typename R> void scnln(R &r) {
  for (auto &i : r)
    cin >> i;
}
ll st[200005] = {0}, ft[200005] = {0}, parent[200005], tt = 0,
   level[200005] = {0}, fans = 0;
ll binexp(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
// ll dp[100001];
void dfs(vector<pair<ll, ll>> graph[], ll start, ll *visit, ll color) {
  visit[start] = 1;
  level[1] = 0;
  st[start] = tt;
  tt++;
  for (auto child : graph[start]) {
    if (!visit[child.ff]) {
      // level[child]=level[start]+1;
      //  parent[child]=start;
      //  tans+=b[child];
      if (child.ss == color)
        dfs(graph, child.ff, visit, color);
    }
  }
  ft[start] = tt;
}
ll depth[20005];
void make_set(int v) {
  parent[v] = v;
  depth[v] = 0;
}
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (depth[a] < depth[b])
      swap(a, b);
    parent[b] = a;
    if (depth[a] == depth[b])
      depth[a]++;
  }
}
void dijikstra(vector<pair<ll, ll>> graph[], ll start, ll *visit, ll *cost) {
  cost[start] = 0;
  // visit[start]=1;
  multiset<pair<ll, ll>> ms;
  ms.insert({0, start});
  while (ms.size() != 0) {
    pair<ll, ll> p = *ms.begin();
    ms.erase(ms.begin());
    ll x = p.second; // ll w=p.second;
    if (visit[x] == 1)
      continue;
    visit[x] = 1;
    for (int i = 0; i < graph[x].size(); i++) {
      ll e = graph[x][i].first;
      ll w = graph[x][i].second;
      if (cost[x] + w < cost[e]) {
        cost[e] = w + cost[x];
        ms.insert({cost[e], e});
      }
    }
  }
}
int bfs1(vector<ll> graph[], ll *visit, ll *level, ll &ans, ll start) {
  queue<ll> q;
  // ll start=1;
  q.push(start);
  level[start] = 0;
  visit[start] = 1;
  while (q.size() != 0) {
    ll p = q.front();
    q.pop();
    for (auto it = graph[p].begin(); it != graph[p].end(); it++) {
      if (visit[*it] == 1)
        continue;
      level[(*it)] = level[p] + 1;
      ans = max(ans, level[*it]);
      if (ans == 5) {
        ll h = 90;
      }
      if (visit[(*it)] != 1) {
        q.push((*it));
        visit[(*it)] = 1;
      }
    }
  }
}
ll dp[10000000];
int main() {
  ios ll md = ll(1e15 + 7);
  rep(i, 0, 10000000) dp[i] = 999999999999;
  dp[0] = 0;
  ll h, n;
  cin >> h >> n;
  vi a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];
  rep(i, 0, n) dp[a[i]] = b[i];
  rep(i, 1, 100000) {
    rep(j, 0, n) {
      // if(i-a[j]>0)
      dp[i] = min(dp[min(0LL, i - a[j])] + b[j], dp[i]);
      if (dp[i] < 0)
        ll h = 89;
    }
  }
  ll ans = LLONG_MAX;
  rep(i, h, 100000) { ans = min(dp[i], ans); }
  cout << dp[h] << endl;

  Auriel();
}


Fixed submission:
// #pragma GCC optimize("Og,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
template <typename T>
using os =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef double ld;
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define umi unordered_map<ll, ll, custom_hash>
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define make make_pair
#define ff first
#define ss second
#define osl os<ll>
#define ospii os<pair<ll, ll>>
#define endl "\n"
#define n_l '\n'
#define dbg(...)                                                               \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; }
template <typename T> int SIZE(const T &t) { return t.size(); }
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
  return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
  string t = "";
  for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
       __iii__ <= __jjj__; ++__iii__) {
    t += b[__iii__] + '0';
  }
  return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B> string to_string(const pair<A, B> &p) {
  l_v_l_v_l++;
  string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
  l_v_l_v_l--;
  return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
  int rnk = rank<A>::value;
  string tab(t_a_b_s, ' ');
  string res = "";
  bool first = true;
  if (l_v_l_v_l == 0)
    res += n_l;
  res += tab + "[";
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e)) {
    if (!first) {
      res += ", ";
    }
    first = false;
    l_v_l_v_l++;
    if (e != l) {
      if (rnk > 1) {
        res += n_l;
        t_a_b_s = l_v_l_v_l;
      };
    } else {
      t_a_b_s = 0;
    }
    res += to_string(*e, coords...);
    l_v_l_v_l--;
  }
  res += "]";
  if (l_v_l_v_l == 0)
    res += n_l;
  return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) {
  cout << to_string(H) << " | ";
  dbgm(T...);
}
#define dbgm(...)                                                              \
  cout << "[" << #__VA_ARGS__ << "]: ";                                        \
  dbgm(__VA_ARGS__);                                                           \
  cout << endl
clock_t time_p = clock();
void Auriel() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {

    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <typename R> void println(R r) {
  for (auto i : r)
    cout << i << " ";
  cout << endl;
}
template <typename R> void print(R r) {
  int e = 0;
  for (auto i : r)
    cout << i << endl;
}
template <typename R> void scnln(R &r) {
  for (auto &i : r)
    cin >> i;
}
ll st[200005] = {0}, ft[200005] = {0}, parent[200005], tt = 0,
   level[200005] = {0}, fans = 0;
ll binexp(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
// ll dp[100001];
void dfs(vector<pair<ll, ll>> graph[], ll start, ll *visit, ll color) {
  visit[start] = 1;
  level[1] = 0;
  st[start] = tt;
  tt++;
  for (auto child : graph[start]) {
    if (!visit[child.ff]) {
      // level[child]=level[start]+1;
      //  parent[child]=start;
      //  tans+=b[child];
      if (child.ss == color)
        dfs(graph, child.ff, visit, color);
    }
  }
  ft[start] = tt;
}
ll depth[20005];
void make_set(int v) {
  parent[v] = v;
  depth[v] = 0;
}
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (depth[a] < depth[b])
      swap(a, b);
    parent[b] = a;
    if (depth[a] == depth[b])
      depth[a]++;
  }
}
void dijikstra(vector<pair<ll, ll>> graph[], ll start, ll *visit, ll *cost) {
  cost[start] = 0;
  // visit[start]=1;
  multiset<pair<ll, ll>> ms;
  ms.insert({0, start});
  while (ms.size() != 0) {
    pair<ll, ll> p = *ms.begin();
    ms.erase(ms.begin());
    ll x = p.second; // ll w=p.second;
    if (visit[x] == 1)
      continue;
    visit[x] = 1;
    for (int i = 0; i < graph[x].size(); i++) {
      ll e = graph[x][i].first;
      ll w = graph[x][i].second;
      if (cost[x] + w < cost[e]) {
        cost[e] = w + cost[x];
        ms.insert({cost[e], e});
      }
    }
  }
}
int bfs1(vector<ll> graph[], ll *visit, ll *level, ll &ans, ll start) {
  queue<ll> q;
  // ll start=1;
  q.push(start);
  level[start] = 0;
  visit[start] = 1;
  while (q.size() != 0) {
    ll p = q.front();
    q.pop();
    for (auto it = graph[p].begin(); it != graph[p].end(); it++) {
      if (visit[*it] == 1)
        continue;
      level[(*it)] = level[p] + 1;
      ans = max(ans, level[*it]);
      if (ans == 5) {
        ll h = 90;
      }
      if (visit[(*it)] != 1) {
        q.push((*it));
        visit[(*it)] = 1;
      }
    }
  }
}
ll dp[10000000];
int main() {
  ios ll md = ll(1e15 + 7);
  rep(i, 0, 10000000) dp[i] = 999999999999;
  dp[0] = 0;
  ll h, n;
  cin >> h >> n;
  vi a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];
  rep(i, 0, n) dp[a[i]] = b[i];
  rep(i, 1, 100000) {
    rep(j, 0, n) {
      // if(i-a[j]>0)
      dp[i] = min(dp[max(0LL, i - a[j])] + b[j], dp[i]);
      if (dp[i] < 0)
        ll h = 89;
    }
  }
  ll ans = LLONG_MAX;
  rep(i, h, 100000) { ans = min(dp[i], ans); }
  cout << dp[h] << endl;

  Auriel();
}