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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i < n; i++)

int main() {
  int num[10];
  num[1] = 2, num[2] = 5, num[3] = 5, num[4] = 4, num[5] = 5;
  num[6] = 6, num[7] = 3, num[8] = 7, num[9] = 6;
  int N, M;
  cin >> N >> M;
  int A[M];
  rep(i, M) { cin >> A[i]; }
  long long dp[N + 1];
  dp[0] = 0;
  rep2(i, 1, N + 1) {
    long long ans = -10000000000;
    rep(j, M) {
      int k = i - num[A[j]];
      if (k >= 0) {
        ans = max(ans, dp[k] + 1);
      }
    }
    dp[i] = ans;
  }
  sort(A, A + M, greater<int>());
  long long n = dp[N - 1];
  long long x = N;
  int res[n];
  while (n > 0) {
    rep(i, M) {
      if (dp[x - num[A[i]]] == n - 1) {
        x -= num[A[i]];
        n--;
        res[n] = A[i];
        break;
      }
    }
  }
  for (int i = dp[N] - 1; i >= 0; i--) {
    cout << res[i];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i < n; i++)

int main() {
  int num[10];
  num[1] = 2, num[2] = 5, num[3] = 5, num[4] = 4, num[5] = 5;
  num[6] = 6, num[7] = 3, num[8] = 7, num[9] = 6;
  int N, M;
  cin >> N >> M;
  int A[M];
  rep(i, M) { cin >> A[i]; }
  long long dp[N + 1];
  dp[0] = 0;
  rep2(i, 1, N + 1) {
    long long ans = -10000000000;
    rep(j, M) {
      int k = i - num[A[j]];
      if (k >= 0) {
        ans = max(ans, dp[k] + 1);
      }
    }
    dp[i] = ans;
  }
  sort(A, A + M, greater<int>());
  long long n = dp[N];
  long long x = N;
  int res[n];
  while (n > 0) {
    rep(i, M) {
      if (dp[x - num[A[i]]] == n - 1) {
        x -= num[A[i]];
        n--;
        res[n] = A[i];
        break;
      }
    }
  }
  for (int i = dp[N] - 1; i >= 0; i--) {
    cout << res[i];
  }
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 10000;

int num[9] = {2, 5, 5, 4, 5,
              6, 3, 7, 6}; // num[i] : iを作るのに必要なマッチの本数

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

  vector<int> a(m);
  rep(i, m) {
    cin >> a[i];
    a[i]--; // a[i]を0-indexにする
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end()); // aを降順ソート

  vector<int> dp(
      n + 1,
      -INF); // dp[i] :
             // i本(iは1-index)のマッチを使って条件を満たすように整数を作るときの桁数の最大値

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    rep(j, 9) {
      if (i >= num[a[j]]) {
        dp[i] = max(dp[i], dp[i - num[a[j]]] + 1);
      }
    }
  }

  int nokori = n; // 残っているマッチの本数
  rep(i, dp[n]) { // dp[n]桁作る
    rep(j, m) {   // a[j]が作れるか調べる
      if (nokori >= num[a[j]] &&
          dp[nokori - num[a[j]]] == dp[nokori] - 1) { // a[j]が作れる場合
        cout << a[j] + 1;    // 1-indexにしたa[j]を出力して
        nokori -= num[a[j]]; // 残り本数をnum[a[j]]本減らして
        break;               // 次の桁に行く
      }
    }
  }

  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int INF = 10000;

int num[9] = {2, 5, 5, 4, 5,
              6, 3, 7, 6}; // num[i] : iを作るのに必要なマッチの本数

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

  vector<int> a(m);
  rep(i, m) {
    cin >> a[i];
    a[i]--; // a[i]を0-indexにする
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end()); // aを降順ソート

  vector<int> dp(
      n + 1,
      -INF); // dp[i] :
             // i本(iは1-index)のマッチを使って条件を満たすように整数を作るときの桁数の最大値

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    rep(j, m) {
      if (i >= num[a[j]]) {
        dp[i] = max(dp[i], dp[i - num[a[j]]] + 1);
      }
    }
  }

  int nokori = n; // 残っているマッチの本数
  rep(i, dp[n]) { // dp[n]桁作る
    rep(j, m) {   // a[j]が作れるか調べる
      if (nokori >= num[a[j]] &&
          dp[nokori - num[a[j]]] == dp[nokori] - 1) { // a[j]が作れる場合
        cout << a[j] + 1;    // 1-indexにしたa[j]を出力して
        nokori -= num[a[j]]; // 残り本数をnum[a[j]]本減らして
        break;               // 次の桁に行く
      }
    }
  }

  cout << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e18;
