Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \
  int aa;                                                                      \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl(p)                                                               \
  long long aa;                                                                \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecps(p)                                                               \
  string aa;                                                                   \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecp2(p)                                                               \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl2(p)                                                              \
  long long a b;                                                               \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecps2(p)                                                              \
  string ab;                                                                   \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define set0(k, n)                                                             \
  for (int nn = 0; nn < (n); nn++) {                                           \
    (k).push_back(0);                                                          \
  }
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define vecsize(b, size) vector<int>(b)((size));
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsl(s) vector<pair<ll, ll>>(s);
#define pairss(s) vector<pair<string, string>>(s);
#define pairsp(s)                                                              \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairspl(s)                                                             \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairsps(s)                                                             \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairsREV(s) (s).push_back(make_pair(bb, aa));
#define pairslREV(s) (s).push_back(make_pair(bb, aa));
#define pairssREV(s) (s).push_back(make_pair(bb, aa));
#define MOD 1000000007

int N, M;
map<int, bool> S;
int A[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
vecs(ANS) int main() {

  cin >> N >> M;
  vec(C) fx(M){vecp(C)} sorts(C) vector<int> dp[2000];
  fy(N + 1) {
    fx(M) {
      if (y - A[C.at(x)] < 0) {
        continue;
      }
      if (dp[y - A[C.at(x)]].size() == 0 && y - A[C.at(x)] != 0) {
        continue;
      }
      if (dp[y - A[C.at(x)]].size() + 1 > dp[y].size()) {

        dp[y].clear();
        pb(dp[y], C.at(x)) fz(dp[y - A[C.at(x)]].size()) {
          pb(dp[y], dp[y - A[C.at(x)]].at(z))
        }
      } else if (dp[y - A[C.at(x)]].size() + 1 == dp[y].size()) {
        sorts(dp[y]) vector<int> TEMP;
        TEMP.clear();
        pb(TEMP, C.at(x)) fz(dp[y - A[C.at(x)]].size()){
            pb(TEMP, dp[y - A[C.at(x)]].at(z))} sorts(TEMP);
        bool flag = true;
        for (int i = TEMP.size() - 1; i >= 0; i--) {
          if (dp[y].at(i) < TEMP.at(i)) {
            flag = false;
            break;
          }
        }
        if (!flag) {
          dp[y].clear();
          for (int i = 0; i < TEMP.size(); i++) {
            // doublece(y,TEMP.at(i))
            pb(dp[y], TEMP.at(i))
          }
        }
      }
    }
  }

  sorts(dp[N]) reverses(dp[N]) fx(dp[N].size()) {
    // doublece(N,dp[N].at(x))
    cout << dp[N].at(x);
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \
  int aa;                                                                      \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl(p)                                                               \
  long long aa;                                                                \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecps(p)                                                               \
  string aa;                                                                   \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecp2(p)                                                               \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl2(p)                                                              \
  long long a b;                                                               \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecps2(p)                                                              \
  string ab;                                                                   \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define set0(k, n)                                                             \
  for (int nn = 0; nn < (n); nn++) {                                           \
    (k).push_back(0);                                                          \
  }
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define vecsize(b, size) vector<int>(b)((size));
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsl(s) vector<pair<ll, ll>>(s);
#define pairss(s) vector<pair<string, string>>(s);
#define pairsp(s)                                                              \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairspl(s)                                                             \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairsps(s)                                                             \
  int aa, bb;                                                                  \
  cin >> aa >> bb;                                                             \
  (s).push_back(make_pair(aa, bb));
#define pairsREV(s) (s).push_back(make_pair(bb, aa));
#define pairslREV(s) (s).push_back(make_pair(bb, aa));
#define pairssREV(s) (s).push_back(make_pair(bb, aa));
#define MOD 1000000007

int N, M;
map<int, bool> S;
int A[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
vecs(ANS) int main() {

  cin >> N >> M;
  vec(C) fx(M){vecp(C)} sorts(C) vector<int> dp[20000];
  fy(N + 1) {
    fx(M) {
      if (y - A[C.at(x)] < 0) {
        continue;
      }
      if (dp[y - A[C.at(x)]].size() == 0 && y - A[C.at(x)] != 0) {
        continue;
      }
      if (dp[y - A[C.at(x)]].size() + 1 > dp[y].size()) {

        dp[y].clear();
        pb(dp[y], C.at(x)) fz(dp[y - A[C.at(x)]].size()) {
          pb(dp[y], dp[y - A[C.at(x)]].at(z))
        }
      } else if (dp[y - A[C.at(x)]].size() + 1 == dp[y].size()) {
        sorts(dp[y]) vector<int> TEMP;
        TEMP.clear();
        pb(TEMP, C.at(x)) fz(dp[y - A[C.at(x)]].size()){
            pb(TEMP, dp[y - A[C.at(x)]].at(z))} sorts(TEMP);
        bool flag = true;
        for (int i = TEMP.size() - 1; i >= 0; i--) {
          if (dp[y].at(i) < TEMP.at(i)) {
            flag = false;
            break;
          }
        }
        if (!flag) {
          dp[y].clear();
          for (int i = 0; i < TEMP.size(); i++) {
            // doublece(y,TEMP.at(i))
            pb(dp[y], TEMP.at(i))
          }
        }
      }
    }
  }

  sorts(dp[N]) reverses(dp[N]) fx(dp[N].size()) {
    // doublece(N,dp[N].at(x))
    cout << dp[N].at(x);
  }
  cout << endl;
}