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

const int match[10] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
const int INF = 1e9 + 7;

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


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

  while ( n > 0 ) {
    for ( int i = 0; i < m; i++ ) {
      if ( n-match[a[i]] < 0 ) { continue; }
      if ( dp[n-match[a[i]]] == dp[n]-1 ) {
        cout << a[i];
        n -= match[a[i]];
        break;
      }
    }
  }
  cout << endl;
  return 0;
}

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll mod=1e9+7;

int main(){
    ll n,k;
    cin >> n >> k;
    vector<ll> l(n);
    rep(i,n){
        cin >> l[i];
    }
    sort(l.rbegin(),l.rend());
    ll ans=0;
    rep(i,k){
        ans+=l[i];
    }
    cout << ans << endl;
    return 0;
} 
