Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define Rep(i, n) for (int i = 0; i < n; i++)
#define For(i, n1, n2) for (int i = n1; i < n2; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define put(a) cout << a << endl;
#define all(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define TDARRAY(int, a, n, m) vector<vector<int>> a(n, vector<int>(m, 0));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m;

int main() {
  cin >> n >> m;
  vector<int> a(m);
  REP(i, m) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> b = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  map<int, int> mp;
  REP(i, m) {
    auto itr = mp.find(b[a[i]]);
    if (itr != mp.end()) {
      if (mp[b[a[i]]] < a[i]) {
        mp[b[a[i]]] = a[i];
      }
    } else {
      mp[b[a[i]]] = a[i];
    }
  }
  vector<vector<int>> dp(n + 1);
  for (auto itr : mp) {
    dp[itr.first].push_back(itr.second);
  }
  REP(i, n + 1) {
    if (!dp[i].empty()) {
      for (auto itr : mp) {
        if (i + itr.first <= n) {
          if (dp[i].size() + 1 > dp[i + itr.first].size()) {
            vector<int> temp = dp[i];
            temp.push_back(itr.second);
            dp[i + itr.first] = temp;
          } else if (itr.second > dp[i + itr.first].back()) {
            vector<int> temp = dp[i];
            temp.push_back(itr.second);
            dp[i + itr.first] = temp;
          }
        }
      }
    }
  }
  string res = "";
  ll l = dp[n].size();
  REP(i, l) { res.push_back((char)(dp[n][l - 1 - i] + 1 + '0')); }
  put(res);
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define Rep(i, n) for (int i = 0; i < n; i++)
#define For(i, n1, n2) for (int i = n1; i < n2; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define put(a) cout << a << endl;
#define all(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define TDARRAY(int, a, n, m) vector<vector<int>> a(n, vector<int>(m, 0));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m;

int main() {
  cin >> n >> m;
  vector<int> a(m);
  REP(i, m) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> b = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  map<int, int> mp;
  REP(i, m) {
    auto itr = mp.find(b[a[i]]);
    if (itr != mp.end()) {
      if (mp[b[a[i]]] < a[i]) {
        mp[b[a[i]]] = a[i];
      }
    } else {
      mp[b[a[i]]] = a[i];
    }
  }
  vector<vector<int>> dp(n + 1);
  for (auto itr : mp) {
    if (itr.first <= n) {
      dp[itr.first].push_back(itr.second);
    }
  }
  REP(i, n + 1) {
    if (!dp[i].empty()) {
      for (auto itr : mp) {
        if (i + itr.first <= n) {
          if (dp[i].size() + 1 > dp[i + itr.first].size()) {
            vector<int> temp = dp[i];
            temp.push_back(itr.second);
            dp[i + itr.first] = temp;
          } else if (itr.second > dp[i + itr.first].back()) {
            vector<int> temp = dp[i];
            temp.push_back(itr.second);
            dp[i + itr.first] = temp;
          }
        }
      }
    }
  }
  string res = "";
  ll l = dp[n].size();
  REP(i, l) { res.push_back((char)(dp[n][l - 1 - i] + 1 + '0')); }
  put(res);
  return 0;
}