Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SZ(i) ((int)i.size())
#define GI(i) (scanf("%d", &i))
#define GLL(i) (scanf("%lld", &i))
#define GD(i) (scanf("%lf", &i))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define GET0(x) (get<0>(x))
#define GET1(x) (get<1>(x))
#define GET2(x) (get<2>(x))
#define ALL(X) (X).begin(), (X).end()
#define LLMAX (1LL << 60)
#define LLMIN -(1LL << 60)
#define IMAX (1 << 30)
#define IMIN -(1 << 30)
typedef long long LL;
using namespace std;

int cost[11] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<string> cache(10000, string(""));
vector<int> A;

int comp(string s, string t) {
  if (s == "N")
    return false;
  if (t == "N")
    return true;
  if (s.size() < t.size())
    return false;
  else if (s.size() > t.size())
    return true;
  else {
    int n = s.size();
    REP(i, n) {
      if (s[i] > t[i])
        return true;
      else if (s[i] < t[i])
        return false;
    }
    return true;
  }
}

string rec(int N) {
  if (N == 0)
    return "";
  if (cache[N] != "")
    return cache[N];
  string res = "N";
  for (int a : A) {
    if (cost[a] <= N && rec(N - cost[a]) != "N") {
      string s = (char)('0' + a) + rec(N - cost[a]);
      if (comp(s, res))
        res = s;
    }
  }
  cache[N] = res;
  return res;
}

int main(void) {
  int N, M;
  GI(N);
  GI(M);
  vector<int> B(M);
  REP(i, M)
  GI(B[i]);
  A = B;
  string ans = rec(N);
  printf("%s\n", ans.c_str());
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SZ(i) ((int)i.size())
#define GI(i) (scanf("%d", &i))
#define GLL(i) (scanf("%lld", &i))
#define GD(i) (scanf("%lf", &i))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define GET0(x) (get<0>(x))
#define GET1(x) (get<1>(x))
#define GET2(x) (get<2>(x))
#define ALL(X) (X).begin(), (X).end()
#define LLMAX (1LL << 60)
#define LLMIN -(1LL << 60)
#define IMAX (1 << 30)
#define IMIN -(1 << 30)
typedef long long LL;
using namespace std;

int cost[11] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<string> cache(20000, string(""));
vector<int> A;

int comp(string s, string t) {
  if (s == "N")
    return false;
  if (t == "N")
    return true;
  if (s.size() < t.size())
    return false;
  else if (s.size() > t.size())
    return true;
  else {
    int n = s.size();
    REP(i, n) {
      if (s[i] > t[i])
        return true;
      else if (s[i] < t[i])
        return false;
    }
    return true;
  }
}

string rec(int N) {
  if (N == 0)
    return "";
  if (cache[N] != "")
    return cache[N];
  string res = "N";
  for (int a : A) {
    if (cost[a] <= N && rec(N - cost[a]) != "N") {
      string s = (char)('0' + a) + rec(N - cost[a]);
      if (comp(s, res))
        res = s;
    }
  }
  cache[N] = res;
  return res;
}

int main(void) {
  int N, M;
  GI(N);
  GI(M);
  vector<int> B(M);
  REP(i, M)
  GI(B[i]);
  A = B;
  string ans = rec(N);
  printf("%s\n", ans.c_str());
  return 0;
}