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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b = {-1, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, m) cin >> a[i];
  rep(i, 10) {
    bool v = false;
    rep(j, m) if (i == a[j]) v = true;
    if (!v)
      b[i] = -1;
  }
  vector<string> dp(n + 1, "");
  for (int i = 0; i <= n; i++) {
    for (int j = 9; j > 0; j--) {
      if (b[j] == -1)
        continue;
      if (i + b[j] > n)
        continue;
      if (i > 0 && dp[i].size() == 0)
        continue;
      if (dp[i + b[j]].size() < dp[i].size() + 1) {
        char c = '0' + j;
        dp[i + b[j]] = dp[i] + c;
      } else if (dp[i + b[j]].size() == dp[i].size() + 1) {
        char c = '0' + j;
        string s = dp[i] + c;
        sort(s.begin(), s.end(), greater<char>());
        sort(dp[i + b[j]].begin(), dp[i + b[j]].end(), greater<char>());
        if (s > dp[i + b[j]])
          dp[i + b[j]] = s;
      }
    }
  }
  sort(dp[n].begin(), dp[n].end(), greater<char>());
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b = {-1, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, m) cin >> a[i];
  rep(i, 10) {
    bool v = false;
    rep(j, m) if (i == a[j]) v = true;
    if (!v)
      b[i] = -1;
  }
  vector<string> dp(n + 1, "");
  for (int i = 0; i <= n; i++) {
    for (int j = 9; j > 0; j--) {
      if (b[j] == -1)
        continue;
      if (i + b[j] > n)
        continue;
      if (i > 0 && dp[i].size() == 0)
        continue;
      if (dp[i + b[j]].size() < dp[i].size() + 1) {
        char c = '0' + j;
        dp[i + b[j]] = dp[i] + c;
      } else if (dp[i + b[j]].size() == dp[i].size() + 1) {
        char c = '0' + j;
        string s = dp[i] + c;
        vector<int> cnt1(10), cnt2(10);
        rep(k, s.size()) {
          cnt1[s[k] - '0']++;
          cnt2[dp[i + b[j]][k] - '0']++;
        }
        for (int k = 9; k > 0; k--) {
          if (cnt1[k] > cnt2[k]) {
            dp[i + b[j]] = s;
            break;
          } else if (cnt1[k] < cnt2[k])
            break;
        }
      }
    }
  }
  sort(dp[n].begin(), dp[n].end(), greater<char>());
  cout << dp[n] << endl;
}
Problem p03128 (C++)

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

string MINF = "-";

void chmax(string &a, string b) {

  if (a == MINF)
    a = b;
  else if (b.size() > a.size())
    a = b;
  else if (b.size() < a.size())
    return;
  else 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];

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

  string MINF = "-";
  vector<string> dp(N + 5, MINF);
  dp[0] = "";

  for (int i = 0; i < N; i++) {
    if (dp[i] == MINF)
      continue;
    for (int j = 0; j < M; j++) {
      chmax(dp[i + match[a[j]]], dp[i] + (char)(a[j] + '0'));
      chmax(dp[i + match[a[j]]], (char)(a[j] + '0') + dp[i]);
    }
  }

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

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

string MINF = "-";

void chmax(string &a, string b) {

  if (a == MINF)
    a = b;
  else if (b.size() > a.size())
    a = b;
  else if (b.size() < a.size())
    return;
  else 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];

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

  string MINF = "-";
  vector<string> dp(N + 50, MINF);
  dp[0] = "";

  for (int i = 0; i < N; i++) {
    if (dp[i] == MINF)
      continue;
    for (int j = 0; j < M; j++) {
      chmax(dp[i + match[a[j]]], dp[i] + (char)(a[j] + '0'));
      chmax(dp[i + match[a[j]]], (char)(a[j] + '0') + dp[i]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, k, a, d[1001], s[9], t[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<ll> v;
int main(void) {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> s[i];
    s[i]--;
  }
  sort(s, s + k);
  reverse(s, s + k);
  for (int i = 1; i <= n; i++)
    d[i] = -(1LL << 30);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - t[s[j]] < 0)
        continue;
      if (d[i - t[s[j]]] < 0)
        continue;
      d[i] = max(d[i], d[i - t[s[j]]] + 1);
    }
  }
  while (n) {
    for (int i = 0; i < k; i++) {
      if (n - t[s[i]] < 0)
        continue;
      if (d[n] - 1 == d[n - t[s[i]]]) {
        v.push_back(s[i]);
        n -= t[s[i]];
        break;
      }
    }
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] + 1;
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, k, a, d[10001], s[9], t[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<ll> v;
int main(void) {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> s[i];
    s[i]--;
  }
  sort(s, s + k);
  reverse(s, s + k);
  for (int i = 1; i <= n; i++)
    d[i] = -(1LL << 30);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - t[s[j]] < 0)
        continue;
      if (d[i - t[s[j]]] < 0)
        continue;
      d[i] = max(d[i], d[i - t[s[j]]] + 1);
    }
  }
  while (n) {
    for (int i = 0; i < k; i++) {
      if (n - t[s[i]] < 0)
        continue;
      if (d[n] - 1 == d[n - t[s[i]]]) {
        v.push_back(s[i]);
        n -= t[s[i]];
        break;
      }
    }
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] + 1;
  }
  cout << endl;
}
Problem p03128 (C++)

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

