Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// define
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define dump(x) cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX / 2)
#define PI (2 * acos(0.0))
#define EPS (1e-8)

#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(void) {
  ios_base::sync_with_stdio(0);
  int n, m;
  int coin[20];
  cin >> n >> m;
  rep(i, m) cin >> coin[i];

  int dp[50001];
  rep(i, 50001) dp[i] = INF;

  dp[0] = 0;

  rep(i, m) { REP(j, coin[i], n + 1) dp[j] = min(dp[j], dp[j - coin[i]] + 1); }

  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// define
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define dump(x) cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX / 2)
#define PI (2 * acos(0.0))
#define EPS (1e-8)

#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

int n, m;
int c[21];
int dp[50001][20];

int cnt = 0;

int solve(int pay, int coin_no) {
  if (pay == 0)
    return 0;
  if (pay < 0 || coin_no >= m)
    return INF;
  if (dp[pay][coin_no] != INF)
    return dp[pay][coin_no];

  int mini = INF;
  for (int i = 0; pay - c[coin_no] * i >= 0; i++) {
    mini = min(mini, i + solve(pay - c[coin_no] * i, coin_no + 1));
  }
  return dp[pay][coin_no] = mini;
}

int main(void) {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep(i, 50001) rep(j, 20) dp[i][j] = INF;

  cout << solve(n, 0) << endl;
  return 0;
}