// template<typename T> void cmax(T &a, T b) { a = max(a, b); }
// template<typename T> void cmin(T &a, T b) { a = min(a, b); }

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

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

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) cin >> a[i];
  vector<string> dp(10004, "-");
  dp[0] = "";
  rep(i, n + 1) {
    if (dp[i] == "-")
      continue;
    for (auto x : a)
      cmax(dp[i + num[x]], dp[i] + (char)(x + '0'));
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e18;
// template<typename T> void cmax(T &a, T b) { a = max(a, b); }
// template<typename T> void cmin(T &a, T b) { a = min(a, b); }

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

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

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) cin >> a[i];
  vector<string> dp(20004, "-");
  dp[0] = "";
  rep(i, n + 1) {
    if (dp[i] == "-")
      continue;
    for (auto x : a)
      cmax(dp[i + num[x]], dp[i] + (char)(x + '0'));
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll f[2001][2001];
int main() {
  ll n, m;
  cin >> n >> m;
  ll c[10];
  c[1] = 2;
  c[2] = 5;
  c[3] = 5;
  c[4] = 4;
  c[5] = 5;
  c[6] = 6;
  c[7] = 3;
  c[8] = 7;
  c[9] = 6;
  ll a[m + 1];
  for (ll i = 1; i <= m; i++) {
    cin >> a[i];
    f[c[a[i]]][a[i]] = 1;
  }
  sort(a + 1, a + m + 1);
  a[0] = 0;
  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (f[i][a[j]]) {
        for (ll k = 1; k <= m; k++) {
          f[i + c[a[k]]][a[k]] = max(f[i + c[a[k]]][a[k]], f[i][a[j]] + 1);
        }
      }
    }
  }
  ll k;
  for (ll i = n; i > 0; i -= c[a[k]]) {
    k = 0;
    for (ll j = 1; j <= m; j++) {
      if (f[i][a[j]] && f[i][a[j]] >= f[i][a[k]]) {
        k = j;
      }
    }
    cout << (char)('0' + a[k]);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll f[20001][20];
int main() {
  ll n, m;
  cin >> n >> m;
  ll c[10];
  c[1] = 2;
  c[2] = 5;
  c[3] = 5;
  c[4] = 4;
  c[5] = 5;
  c[6] = 6;
  c[7] = 3;
  c[8] = 7;
  c[9] = 6;
  ll a[m + 1];
  for (ll i = 1; i <= m; i++) {
    cin >> a[i];
    f[c[a[i]]][a[i]] = 1;
  }
  sort(a + 1, a + m + 1);
  a[0] = 0;
  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (f[i][a[j]]) {
        for (ll k = 1; k <= m; k++) {
          f[i + c[a[k]]][a[k]] = max(f[i + c[a[k]]][a[k]], f[i][a[j]] + 1);
        }
      }
    }
  }
  ll k;
  for (ll i = n; i > 0; i -= c[a[k]]) {
    k = 0;
    for (ll j = 1; j <= m; j++) {
      if (f[i][a[j]] && f[i][a[j]] >= f[i][a[k]]) {
        k = j;
      }
    }
    cout << (char)('0' + a[k]);
  }
}
Problem p03128 (C++)

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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

