Language: C++

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define F first
#define S second
#define SZ(a) (int)((a).size())
#define pb push_back
#define mp make_pair
#define ALL(a) (a).begin(),(a).end()
using namespace std;

typedef long long ll;
typedef pair<int,int> PI;
typedef unsigned long long ull;

#define PR(...) do{cerr << "line : " << __LINE__ << endl; pr(#__VA_ARGS__, __VA_ARGS__);}while(0);
template<class T>
void pr(const string& name, T t){
  cerr << name << ": " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest) {
  auto comma_pos = names.find(',');
  cerr << names.substr(0, comma_pos) << ": " << t << ", ";
  
  auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
  pr(string(names, next_name_pos), rest ...);
}

template<class T,class U> ostream& operator<< (ostream& o, const pair<T,U>& v){return o << "(" << v.F << ", " << v.S << ")";}
template<class T> ostream& operator<< (ostream& o, const vector<T>& v){o << "{";rep(i,SZ(v)) o << (i?", ":"") << v[i];return o << "}";}
template<class T> ostream& operator<< (ostream& o, const set<T> &v){o << "{";for(auto e : v) o << e << ", ";return o << "}";}
template<class T, class U> ostream& operator<< (ostream& o, const map<T,U> &v){o << "{";for(auto e : v) o << e.F << ": " << e.S << ", ";return o << "}";}

//                < v > ^
const int dx[] = {0,1,0,-1, 1, 1, -1, -1};
const int dy[] = {-1,0,1,0, 1, -1, 1, -1};
#define endl '\n'


#include <sys/time.h>

double gett(){
  struct timeval t;
  gettimeofday(&t,NULL);
  return t.tv_sec + t.tv_usec/1e6;
}


int n, k;
string s[2000];
vector<pair<string, int> > sos;

char memo[2001][10010];
bool can(int ne, int res) {
  if (res < 0) return false;
  if(res == 0) return true;
  if(ne == n) return false;
  if(memo[ne][res>=0)
     return memo[ne][res];
  
  for (int i = ne; i < n; ++i) {
    if (can(i + 1, res - SZ(s[i])))
      return memo[ne][res] = true;
  }

  return memo[ne][res] = false;
}

double start;

string ans;
int ach;
int gcnt = 0;
void dfs(const string& cur, int lastidx) {
  // PR(cur, lastidx);

  if(!ans.empty() && ans.substr(0, SZ(cur)) < cur)
    return;

  if (++gcnt % 100 == 0 && 
      gett() - start > 4.7) {
    cout << ans << endl;
    exit(0);
  }

  if(!can(lastidx + 1, k - SZ(cur))) return;
  if(SZ(cur) == k) {
    ans = cur;
    ++ach;
    return;
  }
  int bef = ach;
  for(const auto& s : sos) {
    if(s.second <= lastidx) continue;
    if(!can(s.second + 1, k - SZ(cur) - SZ(s.first))) continue;
    dfs(cur + s.first, s.second);
  }
}

int main(int argc, char *argv[])
{
  start = gett();
  cin >> n >> k;
  rep (i, n) cin >> s[i];
  memset(memo, -1, sizeof(memo));
  // for (int i = n - 1; i >= 0; --i) {
  //   for (int j = 0; j <= k; ++j) {
  //     if(j-SZ(s[i]) < 0) 
  // 	memo[i][j] = false;
  //     else if(j-SZ(s[i]) == 0) 
  // 	memo[i][j] = true;
  //     else if (i + 1 == n){
  // 	memo[i][j] = false;
  //     }else if (memo[i + 1][j - SZ(s[i])]){
  // 	memo[i][j] = true;
  //     }
  //   }
  // }

  rep (i, n) sos.emplace_back(s[i], i);

  sort(ALL(sos));
  
  for (int i = 0; i < sos.size(); ++i) {
    dfs(sos[i].F, sos[i].S);
  }
  
  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'bool can(int, int)':
a.cc:60:21: error: expected ']' before ')' token
   60 |   if(memo[ne][res>=0)
      |                     ^
      |                     ]