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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX = 500000;
int dp[MAX];

int main() {
  int n;
  int N = 0;
  while (cin >> n, n) {
    int c[n];
    int M = 1;
    REP(i, n) {
      cin >> c[i];
      M += c[i];
    }
    sort(c, c + n);
    printf("Case #%d: ", ++N);
    if (c[0] != 1) {
      puts("Cannot pay some amount");
      continue;
    }
    REP(i, M) dp[i] = INF;
    dp[0] = 0;
    REP(i, n) {
      REP(j, M - c[i]) { dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1); }
    }
    // REP(i, MAX) cout << dp[i] << " ";
    bool dame = 0;
    REP(i, M) {
      int now = i;
      int cnt = 0;
      for (int j = n - 1; j >= 0; --j) {
        cnt += now / c[j];
        now %= c[j];
      }
      if (dp[i] != cnt) {
        dame = 1;
        break;
      }
    }
    if (!dame) {
      puts("OK");
    } else {
      puts("Cannot use greedy algorithm");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX = 500000;
int dp[MAX];

int main() {
  int n;
  int N = 0;
  while (cin >> n, n) {
    int c[n];
    REP(i, n) cin >> c[i];
    sort(c, c + n);
    printf("Case #%d: ", ++N);
    if (c[0] != 1) {
      puts("Cannot pay some amount");
      continue;
    }
    REP(i, MAX) dp[i] = INF;
    dp[0] = 0;
    REP(i, n) {
      REP(j, MAX - c[i]) { dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1); }
    }
    // REP(i, MAX) cout << dp[i] << " ";
    bool dame = 0;
    REP(i, MAX) {
      int now = i;
      int cnt = 0;
      for (int j = n - 1; j >= 0; --j) {
        cnt += now / c[j];
        now %= c[j];
      }
      if (dp[i] != cnt) {
        dame = 1;
        break;
      }
    }
    if (!dame) {
      puts("OK");
    } else {
      puts("Cannot use greedy algorithm");
    }
  }
}