int n, m, d[1009];
int h[10] = {0, -2, -5, -5, -4, -5, -6, -3, -7, -6};
bool c[10];
int main(void) {
  cin >> n >> m;
  fill(c, c + 10, false);
  fill(d, d + 1004, -1);
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    h[a] *= -1;
    c[h[a]] = true;
    d[h[a]] = 1;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      int p = d[max(0, i - j)];
      if (c[j] && p > 0 && d[i] < p + 1)
        d[i] = p + 1;
    }
    // cout<<"--- "<<i<<" : "<<d[i]<<endl;
  }
  d[0] = 0;
  int r = n;
  while (r > 0) {
    for (int i = 9; i >= 1; i--) {
      if (h[i] > 0 && r - h[i] >= 0 && d[r - h[i]] == d[r] - 1) {
        r -= h[i];
        cout << i;
        break;
      } else if (i == 1) {
        cout << "-" << r << "-" << d[r] << endl;
        return 0;
      }
    }
  }
  cout << endl;
  return 0;
}


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

int n, m, d[10009];
int h[10] = {0, -2, -5, -5, -4, -5, -6, -3, -7, -6};
bool c[10];
int main(void) {
  cin >> n >> m;
  fill(c, c + 10, false);
  fill(d, d + 1004, -1);
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    h[a] *= -1;
    c[h[a]] = true;
    d[h[a]] = 1;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= 7; j++) {
      int p = d[max(0, i - j)];
      if (c[j] && p > 0 && d[i] < p + 1)
        d[i] = p + 1;
    }
    // cout<<"--- "<<i<<" : "<<d[i]<<endl;
  }
  d[0] = 0;
  int r = n;
  while (r > 0) {
    for (int i = 9; i >= 1; i--) {
      if (h[i] > 0 && r - h[i] >= 0 && d[r - h[i]] == d[r] - 1) {
        r -= h[i];
        cout << i;
        break;
      } else if (i == 1) {
        cout << "-" << r << "-" << d[r] << endl;
        return 0;
      }
    }
  }
  cout << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18 + 1;

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

string comp(string a, string b) {
  if (a.length() > b.length())
    return a;
  else if (a.length() < b.length())
    return b;
  else {
    return a > b ? a : b;
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M, 0);
  for (int i = 0; i < M; i++)
    cin >> A[i];

  sort(A.begin(), A.end(), greater<int>());

  vector<string> dp(N + 1, "");
  for (int i = 0; i < M; i++)
    dp[m[A[i]]] = comp(dp[m[A[i]]], to_string(A[i]));
  for (int j = 0; j <= N; j++) {
    for (int i = 0; i < M; i++) {
      if (j - m[A[i]] >= 0 && dp[j - m[A[i]]] != "")
        dp[j] = comp(dp[j], dp[j - m[A[i]]] + to_string(A[i]));
    }
  }

  cout << dp[N] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18 + 1;

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

string comp(string a, string b) {
  if (a.length() > b.length())
    return a;
  else if (a.length() < b.length())
    return b;
  else {
    return a > b ? a : b;
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M, 0);
  for (int i = 0; i < M; i++)
    cin >> A[i];

  sort(A.begin(), A.end(), greater<int>());

  vector<string> dp(max(N + 1, 10), "");
  for (int i = 0; i < M; i++)
    dp[m[A[i]]] = comp(dp[m[A[i]]], to_string(A[i]));
  for (int j = 0; j <= N; j++) {
    for (int i = 0; i < M; i++) {
      if (j - m[A[i]] >= 0 && dp[j - m[A[i]]] != "")
        dp[j] = comp(dp[j], dp[j - m[A[i]]] + to_string(A[i]));
    }
  }

  cout << dp[N] << endl;

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++)
    cin >> a[i];
  for (int i = 0; i <= 1010; i++)
    s[i] = "0";
  s[0] = "";

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == "0")
        continue;

      string t = s[i] + to_string(a[j]);
      int k = i + num[a[j]];
      if (s[k] == "")
        s[k] = t;
      else if (s[k].length() == t.length()) {
        s[k] = max(s[k], t);
      } else if (s[k].length() < t.length()) {
        s[k] = t;
      }
    }
  }
  cout << s[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++)
    cin >> a[i];
  for (int i = 0; i <= 1010; i++)
    s[i] = "0";
  s[0] = "";

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == "0")
        continue;

      string t = s[i] + to_string(a[j]);
      int k = i + num[a[j]];
      if (s[k] == "")
        s[k] = t;
      else if (s[k].length() == t.length()) {
        s[k] = max(s[k], t);
      } else if (s[k].length() < t.length()) {
        s[k] = t;
      }
    }
  }
  cout << s[n] << endl;
  return 0;
}
Problem p03128 (C++)

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

