prithivMLmods/Coder-Stat huggingface dataset:
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, m, a[10] = {}, c[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}, d[10004] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a, a + m);
  reverse(a, a + m);
  for (int i = 1; i <= n; i++) {
    d[i] = -10004;
    for (int j = 0; j < m; j++)
      if (i >= c[a[j]])
        d[i] = max(d[i], d[i - c[a[j]]] + 1);
  }
  while (n) {
    for (int i = 0; i < m; i++)
      if (n >= a[i] && d[n - c[a[i]]] + 1 == d[n]) {
        cout << a[i];
        n -= c[a[i]];
        break;
      }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, m, a[10] = {}, c[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}, d[10004] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a, a + m);
  reverse(a, a + m);
  for (int i = 1; i <= n; i++) {
    d[i] = -10004;
    for (int j = 0; j < m; j++)
      if (i >= c[a[j]])
        d[i] = max(d[i], d[i - c[a[j]]] + 1);
  }
  while (n) {
    for (int i = 0; i < m; i++)
      if (n >= c[a[i]] && d[n - c[a[i]]] + 1 == d[n]) {
        cout << a[i];
        n -= c[a[i]];
        break;
      }
  }
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;
struct sss {
  string s;
} dp[10001];
bool flag[10];
bool cmp(string a, string b) {
  if (a.size() > b.size())
    return 1;
  else if (a.size() < b.size())
    return 0;
  else
    return a > b;
}
bool cmp1(char a, char b) { return a > b; }
int main() {
  int n, m;
  string x;
  map<int, string> mp;
  map<string, int> vis;
  vis["1"] = 2;
  vis["2"] = 5;
  vis["3"] = 5;
  vis["4"] = 4;
  vis["5"] = 5;
  vis["6"] = 6;
  vis["7"] = 3;
  vis["8"] = 7;
  vis["9"] = 6;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    cin >> x;
    flag[vis[x]] = 1;
    if (mp[vis[x]] < x)
      mp[vis[x]] = x;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      if (flag[j] && i >= j) {
        if (i == j) {
          string z = mp[j];
          if (dp[i].s == "")
            dp[i].s = z;
          else if (cmp(z, dp[i].s))
            dp[i].s = z;
        } else {
          if (dp[i - j].s == "")
            continue;
          else {
            string z = dp[i - j].s + mp[j];
            sort(z.begin(), z.end(), cmp1);
            if (dp[i].s == "")
              dp[i].s = z;
            else {
              if (cmp(z, dp[i].s))
                dp[i].s = z;
            }
          }
        }
      }
    }
  }
  cout << dp[n].s;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;
struct sss {
  string s;
} dp[10001];
bool flag[10];
bool cmp(string a, string b) {
  if (a.size() > b.size())
    return 1;
  else if (a.size() < b.size())
    return 0;
  else
    return a > b;
}
bool cmp1(char a, char b) { return a > b; }
int main() {
  int n, m;
  string x;
  map<int, string> mp;
  map<string, int> vis;
  vis["1"] = 2;
  vis["2"] = 5;
  vis["3"] = 5;
  vis["4"] = 4;
  vis["5"] = 5;
  vis["6"] = 6;
  vis["7"] = 3;
  vis["8"] = 7;
  vis["9"] = 6;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    cin >> x;
    flag[vis[x]] = 1;
    if (mp[vis[x]] < x)
      mp[vis[x]] = x;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      if (flag[j] && i >= j) {
        if (i == j) {
          string z = mp[j];
          if (dp[i].s == "")
            dp[i].s = z;
          else if (cmp(z, dp[i].s))
            dp[i].s = z;
        } else {
          if (dp[i - j].s == "")
            continue;
          else {
            string z = dp[i - j].s + mp[j];
            if (dp[i].s == "")
              dp[i].s = z;
            else {
              if (cmp(z, dp[i].s))
                dp[i].s = z;
            }
          }
        }
      }
    }
  }
  cout << dp[n].s;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;
