Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
namespace FastIO {
const char *ln = "\n";
struct Reader {
  char buf[1 << 20], *s, *t;
  bool EOF_FLG;
  Reader() : s(buf), t(buf), EOF_FLG(false){};
  char gt() {
    return s == t && ((t = (s = buf) + fread(buf, 1, 1 << 20, stdin)) == s)
               ? EOF
               : (*s++);
  }
  Reader &operator>>(char *str) {
    if (EOF_FLG)
      return *str = 0, *this;
    while ((*str = gt()) != ' ' && *str != '\n' && *str != EOF)
      ++str;
    if (*str == EOF)
      EOF_FLG = true;
    *str = 0;
    return *this;
  }
  template <typename T> Reader &operator>>(T &x) {
    if (EOF_FLG)
      return *this;
    char c = 0, d;
    while (c != EOF && (c < '0' || c > '9'))
      d = c, c = gt();
    if (c == EOF) {
      EOF_FLG = true;
      return *this;
    } else
      x = 0;
    while (c >= '0' && c <= '9')
      x = (x << 3) + (x << 1) + c - '0', c = gt();
    if (d == '-')
      x = -x;
    return *this;
  }
} in;
struct Writer {
  char buf[1 << 20], *s, *t;
  Writer() : s(buf), t(buf + (1 << 20)) {}
  ~Writer() { fwrite(buf, 1, s - buf, stdout); }
  void pt(char c) {
    (s == t) ? (fwrite(s = buf, 1, 1 << 20, stdout), *s++ = c) : (*s++ = c);
  }
  template <typename T> Writer &operator<<(T x) {
    if (!x)
      return pt('0'), *this;
    if (x < 0)
      pt('-'), x = -x;
    char a[40], t = 0;
    while (x)
      a[t++] = x % 10, x /= 10;
    while (t--)
      pt(a[t] + '0');
    return *this;
  }
  Writer &operator<<(const char *s) {
    while (*s)
      pt(*s++);
    return *this;
  }
} out;
} // namespace FastIO
using namespace FastIO;
namespace ZDY {
#define ll long long
#define db double
#define sht short
#define MB template <class T>
#define Fur(i, x, y) for (int i = x; i <= y; i++)
#define fur(i, x, y) for (i = x; i <= y; i++)
#define Fdr(i, x, y) for (int i = x; i >= y; i--)
#define fdr(i, x, y) for (i = x; i >= y; i--)
#define clr(x, y) memset(x, y, sizeof(x))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define inf 0x3f3f3f3f
#define fin(s) freopen(s ".in", "r", stdin)
#define fout(s) freopen(s ".out", "w", stdout)
#define l2(n) (int(log2(n)))
#define fl(i, x) for (int i = head[x], to; to = e[i].to, i; i = e[i].nxt)
#define fcin ios::sync_with_stdio(false)
MB T ABS(T x) { return x > 0 ? x : -x; }
MB T MAX(T x, T y) { return x >= y ? x : y; }
MB T MIN(T x, T y) { return x <= y ? x : y; }
MB T GCD(T x, T y) { return y ? GCD(y, x % y) : x; }
void SWAP(int &x, int &y) {
  x ^= y;
  y ^= x;
  x ^= y;
}
} // namespace ZDY
using namespace ZDY;
using namespace std;
#define N 10001
string f[N];
int n, m, a[10], c[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
void upd(string &x, string y) {
  if (x == "0")
    return (void)(x = y);
  if (x.size() < y.size())
    x = y;
  if (x.size() == y.size() && x < y)
    x = y;
}
int main() {
  in >> n >> m;
  Fur(i, 1, m) in >> a[i];
  Fur(i, 0, n) f[i] = "0";
  f[0] = "";
  Fur(i, 0, n) if (f[i] != "0") Fur(j, 1, m)
      upd(f[i + c[a[j]]], f[i] + char(a[j] + 48));
  cout << f[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
namespace FastIO {
const char *ln = "\n";
struct Reader {
  char buf[1 << 20], *s, *t;
  bool EOF_FLG;
  Reader() : s(buf), t(buf), EOF_FLG(false){};
  char gt() {
    return s == t && ((t = (s = buf) + fread(buf, 1, 1 << 20, stdin)) == s)
               ? EOF
               : (*s++);
  }
  Reader &operator>>(char *str) {
    if (EOF_FLG)
      return *str = 0, *this;
    while ((*str = gt()) != ' ' && *str != '\n' && *str != EOF)
      ++str;
    if (*str == EOF)
      EOF_FLG = true;
    *str = 0;
    return *this;
  }
  template <typename T> Reader &operator>>(T &x) {
    if (EOF_FLG)
      return *this;
    char c = 0, d;
    while (c != EOF && (c < '0' || c > '9'))
      d = c, c = gt();
    if (c == EOF) {
      EOF_FLG = true;
      return *this;
    } else
      x = 0;
    while (c >= '0' && c <= '9')
      x = (x << 3) + (x << 1) + c - '0', c = gt();
    if (d == '-')
      x = -x;
    return *this;
  }
} in;
struct Writer {
  char buf[1 << 20], *s, *t;
  Writer() : s(buf), t(buf + (1 << 20)) {}
  ~Writer() { fwrite(buf, 1, s - buf, stdout); }
  void pt(char c) {
    (s == t) ? (fwrite(s = buf, 1, 1 << 20, stdout), *s++ = c) : (*s++ = c);
  }
  template <typename T> Writer &operator<<(T x) {
    if (!x)
      return pt('0'), *this;
    if (x < 0)
      pt('-'), x = -x;
    char a[40], t = 0;
    while (x)
      a[t++] = x % 10, x /= 10;
    while (t--)
      pt(a[t] + '0');
    return *this;
  }
  Writer &operator<<(const char *s) {
    while (*s)
      pt(*s++);
    return *this;
  }
} out;
} // namespace FastIO
using namespace FastIO;
namespace ZDY {
#define ll long long
#define db double
#define sht short
#define MB template <class T>
#define Fur(i, x, y) for (int i = x; i <= y; i++)
#define fur(i, x, y) for (i = x; i <= y; i++)
#define Fdr(i, x, y) for (int i = x; i >= y; i--)
#define fdr(i, x, y) for (i = x; i >= y; i--)
#define clr(x, y) memset(x, y, sizeof(x))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define inf 0x3f3f3f3f
#define fin(s) freopen(s ".in", "r", stdin)
#define fout(s) freopen(s ".out", "w", stdout)
#define l2(n) (int(log2(n)))
#define fl(i, x) for (int i = head[x], to; to = e[i].to, i; i = e[i].nxt)
#define fcin ios::sync_with_stdio(false)
MB T ABS(T x) { return x > 0 ? x : -x; }
MB T MAX(T x, T y) { return x >= y ? x : y; }
MB T MIN(T x, T y) { return x <= y ? x : y; }
MB T GCD(T x, T y) { return y ? GCD(y, x % y) : x; }
void SWAP(int &x, int &y) {
  x ^= y;
  y ^= x;
  x ^= y;
}
} // namespace ZDY
using namespace ZDY;
using namespace std;
#define N 10001
string f[N + 100];
int n, m, a[10], c[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
void upd(string &x, string y) {
  if (x == "0")
    return (void)(x = y);
  if (x.size() < y.size())
    x = y;
  if (x.size() == y.size() && x < y)
    x = y;
}
int main() {
  in >> n >> m;
  Fur(i, 1, m) in >> a[i];
  Fur(i, 0, n) f[i] = "0";
  f[0] = "";
  Fur(i, 0, n) if (f[i] != "0") Fur(j, 1, m)
      upd(f[i + c[a[j]]], f[i] + char(a[j] + 48));
  cout << f[n] << endl;
}