template <typename T> void trace(T *a, int s, int n) {
  for (int i = s; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 1e9;

int n, m;
vector<int> digits;
int maxlen[maxn]; // max length when using i sticks

void relax(int &x, int y) { x = max(x, y); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    digits.push_back(d);
  }

  for (int i = 0; i <= n; i++) {
    maxlen[i] = -inf;
  }
  maxlen[0] = 0;

  for (int i = 0; i <= n; i++) {
    for (int d : digits) {
      if (d + i > n)
        continue;
      relax(maxlen[i + cost[d]], maxlen[i] + 1);
    }
  }

  int have = n;
  string res = "";

  while (have > 0) {
    int x = 0;
    for (int d : digits) {
      if (cost[d] > have)
        continue;
      if (maxlen[have - cost[d]] + 1 == maxlen[have]) {
        x = max(x, d);
      }
    }

    have -= cost[x];
    res += char('0' + x);
  }

  cout << res << endl;
  return 0;
}


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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

template <typename T> void trace(T *a, int s, int n) {
  for (int i = s; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 1e9;

int n, m;
vector<int> digits;
int maxlen[maxn]; // max length when using i sticks

void relax(int &x, int y) { x = max(x, y); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int d;
    cin >> d;
    digits.push_back(d);
  }

  for (int i = 0; i <= n; i++) {
    maxlen[i] = -inf;
  }
  maxlen[0] = 0;

  for (int i = 0; i <= n; i++) {
    for (int d : digits) {
      if (cost[d] + i > n)
        continue;
      relax(maxlen[i + cost[d]], maxlen[i] + 1);
    }
  }

  int have = n;
  string res = "";

  while (have > 0) {
    int x = 0;
    for (int d : digits) {
      if (cost[d] > have)
        continue;
      if (maxlen[have - cost[d]] + 1 == maxlen[have]) {
        x = max(x, d);
      }
    }

    have -= cost[x];
    res += char('0' + x);
  }

  cout << res << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp(a, b) make_pair(a, b)
#define pb(t) push_back(t)
#define ft first
#define sc second
#define pt(num) cout << num << "\n"
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

int main(void) {
  ll N, M;
  cin >> N >> M;
  ll use[10] = {};
  ll cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  ll i, j;

  for (i = 1; i <= M; i++) {
    ll a;
    cin >> a;
    use[a] = 1;
  }

  string dp[1111];
  for (i = 0; i <= N; i++)
    dp[i] = "";

  for (i = 1; i < 10; i++)
    if (use[i])
      dp[cost[i]] = (char)(i + '0');

  for (i = 0; i <= N; i++) {
    if (dp[i] == "")
      continue;
    for (j = 1; j < 10; j++) {
      if (use[j] == 0)
        continue;
      if (i + cost[j] > N)
        continue;
      string s = dp[i] + (char)(j + '0');
      if (dp[i + cost[j]].size() < dp[i].size() + 1)
        dp[i + cost[j]] = s;
      else if (dp[i + cost[j]] < s)
        dp[i + cost[j]] = s;
    }
  }

  pt(dp[N]);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp(a, b) make_pair(a, b)
#define pb(t) push_back(t)
#define ft first
#define sc second
#define pt(num) cout << num << "\n"
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

int main(void) {
  ll N, M;
  cin >> N >> M;
  ll use[10] = {};
  ll cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  ll i, j;

  for (i = 1; i <= M; i++) {
    ll a;
    cin >> a;
    use[a] = 1;
  }

  string dp[11111];
  for (i = 0; i <= N; i++)
    dp[i] = "";

  for (i = 1; i < 10; i++)
    if (use[i])
      dp[cost[i]] = (char)(i + '0');

  for (i = 0; i <= N; i++) {
    if (dp[i] == "")
      continue;
    for (j = 1; j < 10; j++) {
      if (use[j] == 0)
        continue;
      if (i + cost[j] > N)
        continue;
      string s = dp[i] + (char)(j + '0');
      if (dp[i + cost[j]].size() < dp[i].size() + 1)
        dp[i + cost[j]] = s;
      else if (dp[i + cost[j]] < s)
        dp[i + cost[j]] = s;
    }
  }

  pt(dp[N]);
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

const int INF = 1000000;
const int MAXK = 6000;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(m);
  rep(i, m) cin >> A[i];
  sort(all(A));
  vector<int> K = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> dp(MAXK, -INF);
  dp[0] = 0;
  for (int j = 0; j <= n; j++)
    rep(i, m) dp[j + K[A[i]]] = max(dp[j + K[A[i]]], dp[j] + 1);

  int rem = dp[n];
  string s;
  int j = n;
  while (j > 0) {
    for (int i = m - 1; i >= 0; i--) {
      if (j - K[A[i]] < 0)
        continue;
      if (dp[j - K[A[i]]] == rem - 1) {
        char c = '0' + A[i];
        s = s + c;
        j -= K[A[i]];
        rem--;
        break;
      }
    }
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

const int INF = 1000000;
const int MAXK = 1000005;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(m);
  rep(i, m) cin >> A[i];
  sort(all(A));
  vector<int> K = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> dp(MAXK, -INF);
  dp[0] = 0;
  for (int j = 0; j <= n; j++)
    rep(i, m) dp[j + K[A[i]]] = max(dp[j + K[A[i]]], dp[j] + 1);

  int rem = dp[n];
  string s;
  int j = n;
  while (j > 0) {
    for (int i = m - 1; i >= 0; i--) {
      if (j - K[A[i]] < 0)
        continue;
      if (dp[j - K[A[i]]] == rem - 1) {
        char c = '0' + A[i];
        s = s + c;
        j -= K[A[i]];
        rem--;
        break;
      }
    }
  }
  cout << s << endl;
  return 0;
}
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
inf = float("inf")
N, M = map(int, input().split())
A = map(int, input().split())
C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
costs = {m: C[m - 1] for m in A}
dp = [0]
for i in range(1, N + 1):
    dp.append(max([dp[i - c] if i - c >= 0 else -inf for c in costs.values()]) + 1)

digits = list(costs.keys())
digits = sorted(digits, reverse=True)
num = N
result = []
for _ in range(dp[N]):
    for digit in digits:
        if dp[num - costs[digit]] != -inf and dp[num - costs[digit]] == dp[num] - 1:
            result.append(str(digit))
            num -= costs[digit]
            break
print("".join(result))


Fixed submission:
inf = float("inf")
N, M = map(int, input().split())
A = map(int, input().split())
C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
costs = {m: C[m - 1] for m in A}
dp = [0]
for i in range(1, N + 1):
    dp.append(max([dp[i - c] if i - c >= 0 else -inf for c in costs.values()]) + 1)

digits = list(costs.keys())
digits = sorted(digits, reverse=True)
num = N
result = []
for _ in range(dp[N]):
    for digit in digits:
        if (
            num >= costs[digit]
            and dp[num - costs[digit]] != -inf
            and dp[num - costs[digit]] == dp[num] - 1
        ):
            result.append(str(digit))
            num -= costs[digit]
            break
print("".join(result))
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
"""
Match Matching
提出 #4299735 を参考に作成
"""


def solve():
    # 初期値の設定
    num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = [int(i) for i in input().split()]
    A.sort(reverse=True)
    # 丁度n本で作成できる最大桁数を格納する.
    # 初期値は -inf (丁度使い切ることは不可能) としておく
    dp = [-1 * float("inf")] * (N + 1)

    # 動的計画法で丁度n本のマッチを使うときに作れる最大桁数を計算する
    for n in range(1, N + 1):
        for a in A:
            if n - num[a] >= 0:
                # マッチがnum[a]本あるときにaが作れるなら,
                # n - num[a]本の時よりも1桁多く作れるはず.
                # これをすべてのAに対して行い、その中で最大となった桁数を求める.
                dp[n] = max(dp[n], dp[n - num[a]] + 1)
    # print('各本数丁度で作れる最大桁数')
    # print(dp)

    # 回答となる数値を算出
    ans = ""
    for i in range(dp[N]):  # dp[N]桁の数を上位の桁から決定していく
        for a in A:
            # num[a]本使って数を作ったとき,
            # N - num[a] 本で作れる桁数よりも N 本で作れる桁数のほうが1多い場合,
            # numを作っても最終的に作れる数の桁数は変わらない.
            # そのため dp[N] - 1 == dp[N - num[a]] を満たすような最大のAを作り,
            # 作成する数の上位桁に使用すればよい.
            # 同じ本数のマッチを使用する場合はより大きい数を採用する.
            # そのためAはあらかじめ降順にソートしておく.
            # 最初の条件で配列外アクセスを防ぐ
            if N - num[a] > -1 and dp[N] - 1 == dp[N - num[a]]:
                ans += str(a)
                N -= num[a]
                break
        # print('現在の残本数', N, '採用された数', a)

    print(ans)


if __name__ == "__main__":
    solve()


Fixed submission:
"""
Match Matching
提出 #4299735 を参考に作成
"""


def solve():
    # 初期値の設定
    num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = [int(i) for i in input().split()]
    A.sort(reverse=True)
    # 丁度n本で作成できる最大桁数を格納する.
    # 初期値は -inf (丁度使い切ることは不可能) としておく
    dp = [-1 * float("inf")] * (N + 1)

    # 動的計画法で丁度n本のマッチを使うときに作れる最大桁数を計算する
    dp[0] = 0
    for n in range(1, N + 1):
        for a in A:
            if n - num[a] >= 0:
                # マッチがnum[a]本あるときにaが作れるなら,
                # n - num[a]本の時よりも1桁多く作れるはず.
                # これをすべてのAに対して行い、その中で最大となった桁数を求める.
                dp[n] = max(dp[n], dp[n - num[a]] + 1)
    # print('各本数丁度で作れる最大桁数')
    # print(dp)

    # 回答となる数値を算出
    ans = ""
    for i in range(dp[N]):  # dp[N]桁の数を上位の桁から決定していく
        for a in A:
            # num[a]本使って数を作ったとき,
            # N - num[a] 本で作れる桁数よりも N 本で作れる桁数のほうが1多い場合,
            # numを作っても最終的に作れる数の桁数は変わらない.
            # そのため dp[N] - 1 == dp[N - num[a]] を満たすような最大のAを作り,
            # 作成する数の上位桁に使用すればよい.
            # 同じ本数のマッチを使用する場合はより大きい数を採用する.
            # そのためAはあらかじめ降順にソートしておく.
            # 最初の条件で配列外アクセスを防ぐ
            if N - num[a] > -1 and dp[N] - 1 == dp[N - num[a]]:
                ans += str(a)
                N -= num[a]
                break
        # print('現在の残本数', N, '採用された数', a)

    print(ans)


if __name__ == "__main__":
    solve()
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)

inf = float("inf")
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-inf] * N
for i in range(N + 1)[1::]:
    for x in A:
        if i >= num[x]:
            dp[i] = max(dp[i], dp[i - num[x]] + 1)

ans = ""
while N > 0:
    for x in A:
        if dp[N - num[x]] == dp[N] - 1:
            ans += str(x)
            N -= num[x]
            break

print(ans)


Fixed submission:
#!/usr/bin/env python3
N, M = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)

inf = float("inf")
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-inf] * N
for i in range(N + 1)[1::]:
    for x in A:
        if i >= num[x]:
            dp[i] = max(dp[i], dp[i - num[x]] + 1)

ans = ""
while N > 0:
    for x in A:
        if N >= num[x] and dp[N - num[x]] == dp[N] - 1:
            ans += str(x)
            N -= num[x]
            break

print(ans)
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
num_A = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A.reverse()

INF = 10**6
dp = [-INF for i in range(N + 1)]  # get the max digit size using i matches
dp[0] = 0
for i in range(1, N + 1):
    next_num = [Ai for Ai in A if num_A[Ai] <= i]
    if next_num != []:
        dpi_candidate = [dp[i - num_A[next_numi]] + 1 for next_numi in next_num]
        dp[i] = max(dpi_candidate)

remain_match = N
ans = "0"
while remain_match > 0:
    for Ai in A:
        if dp[remain_match - num_A[Ai]] == dp[remain_match] - 1:
            ans += str(Ai)
            remain_match -= num_A[Ai]
            break

print(int(ans[1:]))


Fixed submission:
num_A = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A.reverse()

INF = 10**6
dp = [-INF for i in range(N + 1)]  # get the max digit size using i matches
dp[0] = 0
for i in range(1, N + 1):
    next_num = [Ai for Ai in A if num_A[Ai] <= i]
    if next_num != []:
        dpi_candidate = [dp[i - num_A[next_numi]] + 1 for next_numi in next_num]
        dp[i] = max(dpi_candidate)

remain_match = N
ans = "0"
while remain_match > 0:
    for Ai in A:
        if (
            remain_match >= num_A[Ai]
            and dp[remain_match - num_A[Ai]] == dp[remain_match] - 1
        ):
            ans += str(Ai)
            remain_match -= num_A[Ai]
            break

print(int(ans[1:]))
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, m = map(int, readline().split())
a = list(map(int, readline().split()))
memo = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [""] * (n + 1)
for aa in a:
    if aa <= n:
        if dp[memo[aa]] < str(aa):
            dp[memo[aa]] = str(aa)
for i in range(n):
    if dp[i] == "":
        continue
    for aa in a:
        if i + memo[aa] <= n:
            v = str(aa) + dp[i]
            if len(dp[i + memo[aa]]) < len(v):
                dp[i + memo[aa]] = v
            else:
                dp[i + memo[aa]] = max(dp[i + memo[aa]], v)
print(dp[-1])


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

n, m = map(int, readline().split())
a = list(map(int, readline().split()))
memo = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [""] * (n + 1)
for aa in a:
    if memo[aa] <= n:
        dp[memo[aa]] = max(dp[memo[aa]], str(aa))
for i in range(n):
    if dp[i] == "":
        continue
    for aa in a:
        if i + memo[aa] <= n:
            v = str(aa) + dp[i]
            if len(dp[i + memo[aa]]) < len(v):
                dp[i + memo[aa]] = v
            else:
                dp[i + memo[aa]] = max(dp[i + memo[aa]], v)
print(dp[-1])
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
n, m = map(int, input().split())
a = list(map(int, input().split()))

memo = ["-1"] * 10100


def MAX(x, y):
    if y[0] != "#" and (x == "#" or len(x) < len(y) or (len(x) == len(y) and x < y)):
        return y
    else:
        return x


def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return "#"

    if memo[i] != "-1":
        return memo[i]

    if i == 0:
        return ""

    ret = "#"
    for x in a:
        ret = MAX(ret, dp(i - f[x]) + str(x))
    memo[i] = ret
    return ret


print(dp(n))


Fixed submission:
import resource
import sys

sys.setrecursionlimit(20000)
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
n, m = map(int, input().split())
a = list(map(int, input().split()))

memo = ["-1"] * 10100


def MAX(x, y):
    if y[0] != "#" and (x == "#" or len(x) < len(y) or (len(x) == len(y) and x < y)):
        return y
    else:
        return x


def dp(i):
    # i本のマッチを使って作れる最大の整数
    if i < 0:
        return "#"

    if memo[i] != "-1":
        return memo[i]

    if i == 0:
        return ""

    ret = "#"
    for x in a:
        ret = MAX(ret, dp(i - f[x]) + str(x))
    memo[i] = ret
    return ret


print(dp(n))
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))

