Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long int ll;
#define int long long
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
#define REP(i, n) for (int i = 0; i < (n); i++)
const int INF = 100000000000000000;

int N, M;
int A[1010];
int V[11] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[1010];
bool F[1010];

string comp(string s, string t) {
  if (s.size() > t.size()) {
    return s;
  } else if (s.size() < t.size()) {
    return t;
  } else {
    if (s > t) {
      return s;
    } else {
      return t;
    }
  }
}

string DP(int a) {
  string res;
  vector<string> v, v2;
  if (F[a] != false) {
    return dp[a];
  }
  if (a >= N) {
    if (a == N) {
      return "s";
    } else {
      return "!";
    }
  }
  REP(i, M) {
    string s;
    s = DP(a + V[A[i] - 1]) + to_string(A[i]);
    bool flag = false;
    REP(j, s.size()) {
      if (s[j] == '!') {
        flag = true;
      }
    }
    if (flag != true) {
      v.push_back(s);
    }
  }
  REP(i, v.size()) {
    if (comp(res, v[i]) == v[i]) {
      res = v[i];
    }
  }
  F[a] = true;
  return dp[a] = res;
}

signed main() {
  cin >> N >> M;
  REP(i, M) { cin >> A[i]; }
  string ans = DP(0);
  string ans2;
  REP(i, ans.size()) {
    if (ans[i] != 's') {
      ans2 += ans[i];
    }
  }
  cout << ans2 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long int ll;
#define int long long
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
#define REP(i, n) for (int i = 0; i < (n); i++)
const int INF = 100000000000000000;

int N, M;
int A[1010];
int V[11] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[1000000];
bool F[1000000];

string comp(string s, string t) {
  if (s.size() > t.size()) {
    return s;
  } else if (s.size() < t.size()) {
    return t;
  } else {
    if (s > t) {
      return s;
    } else {
      return t;
    }
  }
}

string DP(int a) {
  string res;
  vector<string> v, v2;
  if (F[a] != false) {
    return dp[a];
  }
  if (a >= N) {
    if (a == N) {
      return "s";
    } else {
      return "!";
    }
  }
  REP(i, M) {
    string s;
    s = DP(a + V[A[i] - 1]) + to_string(A[i]);
    bool flag = false;
    REP(j, s.size()) {
      if (s[j] == '!') {
        flag = true;
      }
    }
    if (flag != true) {
      v.push_back(s);
    }
  }
  REP(i, v.size()) {
    if (comp(res, v[i]) == v[i]) {
      res = v[i];
    }
  }
  F[a] = true;
  return dp[a] = res;
}

signed main() {
  cin >> N >> M;
  REP(i, M) { cin >> A[i]; }
  string ans = DP(0);
  string ans2;
  REP(i, ans.size()) {
    if (ans[i] != 's') {
      ans2 += ans[i];
    }
  }
  cout << ans2 << endl;
  return 0;
}