Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define forn(i, n) for (int i = 0; i < n; i++)
#define fornd(i, n) for (int i = n - 1; i >= 0; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define for1d(i, n) for (int i = n; i >= 1; i--)
#define fi first
#define se second
#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;

const int maxn = 100010;
int c[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6}, n, a[11];

struct node {
  int cnt[11] = {0}, size = 0;
};

node dp[1010];

bool grt(node a, node b) {
  if (a.size != b.size)
    return a.size > b.size;
  for1d(i, 9) if (a.cnt[i] != b.cnt[i]) return a.cnt[i] > b.cnt[i];
  return false;
}

void print(node a) {
  for1d(i, 9) while (a.cnt[i]--) cerr << i;
  cerr << ' ';
}

int main() {
  int ndig;
  cin >> n >> ndig;
  forn(i, n) scanf("%d", &a[i]);
  for1(i, n) {
    forn(j, ndig) {
      if (i >= c[a[j]]) {
        if (dp[i - c[a[j]]].size == 0 && i - c[a[j]])
          continue;
        node tmp;
        for1(k, 9) tmp.cnt[k] = dp[i - c[a[j]]].cnt[k];
        tmp.cnt[a[j]]++;
        tmp.size = dp[i - c[a[j]]].size;
        tmp.size++;
        // cerr << i << ' '; print(tmp); print(dp[i]); cerr << tmp.size << ' '
        // << dp[i].size << endl;
        if (grt(tmp, dp[i])) {
          for1(k, 9) dp[i].cnt[k] = tmp.cnt[k];
          dp[i].size = tmp.size;
        }
      }
    }
  }
  // for1(i, n) { for1d(j, 9) while (dp[i].cnt[j]--) printf("%d", j); cerr << '
  // '; }
  for1d(i, 9) while (dp[n].cnt[i]--) printf("%d", i);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define forn(i, n) for (int i = 0; i < n; i++)
#define fornd(i, n) for (int i = n - 1; i >= 0; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define for1d(i, n) for (int i = n; i >= 1; i--)
#define fi first
#define se second
#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;

const int maxn = 100010;
int c[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6}, n, a[11];

struct node {
  int cnt[11] = {0}, size = 0;
};

node dp[10010];

bool grt(node a, node b) {
  if (a.size != b.size)
    return a.size > b.size;
  for1d(i, 9) if (a.cnt[i] != b.cnt[i]) return a.cnt[i] > b.cnt[i];
  return false;
}

void print(node a) {
  for1d(i, 9) while (a.cnt[i]--) cerr << i;
  cerr << ' ';
}

int main() {
  int ndig;
  cin >> n >> ndig;
  forn(i, n) scanf("%d", &a[i]);
  for1(i, n) {
    forn(j, ndig) {
      if (i >= c[a[j]]) {
        if (dp[i - c[a[j]]].size == 0 && i - c[a[j]])
          continue;
        node tmp;
        for1(k, 9) tmp.cnt[k] = dp[i - c[a[j]]].cnt[k];
        tmp.cnt[a[j]]++;
        tmp.size = dp[i - c[a[j]]].size;
        tmp.size++;
        // cerr << i << ' '; print(tmp); print(dp[i]); cerr << tmp.size << ' '
        // << dp[i].size << endl;
        if (grt(tmp, dp[i])) {
          for1(k, 9) dp[i].cnt[k] = tmp.cnt[k];
          dp[i].size = tmp.size;
        }
      }
    }
  }
  // for1(i, n) { for1d(j, 9) while (dp[i].cnt[j]--) printf("%d", j); cerr << '
  // '; }
  for1d(i, 9) while (dp[n].cnt[i]--) printf("%d", i);
  return 0;
}