dp = dict.fromkeys(range(n + 1))  # dp[n]: n本使うときの桁数
dp[0] = 0

num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
)  # 後の計算用にsortしておく

for i in range(n):
    dp[i + 1] = dp[i]
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1)

ans = ""
while dp[n] > 1:
    for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定)
ans += str(list(filter(lambda e: e[1] == n, anum))[0][0])

print(ans)


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

dp = dict.fromkeys(range(n + 1))  # dp[n]: n本使うときの桁数
dp[0] = 0

num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
)  # 後の計算用にsortしておく

for i in range(n):
    dp[i + 1] = -float("inf")
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1)

ans = ""
while dp[n] > 1:
    for e in anum:  # anum は降順sortされているので, 大きい順に確認
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break

# 末尾桁は n =0(ちょうど使い切るように決定)
ans += str(list(filter(lambda e: e[1] == n, anum))[0][0])

print(ans)
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
A = [int(x) for x in input().split()]

f = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = sorted(A, reverse=True)

dp = [float("-inf")] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    for a in A:
        if i - f[a] >= 0:
            dp[i] = max(dp[i], dp[i - f[a]] + 1)

ans = ""
num, digit = N, dp[N]
while num > 0:
    for a in A:
        if dp[num - f[a]] == digit - 1:
            ans += str(a)
            num -= f[a]
            digit -= 1
            break
