Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAXN = 1001;
const int weight[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
struct Stat {
  short int cnt[10];
  int len;
  void push(int key) {
    cnt[key]++;
    len++;
  }
  Stat(int num = 0) {
    memset(cnt, 0, sizeof(cnt));
    len = 0;
    if (num)
      push(num);
  }
  Stat &operator=(const Stat &rhs) {
    len = rhs.len;
    memcpy(cnt, rhs.cnt, sizeof(cnt));
    return *this;
  }
  bool operator<(const Stat &rhs) const {

    if (len == rhs.len) {
      int pleft = 9, pright = 9;
      do {
        while (pleft && !cnt[pleft])
          pleft--;
        while (pright && !rhs.cnt[pright])
          pright--;
        if (pleft == pright) {
          if (cnt[pleft] != rhs.cnt[pright])
            return cnt[pleft] < rhs.cnt[pright];
        } else
          return pleft < pright;
      } while (pleft-- && pright--);
    }
    return len < rhs.len;
  }
} dp[MAXN];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int num;
    cin >> num;
    Stat init(num);
    if (dp[weight[num]] < init)
      dp[weight[num]] = init;
    for (int i = 1; i + weight[num] <= n; i++)
      if (dp[i].len) {
        Stat nextstat = dp[i];
        nextstat.push(num);
        if (dp[i + weight[num]] < nextstat)
          dp[i + weight[num]] = nextstat;
      }
  }
  // cout<<"Finish dp[n].len "<<dp[n].len<<endl;
  string ans = "";
  for (int i = 9; i > 0; i--)
    while (dp[n].cnt[i]--)
      ans += to_string(i);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAXN = 10001;
const int weight[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
struct Stat {
  short int cnt[10];
  int len;
  void push(int key) {
    cnt[key]++;
    len++;
  }
  Stat(int num = 0) {
    memset(cnt, 0, sizeof(cnt));
    len = 0;
    if (num)
      push(num);
  }
  Stat &operator=(const Stat &rhs) {
    len = rhs.len;
    memcpy(cnt, rhs.cnt, sizeof(cnt));
    return *this;
  }
  bool operator<(const Stat &rhs) const {

    if (len == rhs.len) {
      int pleft = 9, pright = 9;
      do {
        while (pleft && !cnt[pleft])
          pleft--;
        while (pright && !rhs.cnt[pright])
          pright--;
        if (pleft == pright) {
          if (cnt[pleft] != rhs.cnt[pright])
            return cnt[pleft] < rhs.cnt[pright];
        } else
          return pleft < pright;
      } while (pleft-- && pright--);
    }
    return len < rhs.len;
  }
} dp[MAXN];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int num;
    cin >> num;
    Stat init(num);
    if (dp[weight[num]] < init)
      dp[weight[num]] = init;
    for (int i = 1; i + weight[num] <= n; i++)
      if (dp[i].len) {
        Stat nextstat = dp[i];
        nextstat.push(num);
        if (dp[i + weight[num]] < nextstat)
          dp[i + weight[num]] = nextstat;
      }
  }
  // cout<<"Finish dp[n].len "<<dp[n].len<<endl;
  string ans = "";
  for (int i = 9; i > 0; i--)
    while (dp[n].cnt[i]--)
      ans += to_string(i);
  cout << ans << endl;
  return 0;
}