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

#include <algorithm>
#include <cstdio>
#include <iostream>
#define NAX 100000
using namespace std;
int n, c[NAX], T[NAX + 1];
int greedy(int);
void douteki(int);

int main() {
  int i, g, j, k, flag = 0;

  k = 1;
  while (1) {
    flag = 0;
    cin >> n;
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      cin >> c[i];
      if (i == 0 && c[i] != 1)
        flag = 1;
    }
    if (n == 1 && c[i - 1] == 1) {
      cout << "Case #" << k << ": OK" << endl;
      k++;
      continue;
    }

    if (flag == 1) {
      cout << "Case #" << k << ": Cannot pay some amount" << endl;
      k++;
      continue;
    } else {
      //     for(j=1;j<=100;j++){
      //    douteki(n);
      ///   cout << "greedy(" << j << ") =" << greedy(j) << endl;
      //   cout << "douteki("<< j << ")="<< T[j] << endl;
      // 	}

      douteki(n);
      for (j = 1; j <= 100000; j++) {
        if (greedy(j) > T[j]) {
          flag = 3;
          break;
        }
      }
    }
    /*
     for(i=1;i<n;i++){
        if(c[i]%c[i-1] != 0)flag = 2;
      }
*/
    if (flag == 0) {
      cout << "Case #" << k << ": OK" << endl;
      k++;
    } else {
      cout << "Case #" << k << ": Cannot use greedy algorithm" << endl;
      k++;
    }
  }
}

int greedy(int a) {
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a >= c[i]) {
      ans += a / c[i];
      a = a % c[i];
    }
  }
  return ans;
}

void douteki(int re) {
  for (int j = 0; j <= NAX; j++)
    T[j] = 10000;
  T[0] = 0;
  for (int i = 0; i < re; i++) {
    for (int j = c[i]; j <= NAX; j++) {
      T[j] = min(T[j], T[j - c[i]] + 1);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define NAX 500000
using namespace std;
int n, c[NAX], T[NAX + 1];
int greedy(int);
void douteki(int);

int main() {
  int i, g, j, k, flag = 0;

  k = 1;
  while (1) {
    flag = 0;
    cin >> n;
    if (n == 0)
      break;

    for (i = 0; i < n; i++) {
      cin >> c[i];
      if (i == 0 && c[i] != 1)
        flag = 1;
    }
    if (flag == 1) {
      cout << "Case #" << k << ": Cannot pay some amount" << endl;
      k++;
      continue;
    } else {
      //  for(j=1;j<=100;j++){
      ///  douteki(n);
      // cout << "greedy(" << j << ") =" << greedy(j) << endl;
      //  cout << "douteki("<< j << ")="<< T[j] << endl;
      //}

      douteki(n);
      for (j = 1; j <= 500000; j++) {
        if (greedy(j) != T[j]) {
          flag = 3;
          break;
        }
      }
    }
    /*
     for(i=1;i<n;i++){
        if(c[i]%c[i-1] != 0)flag = 2;
      }
    */
    if (flag == 0) {
      cout << "Case #" << k << ": OK" << endl;
      k++;
    } else {
      cout << "Case #" << k << ": Cannot use greedy algorithm" << endl;
      k++;
    }
  }

  return 0;
}

int greedy(int a) {
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a >= c[i]) {
      ans += a / c[i];
      a = a % c[i];
    }
  }
  return ans;
}

void douteki(int re) {
  for (int j = 0; j <= NAX; j++)
    T[j] = 10000;
  T[0] = 0;
  for (int i = 0; i < re; i++) {
    for (int j = c[i]; j <= NAX; j++) {
      T[j] = min(T[j], T[j - c[i]] + 1);
    }
  }
}