Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define ll long long

int cut(int x) {
  if (x > 0) {
    return x;
  } else {
    return 0;
  }
}

int main(void) {
  int h, n;
  cin >> h >> n;

  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];

  vector<int> dp(h + 1, 1000000000);

  for (int i = 0; i < n; ++i) {
    dp[h] = 0;
  }

  for (int h_i = h; h_i > -1; --h_i) {
    for (int i = 0; i < n; ++i) {
      if (dp[h_i] != 1000000000) {
        for (int i_next = 0; i_next < n; ++i_next) {
          dp[cut(h_i - a[i])] = min(dp[cut(h_i - a[i])], dp[h_i] + b[i]);
        }
      }
    }
  }

  cout << dp[0] << endl;

  /*
  vector<pair<double, int>> eff(n, pair<double, int>());
  for(int i = 0;i < n;++i){
      eff[i].first = (double)b[i] / (double)a[i];
      //cout << eff[i].first << endl;
      eff[i].second = i;
  }

  sort(eff.begin(), eff.end());

  int ans=0;
  int eff_i = 0;
  do{
      ans += h/a[eff[eff_i].second] * b[eff[eff_i].second];
      h = h%a[eff[eff_i].second];
      eff_i++;

      cout << h << " " << ans << endl;
  }while(h > 0 && eff_i < n);
  cout << endl;
  cout << ans << endl;


  if(h > 0){
      int min_cost = 1000000000;
      int min_i;
      for(int i = 0;i < n;++i){
          int cost = (int)ceil((double)h/(double)a[i]) * b[i];
          cout << cost << endl;
          if(min_cost > cost){
              min_cost = cost;
              min_i = i;
          }
      }
      ans += min_cost;
  }

  /*
  int min_b = 1000000000;
  if(h > 0){
      for(int i = 0;i < n;++i){
          if(min_b > b[i] && h-a[i] <= 0){
              min_b = b[i];
          }
      }
      ans += min_b;
  }*/

  /*
  int min_cost = 1000000000;
  int min_i;
  for(int i = 0;i < n;++i){
      int cost = (int)ceil((double)h/(double)a[i]) * b[i];
      if(min_cost > cost){
          min_cost = cost;
          min_i = i;
      }
  }

  ans += min_cost;
  */

  // cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define ll long long

int cut(int x) {
  if (x > 0) {
    return x;
  } else {
    return 0;
  }
}

int main(void) {
  int h, n;
  cin >> h >> n;

  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];

  vector<int> dp(h + 1, 1000000000);

  for (int i = 0; i < n; ++i) {
    dp[h] = 0;
  }

  for (int h_i = h; h_i > -1; --h_i) {
    for (int i = 0; i < n; ++i) {
      if (dp[h_i] != 1000000000) {
        dp[cut(h_i - a[i])] = min(dp[cut(h_i - a[i])], dp[h_i] + b[i]);
      }
    }
  }

  cout << dp[0] << endl;

  /*
  vector<pair<double, int>> eff(n, pair<double, int>());
  for(int i = 0;i < n;++i){
      eff[i].first = (double)b[i] / (double)a[i];
      //cout << eff[i].first << endl;
      eff[i].second = i;
  }

  sort(eff.begin(), eff.end());

  int ans=0;
  int eff_i = 0;
  do{
      ans += h/a[eff[eff_i].second] * b[eff[eff_i].second];
      h = h%a[eff[eff_i].second];
      eff_i++;

      cout << h << " " << ans << endl;
  }while(h > 0 && eff_i < n);
  cout << endl;
  cout << ans << endl;


  if(h > 0){
      int min_cost = 1000000000;
      int min_i;
      for(int i = 0;i < n;++i){
          int cost = (int)ceil((double)h/(double)a[i]) * b[i];
          cout << cost << endl;
          if(min_cost > cost){
              min_cost = cost;
              min_i = i;
          }
      }
      ans += min_cost;
  }

  /*
  int min_b = 1000000000;
  if(h > 0){
      for(int i = 0;i < n;++i){
          if(min_b > b[i] && h-a[i] <= 0){
              min_b = b[i];
          }
      }
      ans += min_b;
  }*/

  /*
  int min_cost = 1000000000;
  int min_i;
  for(int i = 0;i < n;++i){
      int cost = (int)ceil((double)h/(double)a[i]) * b[i];
      if(min_cost > cost){
          min_cost = cost;
          min_i = i;
      }
  }

  ans += min_cost;
  */

  // cout << ans << endl;

  return 0;
}