int main() {
  int N, M, X[10] = {100, 2, 5, 5, 4, 5, 6, 3, 7, 6}, DP[10010] = {},
            INF = 1145141919;
  vector<int> V;
  cin >> N >> M;
  for (int i = 0; i <= N; i++) {
    DP[i] = -INF;
  }
  DP[0] = 0;
  for (int i = 0; i < M; i++) {
    int A;
    cin >> A;
    V.emplace_back(A);
  }
  sort(V.begin(), V.end(), greater<int>());
  for (int i = 1; i <= N; i++) {
    for (int j : V) {
      if (X[j] > i)
        continue;
      DP[i] = max(DP[i], DP[i - X[j]] + 1);
    }
  }
  int D = N;
  while (D != 0) {
    for (int j : V) {
      if (DP[D - X[j]] == DP[D] - 1) {
        cout << j;
        D -= X[j];
        break;
      }
    }
  }
}

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

int main() {
  int N, M, X[10] = {100, 2, 5, 5, 4, 5, 6, 3, 7, 6}, DP[10010] = {},
            INF = 1145141919;
  vector<int> V;
  cin >> N >> M;
  for (int i = 0; i <= N; i++) {
    DP[i] = -INF;
  }
  DP[0] = 0;
  for (int i = 0; i < M; i++) {
    int A;
    cin >> A;
    V.emplace_back(A);
  }
  sort(V.begin(), V.end(), greater<int>());
  for (int i = 1; i <= N; i++) {
    for (int j : V) {
      if (X[j] > i)
        continue;
      DP[i] = max(DP[i], DP[i - X[j]] + 1);
    }
  }
  int D = N;
  while (D != 0) {
    for (int j : V) {
      if (D >= X[j]) {
        if (DP[D - X[j]] == DP[D] - 1) {
          cout << j;
          D -= X[j];
          break;
        }
      }
    }
  }
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  char A[10];
  cin >> N >> M;
  int C[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  string dp[10010] = {};
  bool possible[10010] = {};
  possible[0] = true;
  for (int i = 2; i <= N; i++) {
    string tmp[10] = {};
    for (int j = 0; j < M; j++) {
      if (i - C[A[j] - '0'] >= 0 && possible[i - C[A[j] - '0']]) {
        tmp[j] = dp[i - C[A[j] - '0']] + A[j];
        sort(tmp[j].begin(), tmp[j].end(), greater<char>());
        possible[i] = true;
      }
    }
    dp[i] = tmp[0];
    for (int k = 1; k < M; k++) {
      if (tmp[k].size() > dp[i].size() ||
          (tmp[k].size() == dp[i].size() && tmp[k] >= dp[i]))
        dp[i] = tmp[k];
    }
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  char A[10];
  cin >> N >> M;
  int C[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  string dp[10010] = {};
  bool possible[10010] = {};
  possible[0] = true;
  for (int i = 2; i <= N; i++) {
    string tmp[10] = {};
    for (int j = 0; j < M; j++) {
      if (i - C[A[j] - '0'] >= 0 && possible[i - C[A[j] - '0']]) {
        tmp[j] = A[j] + dp[i - C[A[j] - '0']];
        if (tmp[j].size() > 1 && tmp[j].at(0) < tmp[j].at(1)) {
          sort(tmp[j].begin(), tmp[j].end(), greater<char>());
        }
        possible[i] = true;
      }
    }
    dp[i] = tmp[0];
    for (int k = 1; k < M; k++) {
      if (tmp[k].size() > dp[i].size() ||
          (tmp[k].size() == dp[i].size() && tmp[k] >= dp[i]))
        dp[i] = tmp[k];
    }
  }
  cout << dp[N] << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
ll n, m, a[10];
string str[10010], test[10];
map<ll, ll> mp;
map<ll, string> np;
bool jud[1010], h[10];
int main(void) {
  mp[1] = 2, mp[2] = 5, mp[3] = 5, mp[4] = 4;
  mp[5] = 5, mp[6] = 6, mp[7] = 3, mp[8] = 7;
  mp[9] = 6;
  np[1] = "1", np[2] = "2", np[3] = "3", np[4] = "4", np[5] = "5";
  np[6] = "6", np[7] = "7", np[8] = "8", np[9] = "9";
  jud[0] = true;
  cin >> n >> m;
  for (ll i = 0; i < m; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++) {
    if (i > 10)
      str[i - 10] = "9";
    for (int j = 0; j < m; j++)
      h[j] = false;
    for (ll j = 0; j < m; j++) {
      if (i < mp[a[j]])
        continue;
      if (!jud[i - mp[a[j]]])
        continue;
      h[j] = true;
      test[j] = str[i - mp[a[j]]] + np[a[j]];
    }
    ll len = 0, num = -1;
    for (ll j = 0; j < m; j++) {
      if (!h[j])
        continue;
      if (test[j].length() > len) {
        len = test[j].length();
        num = j;
      } else if (test[j].length() == len) {
        if (test[j] > test[num]) {
          num = j;
        }
      }
    }
    if (num == -1)
      continue;
    str[i] = test[num];
    jud[i] = true;
  }
  for (ll i = 0; i < str[n].length(); i++)
    cout << str[n][i];
  cout << endl;
}


Fixed submission:
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
ll n, m, a[10];
string str[10010], test[10];
map<ll, ll> mp;
map<ll, string> np;
bool jud[10010], h[10];
int main(void) {
  mp[1] = 2, mp[2] = 5, mp[3] = 5, mp[4] = 4;
  mp[5] = 5, mp[6] = 6, mp[7] = 3, mp[8] = 7;
  mp[9] = 6;
  np[1] = "1", np[2] = "2", np[3] = "3", np[4] = "4", np[5] = "5";
  np[6] = "6", np[7] = "7", np[8] = "8", np[9] = "9";
  jud[0] = true;
  cin >> n >> m;
  for (ll i = 0; i < m; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++) {
    if (i > 10)
      str[i - 10] = "9";
    for (int j = 0; j < m; j++)
      h[j] = false;
    for (ll j = 0; j < m; j++) {
      if (i < mp[a[j]])
        continue;
      if (!jud[i - mp[a[j]]])
        continue;
      h[j] = true;
      test[j] = str[i - mp[a[j]]] + np[a[j]];
    }
    ll len = 0, num = -1;
    for (ll j = 0; j < m; j++) {
      if (!h[j])
        continue;
      if (test[j].length() > len) {
        len = test[j].length();
        num = j;
      } else if (test[j].length() == len) {
        if (test[j] > test[num]) {
          num = j;
        }
      }
    }
    if (num == -1)
      continue;
    str[i] = test[num];
    jud[i] = true;
  }
  for (ll i = 0; i < str[n].length(); i++)
    cout << str[n][i];
  cout << endl;
}
Problem p03128 (C++)

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

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int cost[]{0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[10101];

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  vin(A, M);

  rep(i, N) dp[i] = "X"; // 初期化

  dp[0] = "";
  for (int i = 0; i < N; ++i) {

    if (dp[i] == "X")
      continue;

    for (int j = 0; j < M; ++j) {
      string tmp = dp[i];
      if (tmp == "X")
        tmp = "";
      tmp += to_string(A[j]);

      string now = dp[i + cost[A[j]]];
      if (now == "X")
        now = "";

      if (tmp.size() > now.size()) {
        dp[i + cost[A[j]]] = tmp;
      } else if (tmp.size() < now.size())
        continue;
      else {
        int cnt1[10] = {0};
        int cnt2[10] = {0};
        rep(k, tmp.size()) {
          cnt1[tmp[k] - '0']++;
          cnt2[now[k] - '0']++;
        }
        for (int k = tmp.size() - 1; k >= 0; --k) {
          if (cnt1[k] == cnt2[k])
            continue;
          else if (cnt1[k] > cnt2[k]) {
            dp[i + cost[A[j]]] = tmp;
            break;
          } else
            break;
        }
      }
    }
  }

  sort(dp[N].rbegin(), dp[N].rend());
  cout << dp[N] << endl;

  return 0;
}


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

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int cost[]{0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[10101];

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  vin(A, M);

  rep(i, N) dp[i] = "X"; // 初期化

  dp[0] = "";
  for (int i = 0; i < N; ++i) {

    if (dp[i] == "X")
      continue;

    for (int j = 0; j < M; ++j) {
      string tmp = dp[i];
      if (tmp == "X")
        tmp = "";
      tmp += to_string(A[j]);

      string now = dp[i + cost[A[j]]];
      if (now == "X")
        now = "";

      if (tmp.size() > now.size()) {
        dp[i + cost[A[j]]] = tmp;
      } else if (tmp.size() < now.size())
        continue;
      else {
        int cnt1[10] = {0};
        int cnt2[10] = {0};
        rep(k, tmp.size()) {
          cnt1[tmp[k] - '0']++;
          cnt2[now[k] - '0']++;
        }
        for (int k = 9; k >= 0; --k) {
          if (cnt1[k] == cnt2[k])
            continue;
          else if (cnt1[k] > cnt2[k]) {
            dp[i + cost[A[j]]] = tmp;
            break;
          } else
            break;
        }
      }
    }
  }

  sort(dp[N].rbegin(), dp[N].rend());
  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;
typedef long long ll;

int match_num[10] = {1 << 30, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string num_string[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
string dp[1250] = {};

string calc_bigger(const string &a, const string &b) {
  if (a.size() == b.size()) {
    for (int i = 0; i < a.size(); i++) {
      if (a[i] != b[i]) {
        return (a[i] > b[i]) ? a : b;
      }
    }
    return a;
  }
  return (a.size() > b.size()) ? a : b;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> cda;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    cda.push_back(a);
    dp[match_num[a]] = calc_bigger(dp[match_num[a]], num_string[a]);
  }
  for (int i = 0; i < n; i++) {
    if (dp[i].size() == 0)
      continue;
    for (int j = 0; j < m; j++) {
      dp[i + match_num[cda[j]]] =
          calc_bigger(dp[i + match_num[cda[j]]], num_string[cda[j]] + dp[i]);
    }
  }
  /*
      for(int i = 0; i <= n; i++) {
          cout << dp[i] << endl;
      }
  */
  cout << dp[n] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int match_num[10] = {1 << 30, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string num_string[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
string dp[12500] = {};

string calc_bigger(const string &a, const string &b) {
  if (a.size() == b.size()) {
    for (int i = 0; i < a.size(); i++) {
      if (a[i] != b[i]) {
        return (a[i] > b[i]) ? a : b;
      }
    }
    return a;
  }
  return (a.size() > b.size()) ? a : b;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> cda;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    cda.push_back(a);
    dp[match_num[a]] = calc_bigger(dp[match_num[a]], num_string[a]);
  }
  for (int i = 0; i < n; i++) {
    if (dp[i].size() == 0)
      continue;
    for (int j = 0; j < m; j++) {
      dp[i + match_num[cda[j]]] =
          calc_bigger(dp[i + match_num[cda[j]]], num_string[cda[j]] + dp[i]);
    }
  }
  /*
      for(int i = 0; i <= n; i++) {
          cout << dp[i] << endl;
      }
  */
  cout << dp[n] << endl;
}
Problem p03128 (C++)

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

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;

inline string chmax(string &lhs, string rhs) {

  sort(rhs.begin(), rhs.end(), greater<char>());
  if (lhs.size() > rhs.size())
    return lhs;
  else if (lhs.size() < rhs.size())
    return rhs;
  else
    return lhs > rhs ? lhs : rhs;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

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

  vector<string> dp(N + 1);
  int idx;
  for (int m = 0; m <= N; m++) {
    for (int i = 0; i < M; i++) {

      idx = m - m_num[A[i] - 1];
      if (idx >= 0 && (dp[idx] != "" | idx == 0)) {
        dp[m] = chmax(dp[m], dp[idx] + to_string(A[i]));
      }
    }
  }

  cout << dp[N] << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;

inline string chmax(string &lhs, string rhs) {

  // sort(rhs.begin(), rhs.end(), greater<char>());
  if (lhs.size() > rhs.size())
    return lhs;
  else if (lhs.size() < rhs.size())
    return rhs;
  else
    return lhs > rhs ? lhs : rhs;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

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

  vector<string> dp(N + 1);
  int idx;
  for (int m = 0; m <= N; m++) {
    for (int i = 0; i < M; i++) {

      idx = m - m_num[A[i] - 1];
      if (idx >= 0 && (dp[idx] != "" | idx == 0)) {
        dp[m] = chmax(dp[m], dp[idx] + to_string(A[i]));
      }
    }
  }

  cout << dp[N] << "\n";

  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 <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

string max_string(string a, string b) {
  if (a.length() > b.length()) {
    return a;
  } else if (a.length() < b.length()) {
    return b;
  } else {
    for (int i = 0; i < a.length(); i++) {
      if (a[i] > b[i])
        return a;
      else if (a[i] < b[i])
        return b;
    }
    // 一致
    return a;
  }
}

int mm[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int a[15];
string dp[10005];
int main() {
  int n, m;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < m; i++) {
    if (dp[mm[a[i]]] == "")
      dp[mm[a[i]]] = to_string(a[i]);
    else
      dp[mm[a[i]]] = max_string(dp[mm[a[i]]], to_string(a[i]));
  }
  // for (int i = 0; i< 10; i++) {
  //     printf("%d %s\n", i, dp[i].c_str());
  // }

  for (int i = 1; i <= n; i++) {
    if (dp[i] == "")
      continue;
    else {
      for (int j = 0; j < m; j++) {
        if (dp[i + mm[a[j]]] == "")
          dp[i + mm[a[j]]] = to_string(a[j]) + dp[i];
        else
          dp[i + mm[a[j]]] =
              max_string(dp[i + mm[a[j]]], to_string(a[j]) + dp[i]);
      }
    }
  }

  printf("%s\n", dp[n].c_str());

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

string max_string(string a, string b) {
  if (a.length() > b.length()) {
    return a;
  } else if (a.length() < b.length()) {
    return b;
  } else {
    for (int i = 0; i < a.length(); i++) {
      if (a[i] > b[i])
        return a;
      else if (a[i] < b[i])
        return b;
    }
    // 一致
    return a;
  }
}

int mm[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int a[15];
string dp[11000];
int main() {
  int n, m;

  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
  }

  for (int i = 0; i < m; i++) {
    if (dp[mm[a[i]]] == "")
      dp[mm[a[i]]] = to_string(a[i]);
    else
      dp[mm[a[i]]] = max_string(dp[mm[a[i]]], to_string(a[i]));
  }
  // for (int i = 0; i< 10; i++) {
  //     printf("%d %s\n", i, dp[i].c_str());
  // }

  for (int i = 1; i <= n; i++) {
    if (dp[i] == "")
      continue;
    else {
      for (int j = 0; j < m; j++) {
        if (dp[i + mm[a[j]]] == "")
          dp[i + mm[a[j]]] = to_string(a[j]) + dp[i];
        else
          dp[i + mm[a[j]]] =
              max_string(dp[i + mm[a[j]]], to_string(a[j]) + dp[i]);
      }
    }
  }

  printf("%s\n", dp[n].c_str());

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib> //絶対値利用のため
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

// const int INF = INT_MAX / 2;

int main() {
  int n, m;
  cin >> n >> m;
  int a[10];
  int h[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m, greater<int>());
  int dp[10100];
  fill(dp, dp + 10100, -1);
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] == -1)
      continue;
    for (int j = 0; j < m; j++) {
      dp[i + h[a[j]]] = max(dp[i + h[a[j]]], dp[i] + 1);
    }
  }
  int k = n;
  while (k) {
    for (int i = 0; i < m; i++)
      if (dp[k - h[a[i]]] == (dp[k] - 1)) {
        cout << a[i];
        k = k - h[a[i]];
        break;
      }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib> //絶対値利用のため
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

// const int INF = INT_MAX / 2;

int main() {
  int n, m;
  cin >> n >> m;
  int a[10];
  int h[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m, greater<int>());
  int dp[10100];
  fill(dp, dp + 10100, -1);
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] == -1)
      continue;
    for (int j = 0; j < m; j++) {
      dp[i + h[a[j]]] = max(dp[i + h[a[j]]], dp[i] + 1);
    }
  }
  int k = n;
  while (k) {
    for (int i = 0; i < m; i++)
      if (k - h[a[i]] >= 0 && dp[k - h[a[i]]] == (dp[k] - 1)) {
        cout << a[i];
        k = k - h[a[i]];
        break;
      }
  }
  cout << endl;

  return 0;
}
Problem p03128 (C++)

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

vector<int> maxvec(vector<int> &a, vector<int> &b) {
  if (a[0] > b[0])
    return a;
  if (a[0] < b[0])
    return b;

  for (int i = 9; i >= 1; i--) {
    if (a[i] > b[i])
      return a;
    if (a[i] < b[i])
      return b;
  }

  return a;
}

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

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

  vector<vector<int>> dp(N + 1, vector<int>(10, -1));
  dp[0] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  for (int i = 0; i <= N; i++) {
    vector<int> vec = {INT_MIN};
    for (int j = 0; j < 10; j++) {
      if (A[j] != 0 and i - A[j] >= 0) {
        if (dp[i - A[j]][0] != -1) {
          vector<int> tmp = dp[i - A[j]];
          tmp[0]++;
          tmp[j]++;
          vec = maxvec(vec, tmp);
        }
      }
    }
    dp[i] = maxvec(dp[i], vec);
  }

  for (int i = 9; i >= 1; i--) {
    for (int j = 0; j < dp[N][i]; j++) {
      cout << i;
    }
  }
  cout << endl;
}

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

vector<int> maxvec(vector<int> &a, vector<int> &b) {
  if (a[0] > b[0])
    return a;
  if (a[0] < b[0])
    return b;

  for (int i = 9; i >= 1; i--) {
    if (a[i] > b[i])
      return a;
    if (a[i] < b[i])
      return b;
  }

  return a;
}

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

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

  vector<vector<int>> dp(N + 1, vector<int>(10, -1));
  dp[0] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  for (int i = 0; i <= N; i++) {
    vector<int> vec = {INT_MIN};
    for (int j = 0; j < 10; j++) {
      if (A[j] != 0 and i - A[j] >= 0) {
        if (dp[i - A[j]][0] != -1) {
          vector<int> tmp = dp[i - A[j]];
          tmp[0]++;
          tmp[j]++;
          vec = maxvec(vec, tmp);
        }
      }
    }
    dp[i] = maxvec(dp[i], vec);
  }

  for (int i = 9; i >= 1; i--) {
    for (int j = 0; j < dp[N][i]; j++) {
      cout << i;
    }
  }
  cout << endl;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, m, c[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10001], p[10001], a[10], inf = 100000;

int f(int n) {
  if (n < 0)
    return -inf;
  if (n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  int md = -inf, mi = -1;
  for (int i = 0; i < m; ++i) {
    int d = 1 + f(n - c[a[i]]);
    if (d > md)
      md = d, mi = i;
  }
  return p[n] = mi, dp[n] = md;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < m; ++i)
    cin >> a[i];
  sort(a, a + m, [](int i, int j) {
    // if (c[i] != c[j])
    // return c[i] < c[j];
    return i > j;
  });
  while (n) {
    cout << a[p[n]];
    n -= c[a[p[n]]];
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int n, m, c[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10001], p[10001], a[10], inf = 100000;

int f(int n) {
  if (n < 0)
    return -inf;
  if (n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  int md = -inf, mi = -1;
  for (int i = 0; i < m; ++i) {
    int d = 1 + f(n - c[a[i]]);
    if (d > md)
      md = d, mi = i;
  }
  return p[n] = mi, dp[n] = md;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < m; ++i)
    cin >> a[i];
  sort(a, a + m, [](int i, int j) { return i > j; });
  f(n);
  while (n) {
    cout << a[p[n]];
    n -= c[a[p[n]]];
  }
}
Problem p03128 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  int tmp;
  int bou[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> A;
  for (int i = 0; i < M; i++) {
    cin >> tmp;
    A.push_back(tmp);
  }
  sort(A.begin(), A.end());
  int dp[1100];
  for (int i = 0; i < 1100; i++) {
    dp[i] = -10000;
  }
  dp[10] = 0;
  int maxK;
  for (int i = 11; i <= N + 10; i++) {
    maxK = -10000;
    for (int j = 0; j < A.size(); j++) {
      maxK = max(maxK, dp[i - bou[A.at(j)]] + 1);
    }
    dp[i] = maxK;
  }
  // cout << dp[N+10] << endl;
  int N2 = N;
  for (int i = 0; i < dp[N2 + 10]; i++) {
    for (int j = A.size() - 1; j >= 0; j--) {
      if (dp[N + 10 - bou[A.at(j)]] == dp[N + 10] - 1) {
        cout << A.at(j);
        N -= bou[A.at(j)];
        break;
      }
    }
  }
  return 0;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  int tmp;
  int bou[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vector<int> A;
  for (int i = 0; i < M; i++) {
    cin >> tmp;
    A.push_back(tmp);
  }
  sort(A.begin(), A.end());
  int dp[11000];
  for (int i = 0; i < 11000; i++) {
    dp[i] = -10000;
  }
  dp[10] = 0;
  int maxK;
  for (int i = 11; i <= N + 10; i++) {
    maxK = -10000;
    for (int j = 0; j < A.size(); j++) {
      maxK = max(maxK, dp[i - bou[A.at(j)]] + 1);
    }
    dp[i] = maxK;
  }
  // cout << dp[N+10] << endl;
  int N2 = N;
  for (int i = 0; i < dp[N2 + 10]; i++) {
    for (int j = A.size() - 1; j >= 0; j--) {
      if (dp[N + 10 - bou[A.at(j)]] == dp[N + 10] - 1) {
        cout << A.at(j);
        N -= bou[A.at(j)];
        break;
      }
    }
  }
  return 0;
}
Problem p03128 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

bool cmp(string a, string b) {
  if (a == "x")
    return true;
  if (b == "x")
    return false;
  if (a.size() < b.size())
    return true;
  if (a.size() > b.size())
    return false;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i])
      return a[i] < b[i];
  }
  return false;
}

string cmax(string a, string b) {
  if (cmp(a, b))
    return b;
  else
    return a;
}

int main() {
  int N, M;
  int A[10];
  const int req[10] = {INT32_MAX, 2, 5, 5, 4, 5, 6, 3, 7, 6};

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

  string dp[10000];
  dp[0] = "";
  for (int i = 1; i <= N; i++) {
    dp[i] = "x";
    for (int m = 0; m < M; m++) {
      if (req[A[m]] <= i && dp[i - req[A[m]]] != "x") {
        dp[i] = cmax(dp[i], dp[i - req[A[m]]] + to_string(A[m]));
      }
    }
  }

  cout << dp[N] << endl;

  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;

bool cmp(string a, string b) {
  if (a == "x")
    return true;
  if (b == "x")
    return false;
  if (a.size() < b.size())
    return true;
  if (a.size() > b.size())
    return false;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i])
      return a[i] < b[i];
  }
  return false;
}

string cmax(string a, string b) {
  if (cmp(a, b))
    return b;
  else
    return a;
}

int main() {
  int N, M;
  int A[10];
  const int req[10] = {INT32_MAX, 2, 5, 5, 4, 5, 6, 3, 7, 6};

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

  string dp[10001];
  dp[0] = "";
  for (int i = 1; i <= N; i++) {
    dp[i] = "x";
    for (int m = 0; m < M; m++) {
      if (req[A[m]] <= i && dp[i - req[A[m]]] != "x") {
        dp[i] = cmax(dp[i], dp[i - req[A[m]]] + to_string(A[m]));
      }
    }
  }

  cout << dp[N] << endl;

  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

string str_max(string &a, string &b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  REP(i, a.size()) {
    if (a[i] != b[i]) {
      if (a[i] > b[i])
        return a;
      if (a[i] < b[i])
        return b;
    }
  }
  return a;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  REP(i, m) {
    cin >> a[i];
    a[i]--;
  }

  vector<string> dp(n + 1);
  vector<ll> need = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  REP(i, m) { dp[need[a[i]]] = to_string(a[i] + 1); }
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j < m; j++) {
      if (i >= need[a[j]]) {
        if (dp[i - need[a[j]]] == "")
          continue;
        string t = to_string(a[j] + 1) + dp[i - need[a[j]]];
        dp[i] = str_max(dp[i], t);
      }
    }
    // cout << dp[i] << endl;
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

string str_max(string &a, string &b) {
  if (a.size() > b.size())
    return a;
  if (a.size() < b.size())
    return b;
  REP(i, a.size()) {
    if (a[i] != b[i]) {
      if (a[i] > b[i])
        return a;
      if (a[i] < b[i])
        return b;
    }
  }
  return a;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  REP(i, m) {
    cin >> a[i];
    a[i]--;
  }

  sort(a.begin(), a.end());

  vector<string> dp(10005);
  vector<ll> need = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  REP(i, m) { dp[need[a[i]]] = to_string(a[i] + 1); }
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j < m; j++) {
      if (i >= need[a[j]]) {
        if (dp[i - need[a[j]]] == "")
          continue;
        string t = to_string(a[j] + 1) + dp[i - need[a[j]]];
        dp[i] = str_max(dp[i], t);
      }
    }
    // cout << dp[i] << endl;
  }
  cout << dp[n] << endl;
}
Problem p03128 (C++)

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

#define ll long long

ll match[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  ll N, M;
  cin >> N >> M;
  ll A[M];
  ll dp[N + 1];
  dp[0] = 0;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  sort(A, A + M, greater<ll>());
  for (int i = 1; i <= N; i++) {
    ll d_max = -1;
    for (int j = 0; j < M; j++) {
      ll po = i - match[A[j]];
      if (po < 0)
        continue;
      if (dp[po] < 0)
        continue;
      d_max = max(d_max, dp[po] + 1);
    }
    dp[i] = d_max;
  }
  for (int n = N; 0 < n;) {
    for (int m = 0; m < M; m++) {
      if (A[m] <= n && dp[n - match[A[m]]] == dp[n] - 1) {
        n -= match[A[m]];
        cout << A[m];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}

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

#define ll long long

ll match[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  ll N, M;
  cin >> N >> M;
  ll A[M];
  ll dp[N + 1];
  dp[0] = 0;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
  }
  sort(A, A + M, greater<ll>());
  for (int i = 1; i <= N; i++) {
    ll d_max = -1;
    for (int j = 0; j < M; j++) {
      ll po = i - match[A[j]];
      if (po < 0)
        continue;
      if (dp[po] < 0)
        continue;
      d_max = max(d_max, dp[po] + 1);
    }
    dp[i] = d_max;
  }
  for (int n = N; 0 < n;) {
    for (int m = 0; m < M; m++) {
      if (match[A[m]] <= n && dp[n - match[A[m]]] == dp[n] - 1) {
        n -= match[A[m]];
        cout << A[m];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}
Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

int n, m;
int a[10];
int nums[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d", &a[i]);

  int dp[10000] = {};
  for (int i = 0; i <= n; i++)
    dp[i] = -1;
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] == -1)
      continue;
    for (int j = 0; j < m; j++) {
      if (dp[i] + 1 > dp[i + nums[a[j]]])
        dp[i + nums[a[j]]] = dp[i] + 1;
    }
  }
  // printf("%d\n",dp[n]);
  for (int i = 0; i < m; i++)
    a[i] = -a[i];
  sort(a, a + m);
  for (int i = 0; i < m; i++)
    a[i] = -a[i];

  for (int i = n; i > 0;) {
    for (int j = 0; j < m; j++) {
      if (i - nums[a[j]] < 0)
        continue;
      if (dp[i] == dp[i - nums[a[j]]] + 1) {
        printf("%d", a[j]);
        i -= nums[a[j]];
        // fflush(stdout);
        break;
      }
    }
  }
  printf("\n");
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

int n, m;
int a[10];
int nums[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d", &a[i]);

  int dp[10050] = {};
  for (int i = 0; i <= n; i++)
    dp[i] = -1;
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    if (dp[i] == -1)
      continue;
    for (int j = 0; j < m; j++) {
      if (dp[i] + 1 > dp[i + nums[a[j]]])
        dp[i + nums[a[j]]] = dp[i] + 1;
    }
  }
  // printf("%d\n",dp[n]);
  for (int i = 0; i < m; i++)
    a[i] = -a[i];
  sort(a, a + m);
  for (int i = 0; i < m; i++)
    a[i] = -a[i];

  for (int i = n; i > 0;) {
    for (int j = 0; j < m; j++) {
      if (i - nums[a[j]] < 0)
        continue;
      if (dp[i] == dp[i - nums[a[j]]] + 1) {
        printf("%d", a[j]);
        i -= nums[a[j]];
        // fflush(stdout);
        break;
      }
    }
  }
  printf("\n");
}
Problem p03128 (C++)

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

typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n; i > s; i--)
#define rep_re(i, s, n) for (ll i = n; i >= s; i--)
using namespace std;
static const ll MOD = 1e9 + 7;
static const ll INF = 1e25;
static const ll MAX_N = 100001;

ll match[10] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[100001];
string INF_S = "-";
ll A[10];

string chmax(string s, string t) {
  if (s == INF_S)
    return t;
  else if (s.size() < t.size())
    return t;
  else if (s.size() == t.size()) {
    return ((s < t) ? t : s);
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  rep(i, 0, m) cin >> A[i];
  rep(i, 0, 100001) dp[i] = INF_S;

  dp[0] = "";

  repe(i, 0, n) {
    if (dp[i] == INF_S)
      continue;

    rep(j, 0, m) {
      dp[i + match[A[j] - 1]] =
          chmax(dp[i + match[A[j] - 1]], dp[i] + (char)('0' + A[j]));
    }
  }
  cout << dp[n] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n; i > s; i--)
#define rep_re(i, s, n) for (ll i = n; i >= s; i--)
using namespace std;
static const ll MOD = 1e9 + 7;
static const ll INF = 1e25;
static const ll MAX_N = 100001;

ll match[10] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[100001];
string INF_S = "-";
ll A[10];

string chmax(string s, string t) {
  if (s == INF_S)
    return t;
  else if (s.size() < t.size())
    return t;
  else if (s.size() > t.size())
    return s;
  else if (s.size() == t.size()) {
    return ((s < t) ? t : s);
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  rep(i, 0, m) cin >> A[i];
  rep(i, 0, 100001) dp[i] = INF_S;

  dp[0] = "";

  repe(i, 0, n) {
    if (dp[i] == INF_S)
      continue;

    rep(j, 0, m) {
      dp[i + match[A[j] - 1]] =
          chmax(dp[i + match[A[j] - 1]], dp[i] + (char)('0' + A[j]));
    }
  }
  cout << dp[n] << endl;

  return 0;
}