Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define repi(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define repk(n) for (int k = 0; j < n; k++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, a, b) for (auto i = a; i < b; i++)
#define repv(itr) for (auto &&v : itr)
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
template <typename T> T minptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i < *re)
      re = i;
  }
  return re;
}
template <typename T> T maxptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i > *re)
      re = i;
  }
  return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
  using V = typename iterator_traits<T>::value_type;
  int n = end - begin;
  vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
  vector<int> id(n);
  if (lds && allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (lds) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  }
  int m = *maxptr(id.begin(), id.end());
  vector<T> re(m + 1);
  for (int i = n - 1; i >= 0; i--) {
    if (id[i] == m)
      re[m--] = begin + i;
  }
  return re;
}

bool comp(string a, string b) {
  return a.size() > b.size() || (a.size() == b.size() && a > b);
}
string dp[1010];
bool canuse[10];
const int usecount[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int main() {
  int n, m;
  cin >> n >> m;
  repi(m) {
    int x;
    cin >> x;
    canuse[x] = true;
  }
  dp[0] = "0";
  repi(n) {
    if (dp[i] == "")
      continue;
    string moto = dp[i] == "0" ? "" : dp[i];
    repr(j, 1, 10) {
      if (!canuse[j])
        continue;
      string ns = (char)(j + '0') + moto;
      if (comp(ns, dp[i + usecount[j]]))
        dp[i + usecount[j]] = ns;
      ns = moto + (char)(j + '0');
      if (comp(ns, dp[i + usecount[j]]))
        dp[i + usecount[j]] = ns;
    }
  }
  // repi(n + 1) cout << "dp[" << i << "]:" << dp[i] << endl;
  cout << dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define repi(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define repk(n) for (int k = 0; j < n; k++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, a, b) for (auto i = a; i < b; i++)
#define repv(itr) for (auto &&v : itr)
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
template <typename T> T minptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i < *re)
      re = i;
  }
  return re;
}
template <typename T> T maxptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i > *re)
      re = i;
  }
  return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
  using V = typename iterator_traits<T>::value_type;
  int n = end - begin;
  vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
  vector<int> id(n);
  if (lds && allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (lds) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  }
  int m = *maxptr(id.begin(), id.end());
  vector<T> re(m + 1);
  for (int i = n - 1; i >= 0; i--) {
    if (id[i] == m)
      re[m--] = begin + i;
  }
  return re;
}

bool comp(string a, string b) {
  return a.size() > b.size() || (a.size() == b.size() && a > b);
}
string dp[10010];
bool canuse[10];
const int usecount[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int main() {
  int n, m;
  cin >> n >> m;
  repi(m) {
    int x;
    cin >> x;
    canuse[x] = true;
  }
  dp[0] = "0";
  repi(n) {
    if (dp[i] == "")
      continue;
    string moto = dp[i] == "0" ? "" : dp[i];
    repr(j, 1, 10) {
      if (!canuse[j])
        continue;
      string ns = (char)(j + '0') + moto;
      if (comp(ns, dp[i + usecount[j]]))
        dp[i + usecount[j]] = ns;
      ns = moto + (char)(j + '0');
      if (comp(ns, dp[i + usecount[j]]))
        dp[i + usecount[j]] = ns;
    }
  }
  // repi(n + 1) cout << "dp[" << i << "]:" << dp[i] << endl;
  cout << dp[n] << endl;
}