Language: C++

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using vdp=vector<vector<vector<ll>>>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
using Graph = vector<vector<int>>;
const double pi=acos(-1);
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<MOD>;
vector<bool> seen,finished;
int pos = -1;
int ca=0;
int f=0;
void dfs(const Graph &G, int v, int p) {
    seen[v] = true;
    for (auto nv : G[v]) {
        if (nv == p) continue;
        if (finished[nv]) continue;
      if (seen[nv] && !finished[nv]) {
            pos = nv;f++; 
            return;
        }
      dfs(G, nv, v);
 
        // サイクル検出したならば真っ直ぐに抜けていく
        if (pos != -1) {
        return;}}
      finished[v] = true;
}
    
mint calc(long long N, long long K) {
    mint res = 1;
    for (long long n = 0; n < K; ++n) {
        res *= (N - n);
        res /= (n + 1);
    }
    return res;
}
struct UnionFind {
    vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
 
    UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化
        for(ll i = 0; i < N; i++) par[i] = i;
    }
 
    ll root(ll x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
 
    void unite(ll x, ll y) { // xとyの木を併合
        ll rx = root(x); //xの根をrx
        ll ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }
 
    bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
// a^{-1} mod を計算する
ll modinv(ll a, ll mod) {
    return modpow(a, mod - 2, mod);
}
ll dp[200005][5];
int main(){
ll n,m; cin>>n>>m;
ll a[m]={};
  ll k = 10;
set<ll> S;
rep(i,m){
cin>>a[i];
  S.insert(a[i]);}
 if(S.count(5)){
   if(S.count(2)){
     S.erase(2);}
   if(S.count(3)){
     S.erase(3);}
 }
  else if(S.count(3)){
    if(S.count(2)){
      S.erase(2);}
  }
  if(S.count(9)){
    if(S.count(6)){
      S.erase(6);}
  }
  ll b[30]={};
  ll j = 0;
  if(S.count(1)){
    k = min(k,(ll)2);
  b[j]=2;
  j++;}
  if(S.count(2)){
    k = min(k,(ll)5);
    b[j]=5;
  j++;}
  if(S.count(3)){
    k = min(k,(ll)5);
    b[j]=5;
  j++;}
  if(S.count(4)){
    k = min(k,(ll)4);
    b[j]=4;
  j++;}
  if(S.count(5)){
    k = min(k,(ll)5);
    b[j]=5;
  j++;}
  if(S.count(6)){
    k = min(k,(ll)6);
    b[j]=6;
  j++;}
  if(S.count(7)){
    k = min(k,(ll)3);
    b[j]=3;
  j++;}
  if(S.count(8)){
    k = min(k,(ll)7);
    b[j]=7;
  j++;}
  if(S.count(9)){
    k = min(k,(ll)6);
    b[j]=6;
  j++;}
  string s = "";
  if(k==2){
    if(n%2==0){
      ll ca = n/2;
      s = string(ca,'1');
      cout << s << endl;
      return 0;}
    else{
      if(S.count(7)){
        ll ca = (n-3)/2;
        s = string(ca,'1');
        string t = "7";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(2)){
        ll ca = (n-5)/2;
        s = string(ca,'1');
        string t = "2";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(3)){
        ll ca = (n-5)/2;
        s = string(ca,'1');
        string t = "3";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(5)){
        ll ca = (n-5)/2;
        s = string(ca,'1');
        string t = "7";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-7)/2;
        s = string(ca,'1');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
  else if(k==3){
       if(n%3==0){
      ll ca = n/3;
      s = string(ca,'7');
      cout << s << endl;
      return 0;}
    else if(n%3==1){
      if(S.count(4)){
        ll ca = (n-4)/3;
        s = string(ca,'7');
        string t = "4";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-7)/3;
        s = string(ca,'7');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(2)){
        ll ca = (n-2*5)/3;
        s = string(ca,'7');
        string t = "22";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(3)){
        ll ca = (n-5*2)/3;
        s = string(ca,'7');
        string t = "33";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(5)){
        ll ca = (n-5*2)/3;
        s = string(ca,'7');
        string t = "5";
        s += t;
        cout << s << endl;
        return 0;}
    }
     else{
       if(S.count(2)){
        ll ca = (n-5)/3;
        s = string(ca,'7');
        string t = "2";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(3)){
        ll ca = (n-5)/3;
        s = string(ca,'7');
        string t = "3";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(5)){
        ll ca = (n-5)/3;
        s = string(ca,'7');
        string t = "5";
        s += t;
        cout << s << endl;
        return 0;}
       if(S.count(4)){
        ll ca = (n-2*4)/3;
        s = string(ca,'7');
        string t = "44";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-7*2)/3;
        s = string(ca,'7');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
     }
     }
  else if(k==4){
    if(n%4==0){
      ll ca = n/4;
      s = string(ca,'4');
      cout << s << endl;
      return 0;}
    else if(n%4==1){
      if(S.count(2)){
        ll ca = (n-5)/4;
        s = string(ca,'4');
        string t = "2";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(3)){
        ll ca = (n-5)/4;
        s = string(ca,'4');
        string t = "3";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(5)){
        ll ca = (n-5)/4;
        s = string(ca,'4');
        string t = "5";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-7*3)/4;
        s = string(ca,'4');
        string t = "888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%4==2){
      if(S.count(6)){
        ll ca = (n-6)/4;
        s = string(ca,'4');
        string t = "6";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-6)/4;
        s = string(ca,'4');
        string t = "9";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(2)){
        ll ca = (n-2*5)/4;
        s = string(ca,'4');
        string t = "22";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(3)){
        ll ca = (n-2*5)/4;
        s = string(ca,'4');
        string t = "33";
        s += t;
        cout << s << endl;
        return 0;}
      if(S.count(5)){
        ll ca = (n-2*5)/4;
        s = string(ca,'4');
        string t = "55";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-2*7)/4;
        s = string(ca,'4');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(k%4==3){
      if(S.count(8)){
        ll ca = (n-7)/4;
        s = string(ca,'4');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(2)){
        if(S.count(6)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "6";
            t += s;
            t += "2";
          cout << t << endl;
          return 0;}
        else if(S.count(9)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "9";
            t += s;
            t += "2";
          cout << t << endl;
          return 0;}
        else{
        ll ca = (n-3*5)/4;
        s = string(ca,'4');
        string t = "222";
        s += t;
        cout << s << endl;
        return 0;}}
      if(S.count(3)){
        if(S.count(6)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "6";
            t += s;
            t += "3";
          cout << t << endl;
          return 0;}
        else if(S.count(9)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "9";
            t += s;
            t += "3";
          cout << t << endl;
          return 0;}
        else{
        ll ca = (n-3*5)/4;
        s = string(ca,'4');
        string t = "333";
        s += t;
        cout << s << endl;
        return 0;}}
      if(S.count(5)){
        if(S.count(6)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "65";
            t += s;
          cout << t << endl;
          return 0;}
        else if(S.count(9)){
          ll ca =(n-11)/4;
          s = string(ca,'4');
          string t = "95";
            t += s;
          cout << t << endl;
          return 0;}
        else{
        ll ca = (n-3*5)/4;
        s = string(ca,'4');
        string t = "555";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
  else if(k==5&&S.count(2)){
    if(n%5==0){
      ll ca = n/5;
      s = string(ca,'2');
      cout << s << endl;
      return 0;}
    else if(n%5==1){
       if(S.count(6)){
        ll ca = (n-6)/5;
        s = string(ca,'2');
        string t = "6";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-6)/5;
        s = string(ca,'2');
        string t = "9";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-3*7)/5;
        s = string(ca,'2');
        string t = "888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==2){
      if(S.count(8)){
        ll ca = (n-7)/5;
        s = string(ca,'2');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-12)/5;
        s = string(ca,'2');
        string t = "66";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-12)/5;
        s = string(ca,'2');
        string t = "99";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==3){
      if(S.count(6)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'2');
        string t = "86";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'2');
        string t = "98";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-18)/5;
        s = string(ca,'2');
        string t = "666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-18)/5;
        s = string(ca,'2');
        string t = "999";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-4*7)/5;
        s = string(ca,'2');
        string t = "8888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==4){
      if(S.count(8)){
        ll ca = (n-2*7)/5;
        s = string(ca,'2');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-24)/5;
        s = string(ca,'2');
        string t = "6666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-24)/5;
        s = string(ca,'2');
        string t = "9999";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
 else if(k==5&&S.count(3)){
    if(n%5==0){
      ll ca = n/5;
      s = string(ca,'3');
      cout << s << endl;
      return 0;}
    else if(n%5==1){
       if(S.count(6)){
        ll ca = (n-6)/5;
        s = string(ca,'3');
        string t = "6";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-6)/5;
        s = string(ca,'3');
        string t = "9";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-3*7)/5;
        s = string(ca,'3');
        string t = "888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==2){
      if(S.count(8)){
        ll ca = (n-7)/5;
        s = string(ca,'3');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-12)/5;
        s = string(ca,'3');
        string t = "66";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-12)/5;
        s = string(ca,'3');
        string t = "99";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==3){
      if(S.count(6)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'3');
        string t = "86";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'3');
        string t = "98";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-18)/5;
        s = string(ca,'3');
        string t = "666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-18)/5;
        s = string(ca,'3');
        string t = "999";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-4*7)/5;
        s = string(ca,'3');
        string t = "8888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==4){
      if(S.count(8)){
        ll ca = (n-2*7)/5;
        s = string(ca,'3');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-24)/5;
        s = string(ca,'3');
        string t = "6666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-24)/5;
        s = string(ca,'3');
        string t = "9999";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
else if(k==5&&S.count(5)){
    if(n%5==0){
      ll ca = n/5;
      s = string(ca,'5');
      cout << s << endl;
      return 0;}
    else if(n%5==1){
       if(S.count(6)){
        ll ca = (n-6)/5;
        s = string(ca,'5');
        string t = "6";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-6)/5;
        s = string(ca,'5');
        string t = "9";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-3*7)/5;
        s = string(ca,'5');
        string t = "888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==2){
      if(S.count(8)){
        ll ca = (n-7)/5;
        s = string(ca,'5');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-12)/5;
        s = string(ca,'5');
        string t = "66";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-12)/5;
        s = string(ca,'5');
        string t = "99";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==3){
      if(S.count(6)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'5');
        string t = "86";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)&&S.count(8)){
        ll ca = (n-13)/5;
        s = string(ca,'5');
        string t = "98";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-18)/5;
        s = string(ca,'5');
        string t = "666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-18)/5;
        s = string(ca,'5');
        string t = "999";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(8)){
        ll ca = (n-4*7)/5;
        s = string(ca,'5');
        string t = "8888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(n%5==4){
      if(S.count(8)){
        ll ca = (n-2*7)/5;
        s = string(ca,'5');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(6)){
        ll ca = (n-24)/5;
        s = string(ca,'5');
        string t = "6666";
        t += s;
        cout << t << endl;
        return 0;}
      if(S.count(9)){
        ll ca = (n-24)/5;
        s = string(ca,'5');
        string t = "9999";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
  else if(k==6&&S.count(6)){
    if(k%6==0){
      if(S.count(6)){
        ll ca = n/6;
        s = string(ca,'6');
        cout << s << endl;
        return 0;}
    }
    else if(k%6==1){
      if(S.count(8)){
        ll ca = (n-7)/6;
        s = string(ca,'6');
        string t = "8";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(k%6==2){
      if(S.count(8)){
        ll ca = (n-2*7)/6;
        s = string(ca,'6');
        string t = "88";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(k%6==3){
      if(S.count(8)){
        ll ca = (n-7*3)/6;
        s = string(ca,'6');
        string t = "888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(k%6==4){
      if(S.count(8)){
        ll ca = (n-4*7)/6;
        s = string(ca,'6');
        string t = "8888";
        t += s;
        cout << t << endl;
        return 0;}
    }
    else if(k%6==5){
      if(S.count(8)){
        ll ca = (n-5*7)/6;
        s = string(ca,'6');
        string t = "88888";
        t += s;
        cout << t << endl;
        return 0;}
    }
  }
  else if(k==6&&S.count(9)){
    if(k%6==0){
      if(S.count(9)){
        ll ca = n/6;
        s = string(ca,'9');
        cout << s << endl;
        return 0;}
    }
    else if(k%6==1){
      if(S.count(8)){
        ll ca = (n-7)/6;
        s = string(ca,'9');
        string t = "8";
        s += t;
        cout << s << endl;
        return 0;}
    }
    else if(k%6==2){
      if(S.count(8)){
        ll ca = (n-2*7)/6;
        s = string(ca,'9');
        string t = "88";
        s += t;
        cout << s << endl;
        return 0;}
    }
    else if(k%6==3){
      if(S.count(8)){
        ll ca = (n-7*3)/6;
        s = string(ca,'9');
        string t = "888";
        s += t;
        cout << s << endl;
        return 0;}
    }
    else if(k%6==4){
      if(S.count(8)){
        ll ca = (n-4*7)/6;
        s = string(ca,'9');
        string t = "8888";
        s += t;
        cout << s << endl;
        return 0;}
    }
    else if(k%6==5){
      if(S.count(8)){
        ll ca = (n-5*7)/6;
        s = string(ca,'9');
        string t = "88888";
        s += t;
        cout << s << endl;
        return 0;}
    }
  }
  else if(k==7){
    if(S.count(8)){
        ll ca = n/7;
        s = string(ca,'8');
        cout << s << endl;
        return 0;}
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:950:2: error: expected '}' at end of input
  950 | }
      |  ^
a.cc:150:11: note: to match this '{'
  150 | int main(){
      |           ^