print(ans)


Fixed submission:
N, M = map(int, input().split())
A = [int(x) for x in input().split()]

f = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
A = sorted(A, reverse=True)

dp = [float("-inf")] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    for a in A:
        if i - f[a] >= 0:
            dp[i] = max(dp[i], dp[i - f[a]] + 1)

ans = ""
num, digit = N, dp[N]
while num > 0:
    for a in A:
        i = num - f[a]
        if i >= 0 and dp[i] == digit - 1:
            ans += str(a)
            num -= f[a]
            digit -= 1
            break
print(ans)
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = list(map(int, input().split()))

cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
cd = [(a, cost[a]) for a in A]
cd.sort(key=lambda x: x[0], reverse=True)

dp = [0] + [-1] * N
for i in range(1, N + 1):
    for a, c in cd:
        if i - c >= 0 and 0 <= dp[i - c]:
            dp[i] = max(dp[i], dp[i - c] + 1)

ans, i = [], N
for _ in range(dp[-1]):
    for a, c in cd:
        if dp[i - c] == dp[i] - 1:
            ans.append(a)
            i = i - c
            break

print("".join(map(str, ans)))


Fixed submission:
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = list(map(int, input().split()))

cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
cd = [(a, cost[a]) for a in A]
cd.sort(key=lambda x: x[0], reverse=True)

