Language: C++

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=10005;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
//inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc(){return getchar();}
inline int read(){
	int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
	for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
int n,k,len,l[MAXN];string s,t,ans;char c;
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	n=read(),k=read(),cin>>s,n=s.size();c='z';
	Red(i,n-1,0)s.push_back(s[i]),c=min(c,s[i]);
	if(k>=15){Rep(i,1,n)putchar(c);return 0;}
	if(k==1){
		ans=s.substr(0,n);
		Rep(i,1,n)ans=min(ans,s.substr(i,n));
		cout<<ans;return 0;
	}ll Mx=0;k--;
	Rep(i,0,2*n-1)Mx=max(Mx,i==0?l[i]=1:s[i]==c?l[i]=l[i-1]+1:0);
	if(Mx<<k>=n){return 1;Rep(i,1,n)putchar(c);return 0;}
	int len=Mx<<k;len=n-len;
	int opt=0;
	Rep(i,0,2*n-1)if(l[i]==Mx){
		if(i-Mx+1>=len){
			t=s.substr(0,i-Mx+1),reverse(t.begin(),t.end());
			if(!opt)ans=t,opt=1;else ans=min(ans,t);
		}
	}Rep(i,1,Mx<<k)putchar(c);Rep(i,0,len-1)cout<<ans[i];
	return 0;
}




Compiler output:
a.cc: In function 'int main()':
a.cc:32:29: error: no matching function for call to 'max(long long int&, int)'
   32 |         Rep(i,0,2*n-1)Mx=max(Mx,i==0?l[i]=1:s[i]==c?l[i]=l[i-1]+1:0);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:32:29: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   32 |         Rep(i,0,2*n-1)Mx=max(Mx,i==0?l[i]=1:s[i]==c?l[i]=l[i-1]+1:0);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:32:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   32 |         Rep(i,0,2*n-1)Mx=max(Mx,i==0?l[i]=1:s[i]==c?l[i]=l[i-1]+1:0);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~