Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <unordered_map>
#pragma GCC optimize("Ofast")
using namespace std;

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file                                                                   \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v)                                                              \
  cerr << #v << ": [ ";                                                        \
  for (int __ = 0; __ < v.size(); __++)                                        \
    cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends);                     \
  cerr << ']' << endl
#define debugVP(v)                                                             \
  cerr << #v << ": [ ";                                                        \
  for (int ___ = 0; ___ < v.size(); ___++)                                     \
    cerr << '{' << v[___].first << ", " << v[___].second << '}'                \
         << ((___ != v.size() - 1) ? ", " : ends);                             \
  cerr << ']' << endl
#define debugA(a, n)                                                           \
  cerr << #a << ": [ ";                                                        \
  for (int ____ = 0; ____ < n; ____++)                                         \
    cerr << a[____] << ((____ != n - 1) ? ", " : ends);                        \
  cerr << ']' << endl
#define print(args...) fprintf(st derr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<pii, int> mpii;
typedef set<int> sti;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int power(int a, int b) {
  return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1));
}

/////////////////////////////////////////////////////////////////////

const int N = 1e4 + 20, M = 2e1 + 2;

vi v;
int a[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool dp[N][N];

int32_t main() {

  sync;

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }

  sort(all(v));
  reverse(all(v));

  dp[0][0] = true;
  for (int i = 1; i <= n / 2; i++)
    for (int j = 1; j <= n; j++)
      for (int k : v)
        if (j >= a[k])
          dp[i][j] |= dp[i - 1][j - a[k]];

  int mx = 0;
  for (int i = 1; i <= n / 2; i++)
    if (dp[i][n])
      mx = i;

  vi ans;
  for (int i = 1; i <= mx; i++)
    for (int k : v)
      if (a[k] <= n && dp[mx - i][n - a[k]]) {
        n -= a[k];
        ans.push_back(k);
        break;
      }

  for (int k : ans)
    cout << k;
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast")
using namespace std;

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file                                                                   \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v)                                                              \
  cerr << #v << ": [ ";                                                        \
  for (int __ = 0; __ < v.size(); __++)                                        \
    cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends);                     \
  cerr << ']' << endl
#define debugVP(v)                                                             \
  cerr << #v << ": [ ";                                                        \
  for (int ___ = 0; ___ < v.size(); ___++)                                     \
    cerr << '{' << v[___].first << ", " << v[___].second << '}'                \
         << ((___ != v.size() - 1) ? ", " : ends);                             \
  cerr << ']' << endl
#define debugA(a, n)                                                           \
  cerr << #a << ": [ ";                                                        \
  for (int ____ = 0; ____ < n; ____++)                                         \
    cerr << a[____] << ((____ != n - 1) ? ", " : ends);                        \
  cerr << ']' << endl
#define print(args...) fprintf(st derr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<pii, int> mpii;
typedef set<int> sti;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int power(int a, int b) {
  return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1));
}

/////////////////////////////////////////////////////////////////////

const int N = 1e4 + 20, M = 2e1 + 2;

vi v;
int a[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool dp[N][N];

int32_t main() {

  sync;

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }

  sort(all(v));
  reverse(all(v));

  dp[0][0] = true;
  for (int i = 1; i <= n / 2; i++)
    for (int j = 1; j <= n; j++)
      for (int k : v)
        if (j >= a[k])
          dp[i][j] |= dp[i - 1][j - a[k]];

  int mx = 0;
  for (int i = 1; i <= n / 2; i++)
    if (dp[i][n])
      mx = i;

  vi ans;
  for (int i = 1; i <= mx; i++)
    for (int k : v)
      if (a[k] <= n && dp[mx - i][n - a[k]]) {
        n -= a[k];
        ans.push_back(k);
        break;
      }

  for (int k : ans)
    cout << k;
  cout << endl;

  return 0;
}