dp = [0] + [-1] * N
for i in range(1, N + 1):
    for a, c in cd:
        if i - c >= 0 and 0 <= dp[i - c]:
            dp[i] = max(dp[i], dp[i - c] + 1)

ans, i = [], N
for _ in range(dp[-1]):
    for a, c in cd:
        if i - c >= 0 and dp[i - c] == dp[i] - 1:
            ans.append(a)
            i = i - c
            break

print("".join(map(str, ans)))
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
import sys

mp = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]

if __name__ == "__main__":
    n, m = map(int, input().split())
    a = sorted(input().split(), reverse=True)

    dp = [-10007] * (n + 1)
    dp[0] = 0
    for i in a:
        step = mp[int(i)]
        for j in range(step, n + 1):
            dp[j] = max(dp[j - step] + 1, dp[j])

    i = n
    while i > 0:
        for x in a:
            step = mp[int(x)]
            if dp[i - step] == dp[i] - 1:
                sys.stdout.write(x)
                i -= step
                break
    print()


Fixed submission:
import sys

mp = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]

if __name__ == "__main__":
    n, m = map(int, input().split())
    a = sorted(input().split(), reverse=True)

    dp = [-10007] * (n + 1)
    dp[0] = 0
    for i in a:
        step = mp[int(i)]
        for j in range(step, n + 1):
            dp[j] = max(dp[j - step] + 1, dp[j])

    i = n
    while i > 0:
        for x in a:
            step = mp[int(x)]
            if i >= step and dp[i - step] == dp[i] - 1:
                sys.stdout.write(x)
                i -= step
                break
    print()
Problem p03128 (C++)

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

using namespace std;
typedef long long ll;

int main(void) {
  const string MINUSINF = "-";
  int N, M;
  cin >> N >> M;
  int A[N];
  string dp[11000]; // N本使った時の最大の数
  // dp[i] = dp[i-M]
  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  for (int i = 0; i < M; i++)
    cin >> A[i];
  for (int i = 0; i < N + 1; i++)
    dp[i] = MINUSINF;
  dp[0] = "";

  for (int i = 0; i < N + 1; i++) {
    //    cout << "i: " << i << endl;
    for (int j = 0; j < M; j++) {
      if (i - cost[A[j]] < 0)
        continue;
      if (dp[i - cost[A[j]]] == MINUSINF)
        continue;

      string newVal;
      if (to_string(A[j]) > dp[i - cost[A[j]]])
        newVal = to_string(A[j]) + dp[i - cost[A[j]]];
      else
        newVal = dp[i - cost[A[j]]] + to_string(A[j]);

      int csz = dp[i].size();
      int nsz = newVal.size();
      if (nsz > csz)
        dp[i] = newVal;
      else if (nsz == csz) {
        if (newVal > dp[i])
          dp[i] = newVal;
      }
      //      cout << "i: " << i << ", j: " << j << ", dp[i]: " << dp[i] <<
      //      endl;
    }
  }
  cout << dp[N] << endl;
}

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

using namespace std;
typedef long long ll;

