Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

bool cmp(const pair<int, int> a, const pair<int, int> b) {
  int x = a.first * b.second, y = b.first * a.second;
  return x > y;
}
vector<pair<int, int>> v;

map<pair<int, int>, int> store;
int f(int j, int h) {
  if (j >= (int)v.size())
    return M;
  if (store.find({j, h}) != store.end())
    return store[{j, h}];
  int ans = M;
  int ini_h = h;
  // ans += (h/v[i].first)*v[i].second;
  // h = h%v[i].first;
  // int z = v[i].second;
  // if(!h) z = 0;
  // pr(i, h, ans + z);
  // ans = ans + z;
  for (int i = 0;; i++) {
    if (v[j].first * i > h) {
      int t = (h / v[j].first) * v[j].second;
      if (h % v[j].first)
        t += v[j].second;
      ans = min(ans, t);
      break;
    }
    int temp = v[j].first * i;
    int at = v[j].second * i + f(j + 1, h - temp);
    ans = min(ans, at);
  }
  store[{j, h}] = ans;
  return ans;
  // return min(ans + min(z, f(i+1, h)), f(i+1, ini_h));
}

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  int h, n;
  cin >> h >> n;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(all(v), cmp);
  cout << f(0, h) << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(), (c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

bool cmp(const pair<int, int> a, const pair<int, int> b) {
  int x = a.first * b.second, y = b.first * a.second;
  return x > y;
}
vector<pair<int, int>> v;

map<pair<int, int>, int> store;
int f(int j, int h) {
  if (j >= (int)v.size())
    return M;
  if (store.find({j, h}) != store.end())
    return store[{j, h}];
  int ans = M;
  int ini_h = h;
  // ans += (h/v[i].first)*v[i].second;
  // h = h%v[i].first;
  // int z = v[i].second;
  // if(!h) z = 0;
  // pr(i, h, ans + z);
  // ans = ans + z;
  for (int i = 0;; i++) {
    if (v[j].first * i > h) {
      int t = (h / v[j].first) * v[j].second;
      if (h % v[j].first)
        t += v[j].second;
      ans = min(ans, t);
      break;
    }
    int temp = v[j].first * i;
    int at = v[j].second * i + f(j + 1, h - temp);
    ans = min(ans, at);
  }
  store[{j, h}] = ans;
  return ans;
  // return min(ans + min(z, f(i+1, h)), f(i+1, ini_h));
}

int32_t main() {
  fastio;
  // freopen("file.in", "r", stdin);
  // freopen("file.out", "w", stdout);
  int h, n;
  cin >> h >> n;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  vector<int> knapsack(h + 1, M);
  knapsack[h] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = h; j >= 0; j--) {
      if (knapsack[j] != M) {
        int z = max(0LL, j - v[i].first);
        knapsack[z] = min(knapsack[z], knapsack[j] + v[i].second);
      }
    }
  }

  cout << knapsack[0] << "\n";

  return 0;
}