Language: C++

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef unsigned long long ull;

const int N=2005;

int n,m,p[N],q[N],a[1002005],f[N],len[N],now,sz;
ull hash[1002005];
bitset<10005> arr[N],bit;
char str[1000005];

bool cmp(int x,int y)
{
	int l=1,r=min(len[x],len[y]);
	while (l<=r)
	{
		int mid=(l+r)/2;
		if (hash[p[x]+mid]==hash[p[y]+mid]) l=mid+1;
		else r=mid-1;
	}
	if (l>min(len[x],len[y])) return len[x]<len[y];
	else return a[p[x]+l]<a[p[y]+l];
}

void pri(int x)
{
	for (int i=1;i<=len[x];i++) putchar(a[p[x]+i]+'a');
}

void solve(int x,int L)
{
	if (!L) return;
	int now=1;
	while (f[now]<x||!arr[f[now]][L]) now++;
	pri(f[now]);
	solve(f[now]+1,L-len[f[now]]);
}

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%s",str+1);
		len[i]=strlen(str+1);
		p[i]=sz+1;q[i]=sz+len[i]+1;
		for (int j=1;j<=len[i];j++)
		{
			hash[p[i]+j]=hash[p[i]+j-1]*27+str[j]-'a'+1;
			a[p[i]+j]=str[j]-'a';
		}
		sz=q[i];
	}
	for (int i=1;i<=n;i++) f[i]=i;
	sort(f+1,f+n+1,cmp);
	bit[0]=1;
	for (int i=n;i>=1;i--) arr[i]=bit<<len[i],bit|=bit<<len[i];
	solve(1,m);
	return 0;
}

Compiler output:
a.cc: In function 'bool cmp(int, int)':
a.cc:24:21: error: reference to 'hash' is ambiguous
   24 |                 if (hash[p[x]+mid]==hash[p[y]+mid]) l=mid+1;
      |                     ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:14:5: note:                 'ull hash [1002005]'
   14 | ull hash[1002005];
      |     ^~~~
a.cc:24:37: error: reference to 'hash' is ambiguous
   24 |                 if (hash[p[x]+mid]==hash[p[y]+mid]) l=mid+1;
      |                                     ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:14:5: note:                 'ull hash [1002005]'
   14 | ull hash[1002005];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:55:25: error: reference to 'hash' is ambiguous
   55 |                         hash[p[i]+j]=hash[p[i]+j-1]*27+str[j]-'a'+1;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:14:5: note:                 'ull hash [1002005]'
   14 | ull hash[1002005];
      |     ^~~~
a.cc:55:38: error: reference to 'hash' is ambiguous
   55 |                         hash[p[i]+j]=hash[p[i]+j-1]*27+str[j]-'a'+1;
      |                                      ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:14:5: note:                 'ull hash [1002005]'
   14 | ull hash[1002005];
      |     ^~~~