int main(void) {
  const string MINUSINF = "-";
  int N, M;
  cin >> N >> M;
  int A[M];
  string dp[11000]; // N本使った時の最大の数
  // dp[i] = dp[i-M]
  int cost[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

  for (int i = 0; i < M; i++)
    cin >> A[i];
  for (int i = 0; i < N + 1; i++)
    dp[i] = MINUSINF;
  dp[0] = "";

  for (int i = 0; i < N + 1; i++) {
    //    cout << "i: " << i << endl;
    for (int j = 0; j < M; j++) {
      if (i - cost[A[j]] < 0)
        continue;
      if (dp[i - cost[A[j]]] == MINUSINF)
        continue;

      string newVal;
      if (to_string(A[j]) > dp[i - cost[A[j]]])
        newVal = to_string(A[j]) + dp[i - cost[A[j]]];
      else
        newVal = dp[i - cost[A[j]]] + to_string(A[j]);

      int csz = dp[i].size();
      int nsz = newVal.size();
      if (nsz > csz)
        dp[i] = newVal;
      else if (nsz == csz) {
        if (newVal > dp[i])
          dp[i] = newVal;
      }
      //      cout << "i: " << i << ", j: " << j << ", dp[i]: " << dp[i] <<
      //      endl;
    }
  }
  cout << dp[N] << endl;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define ios() cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
ll const MOD = 1e9 + 7;
ll const INF = 1e18;
using namespace std;

int main() {
  ios();
  vector<int> cost = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int n, m;
  cin >> n >> m;

  vector<int> v(m);
  for (auto &i : v) {
    cin >> i;
  }

  sort(v.rbegin(), v.rend());

  vector<int> dp(n, (int)-1e9);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto j : v) {
      if (i - cost[j] >= 0) {
        dp[i] = max(dp[i], dp[i - cost[j]] + 1);
      }
    }
  }

  string ans;

  while (n > 0) {
    int a;
    for (auto i : v) {
      if (n >= cost[i] && dp[n - cost[i]] == dp[n] - 1) {
        a = i;
        break;
      }
    }
    ans += '0' + a;
    n -= cost[a];
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define ios() cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
ll const MOD = 1e9 + 7;
ll const INF = 1e18;
using namespace std;

int main() {
  ios();
  vector<int> cost = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int n, m;
  cin >> n >> m;

  vector<int> v(m);
  for (auto &i : v) {
    cin >> i;
  }

  sort(v.rbegin(), v.rend());

  vector<int> dp(n + 1, (int)-1e9);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto j : v) {
      if (i - cost[j] >= 0) {
        dp[i] = max(dp[i], dp[i - cost[j]] + 1);
      }
    }
  }

  string ans;

  while (n > 0) {
    int a;
    for (auto i : v) {
      if (n >= cost[i] && dp[n - cost[i]] == dp[n] - 1) {
        a = i;
        break;
      }
    }
    ans += '0' + a;
    n -= cost[a];
  }

  cout << ans << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
const int N = 10'000;
bool can[N][N];
vector<int> sticks = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
void build(int n, int d, vector<int> &a) {
  for (int x : a) {
    if (sticks[x] <= n and can[n - sticks[x]][d - 1]) {
      cout << x;
      build(n - sticks[x], d - 1, a);
      break;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  cin >> a;
  sort(a.begin(), a.end(), greater<int>());
  can[0][0] = true;
  for (int d = 1; d <= n; d++) {
    for (int i = 1; i <= n; i++) {
      for (int x : a) {
        if (sticks[x] <= i) {
          can[i][d] |= can[i - sticks[x]][d - 1];
        }
      }
    }
  }
  for (int d = n; d >= 0; d--) {
    if (can[n][d]) {
      build(n, d, a);
      cout << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
const int N = 10'000;
bool can[N + 1][N + 1];
vector<int> sticks = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
void build(int n, int d, vector<int> &a) {
  for (int x : a) {
    if (sticks[x] <= n and can[n - sticks[x]][d - 1]) {
      cout << x;
      build(n - sticks[x], d - 1, a);
      break;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  cin >> a;
  sort(a.begin(), a.end(), greater<int>());
  can[0][0] = true;
  for (int d = 1; d <= n; d++) {
    for (int i = 1; i <= n; i++) {
      for (int x : a) {
        if (sticks[x] <= i) {
          can[i][d] |= can[i - sticks[x]][d - 1];
        }
      }
    }
  }
  for (int d = n; d >= 0; d--) {
    if (can[n][d]) {
      build(n, d, a);
      cout << endl;
      break;
    }
  }
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int num[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int n, m;
int a[15];
int dp[10005][15];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a, a + m);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < m; i++)
    dp[0][i] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < m; k++)
        dp[i + num[a[k]]][k] = max(dp[i + num[a[k]]][k], dp[i][j] + 1);
  while (n > 0) {
    int mx = 0, ma = -1;
    for (int i = 0; i < m; i++)
      if (dp[n][i] >= mx) {
        mx = dp[n][i];
        ma = a[i];
      }
    n -= num[ma];
    cout << ma;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int num[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int n, m;
int a[15];
int dp[20005][15];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a, a + m);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < m; i++)
    dp[0][i] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < m; k++)
        dp[i + num[a[k]]][k] = max(dp[i + num[a[k]]][k], dp[i][j] + 1);
  while (n > 0) {
    int mx = 0, ma = -1;
    for (int i = 0; i < m; i++)
      if (dp[n][i] >= mx) {
        mx = dp[n][i];
        ma = a[i];
      }
    n -= num[ma];
    cout << ma;
  }
  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) < (n); (i)++)
#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define forin(i, array) for (auto &x : array)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mod 1000000007
#define inf 10000000000000

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef vector<pint> vpint;

vll s(100009), t(100009);
vll truth(100009);

int vec[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vint arr;
vint ans(100000);

vector<string> dp(10000);

string strmax(string a, string b) {
  if (a == "err")
    return b;
  if (a.length() < b.length())
    return b;
  if (a.length() > b.length())
    return a;
  if (a <= b)
    return b;
  else
    return a;
}

string strfunc(int a, string b) {
  stringstream ss;
  ss << a << b;
  return ss.str();
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x;
    cin >> x;
    arr.pb(x - 1);
  }
  rep(i, n + 1) {
    if (i == 0)
      dp[i] = "";
    else
      dp[i] = "err";
  }
  reps(i, 1, n + 1) {
    rep(j, 9) {
      if (find(all(arr), j) == arr.end())
        continue;
      if (i - vec[j] < 0)
        continue;
      if (dp[i - vec[j]] == "err")
        continue;
      dp[i] = strmax(dp[i], strfunc(j + 1, dp[i - vec[j]]));
    }
  }

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

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define forin(i, array) for (auto &x : array)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mod 1000000007
#define inf 10000000000000

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef vector<pint> vpint;

vll s(100009), t(100009);
vll truth(100009);

int vec[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vint arr;
vint ans(100000);

vector<string> dp(100000);

string strmax(string a, string b) {
  if (a == "err")
    return b;
  if (a.length() < b.length())
    return b;
  if (a.length() > b.length())
    return a;
  if (a <= b)
    return b;
  else
    return a;
}

string strfunc(int a, string b) {
  stringstream ss;
  ss << a << b;
  return ss.str();
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x;
    cin >> x;
    arr.pb(x - 1);
  }
  rep(i, n + 1) {
    if (i == 0)
      dp[i] = "";
    else
      dp[i] = "err";
  }
  reps(i, 1, n + 1) {
    rep(j, 9) {
      if (find(all(arr), j) == arr.end())
        continue;
      if (i - vec[j] < 0)
        continue;
      if (dp[i - vec[j]] == "err")
        continue;
      dp[i] = strmax(dp[i], strfunc(j + 1, dp[i - vec[j]]));
    }
  }

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

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

using namespace std;

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  vector<int> dp(N + 1, -1e9);
  dp.at(0) = 0;
  for (auto &a : A) {
    cin >> a;
    dp.at(num_sticks[a]) = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (auto a : A) {
      int j = i + num_sticks[a];
      if (j <= N) {
        dp.at(j) = max(dp.at(j), dp.at(i) + 1);
      }
    }
  }
  sort(A.begin(), A.end(), greater<>());
  int remaining = N;
  while (remaining > 0) {
    for (auto a : A) {
      int sticks = num_sticks[a];
      if (sticks > remaining) {
        continue;
      } else if (dp.at(remaining) == dp.at(remaining - sticks) + 1) {
        cout << a;
        remaining -= sticks;
        break;
      }
    }
  }
  cout << endl;
  return 0;
}


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

using namespace std;

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  vector<int> dp(N + 10, -1e9);
  dp.at(0) = 0;
  for (auto &a : A) {
    cin >> a;
    dp.at(num_sticks[a]) = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (auto a : A) {
      int j = i + num_sticks[a];
      if (j <= N) {
        dp.at(j) = max(dp.at(j), dp.at(i) + 1);
      }
    }
  }
  sort(A.begin(), A.end(), greater<>());
  int remaining = N;
  while (remaining > 0) {
    for (auto a : A) {
      int sticks = num_sticks[a];
      if (sticks > remaining) {
        continue;
      } else if (dp.at(remaining) == dp.at(remaining - sticks) + 1) {
        cout << a;
        remaining -= sticks;
        break;
      }
    }
  }
  cout << endl;
  return 0;
}
Problem p03128 (Python)

Buggy submission (status: Runtime Error):
def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)

    dp = [-float("Inf")] * (n + 1)
    dp[0] = 0
    for i in range(2, n + 1):
        for j in a:
            dp[i] = max(dp[i], dp[i - t[j]] + 1)

    nums = []
    k = dp[n]
    while n:
        for x in a:
            if dp[n - t[x]] == k - 1:
                nums.append(x)
                n -= t[x]
                break
        k -= 1

    ans = "".join(map(str, nums))
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)

    dp = [-float("Inf")] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        for j in a:
            dp[i] = max(dp[i], dp[i - t[j]] + 1)

    nums = []
    k = dp[n]
    while n:
        for x in a:
            if dp[n - t[x]] == k - 1:
                nums.append(x)
                n -= t[x]
                break
        k -= 1

    ans = "".join(map(str, nums))
    print(ans)


if __name__ == "__main__":
    main()
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string strmax(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  if (a > b)
    return a;
  else
    return b;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  int C[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) cin >> A[i];
  vector<string> dp(1010, "0");
  dp[0] = "";
  rep(i, 0, N) rep(j, 0, M) {
    if (dp[i] != "0")
      dp[i + C[A[j] - 1]] =
          strmax(dp[i + C[A[j] - 1]], to_string(A[j]) + dp[i]);
  }
  cout << dp[N] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string strmax(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  if (a > b)
    return a;
  else
    return b;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  int C[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) cin >> A[i];
  vector<string> dp(10101, "0");
  dp[0] = "";
  rep(i, 0, N) rep(j, 0, M) {
    if (dp[i] != "0")
      dp[i + C[A[j] - 1]] =
          strmax(dp[i + C[A[j] - 1]], to_string(A[j]) + dp[i]);
  }
  cout << dp[N] << endl;
  return 0;
}