Language: C++

#include<bits/stdc++.h>
using namespace std;
const int N = 2e4+10;
const int siz = 2e6+10;
#define min(a,b) (a) < (b) ? (a) : (b)

char ch[siz];

int sa[siz],height[siz],x[siz],y[siz];
int rk[siz];
int cntA[siz];
int st[siz][21];

int n,m;
int leng;
void init(int n){
   m = 129;
   for(int i = 1;i <= m; ++i)cntA[i] = 0;
   for(int i = 1;i <= n; ++i)cntA[x[i] = ch[i]]++;
   for(int i = 1;i <= m; ++i)cntA[i] += cntA[i-1];
   for(int i = 1;i <= n; ++i)sa[cntA[x[i]]--] = i;
   for(int l = 1;l <= n; l <<= 1){
      for(int i = 1;i <= m; ++i)cntA[i] = 0;
      int num = 0;
      for(int i = n-l+1;i <= n; ++i)y[++num] = i;
      for(int i = 1;i <= n; ++i)if(sa[i] > l)y[++num] = sa[i]-l;
      for(int i = 1;i <= n; ++i)cntA[x[i]]++;
      for(int i = 1;i <= m; ++i)
         cntA[i] += cntA[i-1];
      for(int i = 1;i <= n; ++i)sa[cntA[x[y[i]]]--] = y[i],y[i] = 0;
      swap(x,y);
      num = 1;
      x[sa[1]] = 1;
      for(int i = 2;i <= n; ++i){
         x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i]+l] == y[sa[i-1]+l])?num:++num;
      }
      if(num == n)break;
      m = num;
   }
   for(int i = 1;i <= n; ++i){
      rk[sa[i]] = i;
   }
   height[1] = 0;
   int cur = 0;
   for(int i = 1;i <= n; ++i){
      int j = sa[rk[i]-1];
      if(cur != 0)--cur;
      while(ch[j+cur] == ch[i+cur])++cur;
      height[rk[i]] = cur;
      st[rk[i]][0] = cur;
   }
   for(int i = 1;i <= 20; ++i){
      for(int j = 1;j+(1<<i) <= n+1; ++j){
         st[j][i] = min(st[j][i-1],st[j+(1<<(i-1))][i-1]);
      }
   }
}

int lg[siz];
int lcp(int x,int y){
   int w1 = rk[x],w2 = rk[y];
   if(w1 > w2)swap(w1,w2);
   ++w1;
   return min(st[w1][lg[w2-w1+1]],st[w2-(1<<lg[w2-w1+1])][lg[w2-w1+1]]);
}

int cmp(char *a,char *b,int siz){
   for(int i = 1;i <= siz; ++i){
      if(a[i] != b[i])return a[i] < b[i];
   }
}


string str[N];
int pos[N];
int ch2[N];
int lenpos[N];
int lenchar[N];
int chlen,k;
int fh[siz];
char minch[N];
int lenbag[2011][N];
int main(){
   lg[1] = 0;
   for(int i = 1;i < siz; ++i)lg[i] = lg[i/2]+1;
   scanf("%d%d",&n,&k);
   pos[0] = -1;
   for(int i = 1;i <= n; ++i){
      cin >> str[i];
      lensum[i] = lensum[i-1]+(int)str[i].size();
      for(int j = pos[i-1]+2; j <= pos[i-1]+1+(int)str[i].size(); ++j){
         ch[j] = str[i][j-pos[i-1]-2];
      }
      pos[i] = pos[i-1]+1+str[i].size();
      fh[pos[i]] = i;
      ch[pos[i]+1] = '#';
   }
   lenbag[n+1][0] = 1;
   for(int i = n;i >= 1; --i){
      for(int j = k;j >= 0; --j){
         if(j >= (int)str[i].size())lenbag[i][j] |= lenbag[i+1][j-str[i].size()];
         lenbag[i][j] |= lenbag[i+1][j];
      }
   }
   chlen = pos[n];
   init(chlen);
   lenpos[0] = 0;
   for(int i = 1;i <= k; ++i)minch[i] = 'z';
   for(int i = 0;i <= k; ++i){
      if(i && !lenpos[i])continue ;
      if(i){
         string si;
         int cur = i;
         while(cur){
            si = str[fh[lenpos[cur]]]+si;
            cur -= str[fh[lenpos[cur]]].size();
         }
         si = '0'+si;
         bool flag = false;
         bool fail = false;
         for(int j = 1;j <= i; ++j){
            if(si[j] < minch[j]){
               flag = true;
               minch[j] = si[j];
            }
            else if(!flag && minch[j] < si[j]){
               fail = true; break;
            }
            if(flag)minch[j] = si[j];
         }
         if(fail){
            lenpos[i] = 0;continue ;
         }
      }
      for(int j = 1;j <= n; ++j){
         if(pos[j] <= lenpos[i])continue ;
         if(i+(int)str[j].size() > k)continue ;
         if(!lenbag[j+1][k-i-(int)str[j].size()])continue ;
         int lenp = i+str[j].size();
         if(!lenpos[lenp])lenpos[lenp] = pos[j];
         else {
            if(lcp(lenpos[lenp]-(int)str[j].size()+1,
                     pos[j]-(int)str[j].size()+1) == str[j].size()){
               lenpos[lenp] = min(lenpos[lenp],pos[j]);
            } else if(rk[lenpos[lenp]-str[j].size()+1] > rk[pos[j]-str[j].size()+1]){
               lenpos[lenp] = pos[j];
            }
         }
      }
   }
   string ret;
   int cur = k;
   while(cur){
      ret = str[fh[lenpos[cur]]] + ret;
      cur = cur-str[fh[lenpos[cur]]].size();
   }
   cout << ret << endl;
   return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:90:7: error: 'lensum' was not declared in this scope; did you mean 'enum'?
   90 |       lensum[i] = lensum[i-1]+(int)str[i].size();
      |       ^~~~~~
      |       enum
a.cc: In function 'int cmp(char*, char*, int)':
a.cc:71:1: warning: control reaches end of non-void function [-Wreturn-type]
   71 | }
      | ^