#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> usingnamespace std; #define MAXN 100006 #define C 130 int k; char ch[MAXN]; int sa[MAXN] , tp[MAXN] , rnk[MAXN] , buc[MAXN] , len , ht[MAXN]; int P[MAXN][19]; voidinit( ){ len = strlen( ch + 1 ); int m = C; for( int i = 1 ; i <= len ; ++ i ) ++ buc[rnk[i] = ch[i]]; for( int i = 1 ; i <= m ; ++ i ) buc[i] += buc[i-1]; for( int i = len ; i >= 1 ; -- i ) sa[buc[rnk[i]] --] = i; for( int k = 1 , p = 0 ; p < len ; k <<= 1 ) { p = 0; for( int i = 0 ; i <= m ; ++ i ) buc[i] = 0; for( int i = len - k + 1 ; i <= len ; ++ i ) tp[++p] = i; for( int i = 1 ; i <= len ; ++ i ) if( sa[i] > k ) tp[++p] = sa[i] - k; for( int i = 1 ; i <= len ; ++ i ) ++ buc[rnk[i]]; for( int i = 1 ; i <= m ; ++ i ) buc[i] += buc[ i-1 ]; for( int i = len ; i >= 1 ; -- i ) sa[buc[rnk[tp[i]]] --] = tp[i]; p = 1; swap( rnk , tp ); rnk[sa[1]] = 1; for( int i = 2 ; i <= len ; ++ i ) rnk[sa[i]] = ( tp[sa[i]] == tp[sa[i-1]] && tp[sa[i] + k] == tp[sa[i-1] + k] ) ? p : ++ p; m = p; } for( int i = 1 ; i <= len ; ++ i ) rnk[sa[i]] = i; for( int i = 1 , k = 0 ; i <= len ; ++ i ) { if( k ) -- k; while( ch[i + k] == ch[sa[rnk[i] - 1] + k] ) ++ k; ht[rnk[i]] = k; P[rnk[i]][0] = k; } }
voidinitst( ){ for( int k = 1 ; k < 19 ; ++ k ) for( int i = 1 ; i <= len - ( 1 << k ) + 1 ; ++ i ) P[i][k] = min( P[i][k - 1] , P[i + ( 1 << k - 1 )][k - 1] ); } intque( int l , int r ){ if( l > r ) swap( l , r ); elseif( l == r ) return0x3f3f3f3f; ++ l; int L = 31 - __builtin_clz( r - l + 1 ); returnmin( P[l][L] , P[r - ( 1 << L ) + 1][L] ); }
int L , R; voidgetlr( longlong x ){ for( int i = 1 ; i <= len ; ++ i ) { int k = len + 1 - ht[i] - sa[i]; if( x > k ) x -= k; else { L = sa[i]; R = sa[i] + ht[i] + x - 1; break; } } } boolcmp( int l , int r , int L , int R ){ // return S[l,r] > S[L,R] int ret = 1; if( rnk[l] < rnk[L] ) swap( l , L ) , swap( r , R ) , ret ^= 1; int l1 = r - l + 1 , l2 = R - L + 1; if( l1 < l2 ) { int lp = que( rnk[l] , rnk[L] ); if( lp >= l1 ) return ret ^ 1; elsereturn ret; } elseif( l1 == l2 ) { int lp = que( rnk[l] , rnk[L] ); if( lp >= l1 ) return0; elsereturn ret; } return ret; } boolchk( longlong x ){ getlr( x ); int r = len , t = 0; for( int i = len ; i >= 1 ; -- i ) { if( ch[i] > ch[L] ) returnfalse; if( cmp( i , r , L , R ) ) r = i , ++ t; if( t >= k ) returnfalse; } returntrue; }
intmain(){ // freopen("2.in","r",stdin); // freopen("ot","w",stdout); cin >> k; scanf("%s",ch+1); init(); initst(); longlong tot = 0; for( int i = 1 ; i <= len ; ++ i ) tot += len + 1 - ht[i] - sa[i]; longlong l = 1 , r = tot; while( l <= r ) { longlong m = l + r >> 1; if( chk ( m ) ) r = m - 1; else l = m + 1; // cout << m << endl; } getlr( l ); for( int i = L ; i <= R ; ++ i ) putchar( ch[i] ); }