constexpr int MAXN = 1e4 + 10;
vector<string> dp(MAXN);
int cost[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool cmp(string &a, string &b) {
  if (a.size() != b.size()) {
    return a.size() > b.size();
  } else {
    return a > b;
  }
}
string maxs(string &a, string &b) {
  if (a == "?" || a == "") {
    return b;
  }
  if (cmp(a, b)) {
    return a;
  } else {
    return b;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  REP(i, M) { cin >> A[i]; }
  REP(i, MAXN) { dp[i] = "?"; }
  dp[0] = "";
  REP(i, N) {
    if (dp[i] == "?")
      continue;
    for (auto a : A) {
      string s = dp[i] + to_string(a);
      dp[i + cost[a]] = maxs(dp[i + cost[a]], s);
    }
  }
  cout << dp[N] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;
constexpr int MAXN = 1e4 + 10;
vector<string> dp(MAXN);
int cost[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool cmp(string &a, string &b) {
  if (a.size() != b.size()) {
    return a.size() > b.size();
  } else {
    return a > b;
  }
}
string maxs(string &a, string &b) {
  if (a == "?" || a == "") {
    return b;
  }
  if (cmp(a, b)) {
    return a;
  } else {
    return b;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  REP(i, M) { cin >> A[i]; }
  REP(i, MAXN) { dp[i] = "?"; }
  dp[0] = "";
  REP(i, N) {
    if (dp[i] == "?")
      continue;
    for (auto a : A) {
      string s = dp[i] + to_string(a);
      dp[i + cost[a]] = maxs(dp[i + cost[a]], s);
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const vector<int> match = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

void chmax(string &a, string b) {
  if (a == "-")
    a = b;
  else if (a.size() < b.size())
    a = b;
  else if (a.size() == b.size()) {
    if (a < b)
      a = b;
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }

  vector<string> dp(5010, "-"); // dp[0] = "-", dp[1] = "-", dp[2] = "-", ...
  dp[0] = "";

  for (int i = 0; i <= n; i++) {
    if (dp[i] == "-")
      continue;
    for (auto num : a) {
      chmax(dp[i + match[num]],
            dp[i] + (char)('0' + num) // joining : string + char
      );
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const vector<int> match = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

void chmax(string &a, string b) {
  if (a == "-")
    a = b;
  else if (a.size() < b.size())
    a = b;
  else if (a.size() == b.size()) {
    if (a < b)
      a = b;
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }

  vector<string> dp(20000, "-"); // dp[0] = "-", dp[1] = "-", dp[2] = "-", ...
  dp[0] = "";

  for (int i = 0; i <= n; i++) {
    if (dp[i] == "-")
      continue;
    for (auto num : a) {
      chmax(dp[i + match[num]],
            dp[i] + (char)('0' + num) // joining : string + char
      );
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03128 (C++)

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

const int INF = 1001001001;
// id = i - 1
const int need[10] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[1005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
  }

  sort(a.begin(), a.end(), greater<int>());
  fill(dp, dp + n + 1, -INF);
  dp[0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i - need[a[j] - 1] >= 0) {
        dp[i] = max(dp[i], dp[i - need[a[j] - 1]] + 1);
      }
    }
  }

  int now = n;
  for (int i = 0; i < m; ++i) {
    while (now - need[a[i] - 1] >= 0 &&
           dp[now] == dp[now - need[a[i] - 1]] + 1) {
      cout << a[i];
      now -= need[a[i] - 1];
    }
  }
  cout << endl;
}

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

const int INF = 1001001001;
// id = i - 1
const int need[10] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[100005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
  }

  sort(a.begin(), a.end(), greater<int>());
  fill(dp, dp + n + 1, -INF);
  dp[0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i - need[a[j] - 1] >= 0) {
        dp[i] = max(dp[i], dp[i - need[a[j] - 1]] + 1);
      }
    }
  }

  int now = n;
  for (int i = 0; i < m; ++i) {
    while (now - need[a[i] - 1] >= 0 &&
           dp[now] == dp[now - need[a[i] - 1]] + 1) {
      cout << a[i];
      now -= need[a[i] - 1];
    }
  }
  cout << endl;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

long long n, m;
long long a[11];
long long dp[10005];
vector<int> V;
vector<pair<int, int>> pd;
string s;
int k;

int num(int k) {
  int answer = 0;
  if (k == 1) {
    answer = 2;
  } else if (k == 2) {
    answer = 5;
  } else if (k == 3) {
    answer = 5;
  } else if (k == 4) {
    answer = 4;
  } else if (k == 5) {
    answer = 5;
  } else if (k == 6) {
    answer = 6;
  } else if (k == 7) {
    answer = 3;
  } else if (k == 8) {
    answer = 7;
  } else if (k == 9) {
    answer = 6;
  }
  return answer;
}

string solver(int n) {
  for (int i = 1; i < n + 1; ++i) {
    dp[i] = 0;
  }
  for (int i = 1; i < n + 1; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i >= num(a[j])) {
        dp[i] = max(dp[i - num(a[j])] + 1, dp[i]);
      }
    }
  }
  while (n > 0) {
    // for(int i = 0; i < n; ++i){
    for (int j = 0; j < m; ++j) {
      if (dp[n] == dp[n - num(a[j])] + 1 && n >= num(a[j])) {
        if (dp[n] == 1) {
          if (n != num(a[j])) {
            continue;
          }
        }
        n -= num(a[j]);
        s += to_string(a[j]);
        break;
      }
    }
    //}
  }
  return s;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
    // cout << num(a[i]) << endl;
    V.push_back(a[i]);
  }
  sort(V.begin(), V.end(), greater<int>());
  for (int i = 0; i < m; ++i) {
    a[i] = V[i];
    // cout << a[i] << endl;
  }
  cout << solver(n) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

long long n, m;
long long a[11];
long long dp[10005];
vector<int> V;
vector<pair<int, int>> pd;
string s;
int k;

int num(int k) {
  int answer = 0;
  if (k == 1) {
    answer = 2;
  } else if (k == 2) {
    answer = 5;
  } else if (k == 3) {
    answer = 5;
  } else if (k == 4) {
    answer = 4;
  } else if (k == 5) {
    answer = 5;
  } else if (k == 6) {
    answer = 6;
  } else if (k == 7) {
    answer = 3;
  } else if (k == 8) {
    answer = 7;
  } else if (k == 9) {
    answer = 6;
  }
  return answer;
}

string solver(int n) {
  for (int i = 1; i < n + 1; ++i) {
    dp[i] = -10;
  }
  for (int i = 1; i < n + 1; ++i) {
    for (int j = 0; j < m; ++j) {
      if (i >= num(a[j])) {
        dp[i] = max(dp[i - num(a[j])] + 1, dp[i]);
      }
    }
  }
  while (n > 0) {
    // for(int i = 0; i < n; ++i){
    for (int j = 0; j < m; ++j) {
      if (dp[n] == dp[n - num(a[j])] + 1 && n >= num(a[j])) {
        if (dp[n] == 1) {
          if (n != num(a[j])) {
            continue;
          }
        }
        n -= num(a[j]);
        s += to_string(a[j]);
        break;
      }
    }
    //}
  }
  return s;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
    // cout << num(a[i]) << endl;
    V.push_back(a[i]);
  }
  sort(V.begin(), V.end(), greater<int>());
  for (int i = 0; i < m; ++i) {
    a[i] = V[i];
    // cout << a[i] << endl;
  }
  cout << solver(n) << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << (x) << endl

// dp[i] := i本使って作れる数値の最大値(stringで持つ)
// 遷移
// dp[i] = max(dp[i], dp[i-cost[a[j]]] + a[j])

string dp[1010];
int cost[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i)
    cin >> a[i];
  sort(ALL(a));
  reverse(ALL(a));

  fill_n(dp, n, "0");
  dp[0] = "";
  int sm = 0;
  for (int i = 0; i <= n; ++i) {
    for (auto &&ai : a) {
      // cerr<<"fuge"<<endl;
      if (i - cost[ai - 1] < 0)
        continue;
      if (dp[i - cost[ai - 1]] == "0")
        continue;
      // debug(i);
      // debug(ai);
      string tmp = to_string(ai) + dp[i - cost[ai - 1]];
      // cerr<<"hoge"<<endl;
      // debug(cost[ai-1]);
      // cerr<<tmp<< endl<<endl;
      if (dp[i].size() > tmp.size())
        continue;
      if (dp[i].size() == tmp.size()) {
        if (dp[i] > tmp)
          continue;
      }
      // cerr<<tmp<< endl;
      dp[i] = tmp;
      // cerr<<tmp<< endl;
    }
  }
  string ans = dp[n];
  sort(ALL(ans));
  reverse(ALL(ans));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << (x) << endl

// dp[i] := i本使って作れる数値の最大値(stringで持つ)
// 遷移
// dp[i] = max(dp[i], dp[i-cost[a[j]]] + a[j])

string dp[101010];
int cost[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i)
    cin >> a[i];
  sort(ALL(a));
  reverse(ALL(a));

  fill_n(dp, n, "0");
  dp[0] = "";
  int sm = 0;
  for (int i = 0; i <= n; ++i) {
    for (auto &&ai : a) {
      // cerr<<"fuge"<<endl;
      if (i - cost[ai - 1] < 0)
        continue;
      if (dp[i - cost[ai - 1]] == "0")
        continue;
      // debug(i);
      // debug(ai);
      string tmp = to_string(ai) + dp[i - cost[ai - 1]];
      // cerr<<"hoge"<<endl;
      // debug(cost[ai-1]);
      // cerr<<tmp<< endl<<endl;
      if (dp[i].size() > tmp.size())
        continue;
      if (dp[i].size() == tmp.size()) {
        if (dp[i] > tmp)
          continue;
      }
      // cerr<<tmp<< endl;
      dp[i] = tmp;
      // cerr<<tmp<< endl;
    }
  }
  string ans = dp[n];
  sort(ALL(ans));
  reverse(ALL(ans));
  cout << ans << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

const int nmax = 1e9 + 7;
const long long INF = 1e18;
const double PI = 2 * asin(1);
typedef long long ll;

int main() {

  int num[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<pair<int, int>> candidate;
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A;
    cin >> A;
    candidate.push_back(make_pair(A, num[A]));
  }
  sort(candidate.begin(), candidate.end(), greater<pair<int, int>>());

  vector<int> DP(1e5);
  for (int i = 0; i < candidate.size(); i++) {
    DP[candidate[i].second] = 1;
  }

  for (int i = 2; i < N + 1; i++) {
    if (DP[i] > 0) {
      for (int j = 0; j < candidate.size(); j++) {
        int num = candidate[j].second;
        DP[i + num] = max(DP[i + num], DP[i] + 1);
      }
    }
  }

  int dig = DP[N], sum = N;
  while (dig > 0) {
    for (int i = 0; i < candidate.size(); i++) {
      int now = candidate[i].second;
      if (DP[sum - now] + 1 == DP[sum] && DP[sum] > 1) {
        cout << candidate[i].first;
        sum -= now;
        break;
      } else if (DP[sum] == 1 && sum == now) {
        cout << candidate[i].first;
        sum -= now;
        break;
      }
    }
    dig--;
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

const int nmax = 1e9 + 7;
const long long INF = 1e18;
const double PI = 2 * asin(1);
typedef long long ll;

int main() {

  int num[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<pair<int, int>> candidate;
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int A;
    cin >> A;
    candidate.push_back(make_pair(A, num[A]));
  }
  sort(candidate.begin(), candidate.end(), greater<pair<int, int>>());

  map<int, int> DP;
  for (int i = 0; i < candidate.size(); i++) {
    DP[candidate[i].second] = 1;
  }

  for (int i = 2; i < N + 1; i++) {
    if (DP[i] > 0) {
      for (int j = 0; j < candidate.size(); j++) {
        int num = candidate[j].second;
        DP[i + num] = max(DP[i + num], DP[i] + 1);
      }
    }
  }

  int dig = DP[N], sum = N;
  while (dig > 0) {
    for (int i = 0; i < candidate.size(); i++) {
      int now = candidate[i].second;
      if (DP[sum - now] + 1 == DP[sum] && DP[sum] > 1) {
        cout << candidate[i].first;
        sum -= now;
        break;
      } else if (DP[sum] == 1 && sum == now) {
        cout << candidate[i].first;
        sum -= now;
        break;
      }
    }
    dig--;
  }
  cout << endl;

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9;

int N, M;
vector<int> A;

int num[10] = {-1, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[100010];

string f() {
  string S = "";
  int i = N;
  while (1) {
    if (i == 0) {
      return S;
    }
    for (int j = 0; j < M; j++) {
      int k = i - num[A[j]];
      if (k >= 0 && dp[k] == dp[i] - 1) {
        S = S + to_string(A[j]);
        i -= num[A[j]];
        break;
      }
    }
  }
  return S;
}
void abc118d() {
  sort(A.rbegin(), A.rend());

  for (int i = 1; i <= N; i++) {
    dp[i] = -INF;
  }
  dp[0] = 0;

  for (int i = 1; i <= N; i++) {
    int dp_tmp = -INF;
    for (int j = 0; j < M; j++) {
      int k = i - num[A[j]];
      if (k >= 0 && dp[k] != -INF) {
        dp_tmp = max(dp_tmp, dp[i - num[A[j]]] + 1);
      }
    }
    dp[i] = dp_tmp;
  }

  cout << f() << "\n";
  return;
}

int main() {
  cin >> N >> M;
  A.assign(N, 0);
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  abc118d();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9;

int N, M;
vector<int> A;

int num[10] = {-1, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[100010];

string f() {
  string S = "";
  int i = N;
  while (1) {
    if (i == 0) {
      return S;
    }
    for (int j = 0; j < M; j++) {
      int k = i - num[A[j]];
      if (k >= 0 && dp[k] == dp[i] - 1) {
        S = S + to_string(A[j]);
        i -= num[A[j]];
        break;
      }
    }
  }
  return S;
}
void abc118d() {
  sort(A.rbegin(), A.rend());

  for (int i = 1; i <= N; i++) {
    dp[i] = -INF;
  }
  dp[0] = 0;

  for (int i = 1; i <= N; i++) {
    int dp_tmp = -INF;
    for (int j = 0; j < M; j++) {
      int k = i - num[A[j]];
      if (k >= 0 && dp[k] != -INF) {
        dp_tmp = max(dp_tmp, dp[i - num[A[j]]] + 1);
      }
    }
    dp[i] = dp_tmp;
  }

  cout << f() << "\n";
  return;
}

int main() {
  cin >> N >> M;
  A.assign(M, 0);
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  abc118d();
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
int dp[1009];
signed main() {
  int n, m;
  cin >> n >> m;
  int num[10] = {inf, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int nm[10] = {inf, inf, inf, inf, inf, inf, inf, inf, inf, inf};
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    nm[x] = num[x];
  }
  for (int i = 0; i <= n; i++) {
    dp[i] = -inf;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 10; j++) {
      if (i - nm[j] >= 0) {
        dp[i] = max(dp[i], dp[i - nm[j]] + 1);
      }
    }
  }
  int dig = dp[n];
  int cur = n;
  // for(int i=0;i<n;i++){cout<<i<<":"<<dp[i]<<endl;}
  for (int i = 0; i < dig; i++) {
    // cout<<i<<" "<<cur<<endl;
    int nex = 0;
    for (int j = 0; j < 10; j++) {
      if (cur >= nm[j] && (dp[cur - nm[j]] + 1 == dp[cur])) {
        nex = max(nex, j);
      }
    }
    cout << nex;
    cur -= nm[nex];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
int dp[10009];
signed main() {
  int n, m;
  cin >> n >> m;
  int num[10] = {inf, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int nm[10] = {inf, inf, inf, inf, inf, inf, inf, inf, inf, inf};
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    nm[x] = num[x];
  }
  for (int i = 0; i <= n; i++) {
    dp[i] = -inf;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 10; j++) {
      if (i - nm[j] >= 0) {
        dp[i] = max(dp[i], dp[i - nm[j]] + 1);
      }
    }
  }
  int dig = dp[n];
  int cur = n;
  // for(int i=0;i<n;i++){cout<<i<<":"<<dp[i]<<endl;}
  for (int i = 0; i < dig; i++) {
    // cout<<i<<" "<<cur<<endl;
    int nex = 0;
    for (int j = 0; j < 10; j++) {
      if (cur >= nm[j] && (dp[cur - nm[j]] + 1 == dp[cur])) {
        nex = max(nex, j);
      }
    }
    cout << nex;
    cur -= nm[nex];
  }
  cout << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;

int main() {
  ll N, M;
  cin >> N >> M;

  vector<ll> A(10, 0);
  vector<ll> stick = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < M; i++) {
    int temp;
    cin >> temp;
    A[temp] = 1;
  }

  vector<string> dp(1001, "");

  for (int i = 1; i < 10; i++) {
    if (A[i] == 0)
      continue;

    ll s = stick[i - 1];
    for (int j = 0; j < N; j++) {
      if (j + s > N)
        break;

      if (dp[j] == "" && j > 0)
        continue;

      string oldString = dp[j + s];
      string newString = to_string(i) + dp[j];

      if ((newString.length() > oldString.length()) ||
          (newString.length() == oldString.length()) &&
              (newString > oldString)) {

        dp[j + s] = newString;
      }
    }
  }

  cout << dp[N] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;

int main() {
  ll N, M;
  cin >> N >> M;

  vector<ll> A(10, 0);
  vector<ll> stick = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < M; i++) {
    int temp;
    cin >> temp;
    A[temp] = 1;
  }

  vector<string> dp(10001, "");

  for (int i = 1; i < 10; i++) {
    if (A[i] == 0)
      continue;

    ll s = stick[i - 1];
    for (int j = 0; j < N; j++) {
      if (j + s > N)
        break;

      if (dp[j] == "" && j > 0)
        continue;

      string oldString = dp[j + s];
      string newString = to_string(i) + dp[j];

      if ((newString.length() > oldString.length()) ||
          (newString.length() == oldString.length()) &&
              (newString > oldString)) {

        dp[j + s] = newString;
      }
    }
  }

  cout << dp[N] << "\n";
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, M;
  cin >> N >> M;
  vi A(M), P = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) cin >> A[i];
  vector<string> dp(N + 1, "-1");
  dp[0] = "";
  REP(i, 1, N) {
    rep(j, 0, M) {
      int X = P[A[j] - 1];
      if (i - X >= 0 && dp[i - X] != "-1") {
        string S = dp[i - X];
        S += '0' + A[j];
        sort(ALL(S));
        reverse(ALL(S));
        if (dp[i] == "-1")
          dp[i] = S;
        else if (S.size() > dp[i].size())
          dp[i] = S;
        else if (S.size() == dp[i].size() && S > dp[i])
          dp[i] = S;
      }
    }
  }
  cout << dp[N] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, M;
  cin >> N >> M;
  vi A(M), P = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) cin >> A[i];
  vector<string> dp(N + 1, "-1");
  dp[0] = "";
  REP(i, 1, N) {
    rep(j, 0, M) {
      int X = P[A[j] - 1];
      if (i - X >= 0 && dp[i - X] != "-1") {
        string S = dp[i - X];
        S += '0' + A[j];
        sort(ALL(S), greater<char>());
        if (dp[i] == "-1")
          dp[i] = S;
        else if (S.size() > dp[i].size())
          dp[i] = S;
        else if (S.size() == dp[i].size() && S > dp[i])
          dp[i] = S;
      }
    }
  }
  cout << dp[N] << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  int x;
  int B[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> A(10), C(10);
  rep(i, m) {
    cin >> x;
    A[x] = 1;
    C[B[x]] = 1;
  }

  int dp[5010];
  rep(i, 5010) dp[i] = -1;
  dp[0] = 0;

  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      if (C[j] == 1 && i - j >= 0 && dp[i - j] != -1 && dp[i] < dp[i - j] + 1)
        dp[i] = dp[i - j] + 1;
    }
  }

  int t = n;
  for (int i = 9; i > 0 && t > 0; i--) {
    if (A[i] == 0)
      continue;
    while (t - B[i] >= 0 && dp[t - B[i]] == dp[t] - 1) {
      cout << i;
      t -= B[i];
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  int x;
  int B[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> A(10), C(10);
  rep(i, m) {
    cin >> x;
    A[x] = 1;
    C[B[x]] = 1;
  }

  int dp[10010];
  rep(i, 10010) dp[i] = -1;
  dp[0] = 0;

  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      if (C[j] == 1 && i - j >= 0 && dp[i - j] != -1 && dp[i] < dp[i - j] + 1)
        dp[i] = dp[i - j] + 1;
    }
  }

  int t = n;
  for (int i = 9; i > 0 && t > 0; i--) {
    if (A[i] == 0)
      continue;
    while (t - B[i] >= 0 && dp[t - B[i]] == dp[t] - 1) {
      cout << i;
      t -= B[i];
    }
  }
  cout << endl;

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int C[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int N = 5000 + 100;
int n;
bool can[10];
void readData() {
  scanf("%d", &n);
  int m, x;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    can[x] = true;
  }
}
string f[N];
bool bigger(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  int len = a.length();
  for (int i = 0; i < len; i++)
    if (a[i] != b[i])
      return a[i] > b[i];
  return false;
}
void solve() {
  for (int i = 1; i <= 9; i++)
    if (can[i]) {
      char t[] = "x";
      t[0] = '0' + i;
      f[C[i]] = t;
    }
  for (int i = 2; i < n; i++)
    if (f[i] != "") {
      for (int j = 1; j <= 9; j++)
        if (can[j]) {
          char t[] = "x";
          t[0] = '0' + j;
          string ns = string(t) + f[i];
          if (bigger(ns, f[i + C[j]]))
            f[i + C[j]] = ns;
        }
    }
  cout << f[n] << endl;
}
int main() {
  readData();
  solve();
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int C[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int N = 10000 + 100;
int n;
bool can[10];
void readData() {
  scanf("%d", &n);
  int m, x;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    can[x] = true;
  }
}
string f[N];
bool bigger(string a, string b) {
  if (a.length() != b.length())
    return a.length() > b.length();
  int len = a.length();
  for (int i = 0; i < len; i++)
    if (a[i] != b[i])
      return a[i] > b[i];
  return false;
}
void solve() {
  for (int i = 1; i <= 9; i++)
    if (can[i]) {
      char t[] = "x";
      t[0] = '0' + i;
      f[C[i]] = t;
    }
  for (int i = 2; i < n; i++)
    if (f[i] != "") {
      for (int j = 1; j <= 9; j++)
        if (can[j]) {
          char t[] = "x";
          t[0] = '0' + j;
          string ns = string(t) + f[i];
          if (bigger(ns, f[i + C[j]]))
            f[i + C[j]] = ns;
        }
    }
  cout << f[n] << endl;
}
int main() {
  readData();
  solve();
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPI(i, f, n) for (int i = (f); i < (n); ++i)
using namespace std;
typedef long long ll;

int ma[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

// dp[i] : i本使った時の最大値
string dp[10005];

void str_max(string &a, string b) {
  if (a.size() < b.size()) {
    a = b;
  } else if (a.size() == b.size()) {
    if (b > a)
      a = b;
  }
  return;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  REP(i, M) { cin >> A[i]; }
  // "-"を初期値とする。初期値の場合は、その本数になることがないことを示す。
  // はじめは不要かと思ってたけど、dp[0] = "" の状態と、存在しないは違う。
  // 存在しない場合はそのdp[i]を起点とすることができない。
  REP(i, N) dp[i] = "-";

  dp[0] = "";
  REP(i, N + 1) {
    for (auto x : A) {
      // 存在しない場合は飛ばす
      if (dp[i] == "-")
        continue;
      string b = dp[i] + (char)('0' + x);
      str_max(dp[i + ma[x]], b);
    }
  }

  cout << dp[N] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPI(i, f, n) for (int i = (f); i < (n); ++i)
using namespace std;
typedef long long ll;

int ma[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

// dp[i] : i本使った時の最大値
string dp[10020];

void str_max(string &a, string b) {
  if (a.size() < b.size()) {
    a = b;
  } else if (a.size() == b.size()) {
    if (b > a)
      a = b;
  }
  return;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  REP(i, M) { cin >> A[i]; }
  // "-"を初期値とする。初期値の場合は、その本数になることがないことを示す。
  // はじめは不要かと思ってたけど、dp[0] = "" の状態と、存在しないは違う。
  // 存在しない場合はそのdp[i]を起点とすることができない。
  REP(i, N) dp[i] = "-";

  dp[0] = "";
  REP(i, N + 1) {
    for (auto x : A) {
      // 存在しない場合は飛ばす
      if (dp[i] == "-")
        continue;
      string b = dp[i] + (char)('0' + x);
      str_max(dp[i + ma[x]], b);
    }
  }

  cout << dp[N] << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

int main() {
#ifndef ONLINE_JUDGE
  freopen(".in.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, ii;
  cin >> n >> m;
  vector<int> c = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<bool> can(10);
  vector<vector<int>> dp(5001, vector<int>(n + 1, -1));
  dp[0][0] = 0;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    can[a] = 1;
  }
  for (int i = 1; i < 5001; i++)
    for (int j = 0; j < n + 1; j++)
      for (int k = 9; k; k--)
        if (can[k] && j - c[k] > -1 && dp[i - 1][j - c[k]] != -1) {
          dp[i][j] = k;
          break;
        }
  for (int i = 5000; i > -1; i--)
    if (dp[i][n] != -1) {
      ii = i;
      break;
    }
  for (int i = ii; i; i--) {
    cout << dp[i][n];
    n -= c[dp[i][n]];
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen(".in.txt", "r", stdin);
  // freopen(".out.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, ii;
  cin >> n >> m;
  vector<int> c = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<bool> can(10);
  vector<vector<int>> dp(5001, vector<int>(n + 1, -1));
  dp[0][0] = 0;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    can[a] = 1;
  }
  for (int i = 1; i < 5001; i++)
    for (int j = 0; j < n + 1; j++)
      for (int k = 9; k; k--)
        if (can[k] && j - c[k] > -1 && dp[i - 1][j - c[k]] != -1) {
          dp[i][j] = k;
          break;
        }
  for (int i = 5000; i > -1; i--)
    if (dp[i][n] != -1) {
      ii = i;
      break;
    }
  for (int i = ii; i; i--) {
    cout << dp[i][n];
    n -= c[dp[i][n]];
  }

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

#define ALL(obj) (obj).begin(), (obj).end()

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

typedef pair<int, int> P;

#define INF 2000000007
#define MINF -2000000007
#define MOD 10e9 + 7
#define MAX 100005
// テンプレ終了
//====================================================================//

int dp[MAX];

// abc118
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  REP(i, M) cin >> A[i];

  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  REPS(i, 1, N + 1) dp[i] = MINF;

  REPS(i, 2, N + 1) {
    REP(j, M) {
      if (i - cost[A[j]] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - cost[A[j]]] + 1);
    }
  }

  sort(ALL(A), [&](int x, int y) { return x > y; });

  string str;
  for (int i = N; i > 0;) {
    REP(j, M) {
      if (i - cost[A[j]] < 0)
        continue;
      if (dp[i - cost[A[j]]] == dp[i] - 1) {
        str += to_string(A[j]);
        i -= cost[A[j]];
        break;
      }
    }
  }

  cout << str << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

#define ALL(obj) (obj).begin(), (obj).end()

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

typedef pair<int, int> P;

#define INF 2000000007
#define MINF -2000000007
#define MOD 10e9 + 7
#define MAX 100005
// テンプレ終了
//====================================================================//

int dp[MAX];

// abc118
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  REP(i, M) cin >> A[i];

  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  REPS(i, 1, N + 1) dp[i] = MINF;

  REPS(i, 2, N + 1) {
    REP(j, M) {
      if (i - cost[A[j]] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - cost[A[j]]] + 1);
    }
  }

  sort(ALL(A), [&](int x, int y) { return x > y; });

  string str;
  for (int i = N; i > 0;) {
    REP(j, M) {
      if (i - cost[A[j]] < 0)
        continue;
      if (dp[i - cost[A[j]]] == dp[i] - 1) {
        str += to_string(A[j]);
        i -= cost[A[j]];
        break;
      }
    }
  }

  cout << str << endl;

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#ifdef _WIN32
#include "debug.hpp"
#endif
using namespace std;

#define rep(i, N) for (int i = 0; i < (N); i++)
#define reps(i, N) for (int i = 1; i <= (N); i++)
#define repr(i, N) for (int i = (N)-1; i >= 0; i--)
#define pub push_back

template <typename T> void chmax(T &a, T b) { a = max(a, b); };
template <typename T> void chmin(T &a, T b) { a = min(a, b); };

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const ll LINF = 10000000000000000;
const int MOD = 1000000007;
const int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
//--------------------------------------//
int N, M;
vector<int> A;
const int C[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int dp[10000];
void solve() {
  rep(i, 10000) { dp[i] = -INF; }
  dp[0] = 0;
  reps(i, N) {
    for (auto a : A) {
      if (i - C[a] < 0 or dp[i - C[a]] == -INF)
        continue;
      chmax(dp[i], dp[i - C[a]] + 1);
    }
  }
  sort(A.begin(), A.end(), greater<int>());
  int i = N;
  while (i != 0) {
    if (dp[i] != -INF) {
      for (auto a : A) {
        if (i - C[a] >= 0 and dp[i] == dp[i - C[a]] + 1) {
          cout << a;
          i -= C[a];
          break;
        }
      }
    }
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  rep(i, M) {
    int a;
    cin >> a;
    A.pub(a);
  }
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#ifdef _WIN32
#include "debug.hpp"
#endif
using namespace std;

#define rep(i, N) for (int i = 0; i < (N); i++)
#define reps(i, N) for (int i = 1; i <= (N); i++)
#define repr(i, N) for (int i = (N)-1; i >= 0; i--)
#define pub push_back

template <typename T> void chmax(T &a, T b) { a = max(a, b); };
template <typename T> void chmin(T &a, T b) { a = min(a, b); };

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const ll LINF = 10000000000000000;
const int MOD = 1000000007;
const int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
//--------------------------------------//
int N, M;
vector<int> A;
const int C[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int dp[10001];
void solve() {
  rep(i, 10000) { dp[i] = -INF; }
  dp[0] = 0;
  reps(i, N) {
    for (auto a : A) {
      if (i - C[a] < 0 or dp[i - C[a]] == -INF)
        continue;
      chmax(dp[i], dp[i - C[a]] + 1);
    }
  }
  sort(A.begin(), A.end(), greater<int>());
  int i = N;
  while (i != 0) {
    if (dp[i] != -INF) {
      for (auto a : A) {
        if (i - C[a] >= 0 and dp[i] == dp[i - C[a]] + 1) {
          cout << a;
          i -= C[a];
          break;
        }
      }
    }
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  rep(i, M) {
    int a;
    cin >> a;
    A.pub(a);
  }
  solve();
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

int main() {
  int n, m;
  cin >> n >> m;
  int a[m];
  rep(i, m) {
    cin >> a[i];
    --a[i];
  }
  int mc[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  int dp[n + 1000];
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  rep(i, n) {
    rep(j, m) {
      if (dp[i] != -1) {
        dp[i + mc[a[j]]] = max(dp[i] + 1, dp[i + mc[a[j]]]);
      }
    }
  }
  sort(a, a + m, greater<int>());
  int cur = n;
  while (cur) {
    rep(i, m) {
      if (dp[cur - mc[a[i]]] == dp[cur] - 1) {
        cout << a[i] + 1;
        cur -= mc[a[i]];
        break;
      }
    }
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

int main() {
  int n, m;
  cin >> n >> m;
  int a[m];
  rep(i, m) {
    cin >> a[i];
    --a[i];
  }
  int mc[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  int dp[n + 1000];
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  rep(i, n) {
    rep(j, m) {
      if (dp[i] != -1) {
        dp[i + mc[a[j]]] = max(dp[i] + 1, dp[i + mc[a[j]]]);
      }
    }
  }
  sort(a, a + m, greater<int>());
  int cur = n;
  while (cur) {
    rep(i, m) {
      if (cur >= mc[a[i]] && dp[cur - mc[a[i]]] == dp[cur] - 1) {
        cout << a[i] + 1;
        cur -= mc[a[i]];
        break;
      }
    }
  }
  cout << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF -1e9
typedef long long ll;
ll N;
int M;
int N_digit[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main(void) {
  cin >> N >> M;
  vector<int> A(N);
  ll i;
  // dp[i]:i本使って作れる最大の桁数
  int j, min, dp[N + 1];
  min = 10;
  for (i = 0; i < M; i++)
    cin >> A[i];
  dp[0] = 0;
  for (i = 1; i <= N; i++) {
    dp[i] = INF;
    for (j = 0; j < M; j++) {
      if (i >= N_digit[A[j]] && dp[i - N_digit[A[j]]] != INF) {
        dp[i] = max(dp[i], dp[i - N_digit[A[j]]] + 1);
      }
    }
  }

  sort(A.begin(), A.end(), greater<int>());
  ll n = N;
  while (n > 0) {
    for (i = 0; i < M; i++) {
      if (n >= N_digit[A[i]] && dp[n - N_digit[A[i]]] == dp[n] - 1) {
        cout << A[i];
        n -= (ll)N_digit[A[i]];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF -1e9
typedef long long ll;
ll N;
int M;
int N_digit[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main(void) {
  cin >> N >> M;
  vector<int> A(M);
  ll i;
  // dp[i]:i本使って作れる最大の桁数
  int j, min, dp[N + 1];
  min = 10;
  for (i = 0; i < M; i++)
    cin >> A[i];
  dp[0] = 0;
  for (i = 1; i <= N; i++) {
    dp[i] = INF;
    for (j = 0; j < M; j++) {
      if (i >= N_digit[A[j]] && dp[i - N_digit[A[j]]] != INF) {
        dp[i] = max(dp[i], dp[i - N_digit[A[j]]] + 1);
      }
    }
  }

  sort(A.begin(), A.end(), greater<int>());
  ll n = N;
  while (n > 0) {
    for (i = 0; i < M; i++) {
      if (n >= N_digit[A[i]] && dp[n - N_digit[A[i]]] == dp[n] - 1) {
        cout << A[i];
        n -= (ll)N_digit[A[i]];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}