This is the easy version of the problem. The only difference is the constraints on nn and kk. You can make hacks only if all versions of the problem are solved.
You have a string ss, and you can do two types of operations on it:
You can use each operation any number of times (possibly none).
Your task is to find the lexicographically smallest string of length exactly kk that can be obtained by doing these operations on string ss.
A string aa is lexicographically smaller than a string bb if and only if one of the following holds:
Input
The first line contains two integers nn, kk (1≤n,k≤50001≤n,k≤5000) — the length of the original string ss and the length of the desired string.
The second line contains the string ss, consisting of nn lowercase English letters.
Output
Print the lexicographically smallest string of length kk that can be obtained by doing the operations on string ss.
Examples
Input
8 16 dbcadabc
Output
dbcadabcdbcadabc
Input
4 5 abcd
Output
aaaaa
Note
In the first test, it is optimal to make one duplication: "dbcadabc" →→ "dbcadabcdbcadabc".
In the second test it is optimal to delete the last 33 characters, then duplicate the string 33 times, then delete the last 33 characters to make the string have length kk.
"abcd" →→ "abc" →→ "ab" →→ "a" →→ "aa" →→ "aaaa" →→ "aaaaaaaa" →→ "aaaaaaa" →→ "aaaaaa" →→"aaaaa".
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char s[100001], t[100001], ans[100001];
int n, k;
bool cmp()
{
for(int i = 0; i < k; i++)
{
if(ans[i] > t[i]) return 1;
else if(ans[i] == t[i]) continue;
else return 0;
}
return 0;
}
int main()
{
cin >> n >> k;
scanf("%s",s);
for(int i = 1; i <= n; i++)
{
int cnt = k/i + (k%i!=0);
for(int j = 0; j < cnt; j++)
{
for(int l = 0; l < i; l++)
t[i*j+l] = s[l];
}
t[k] = '\0';
if(i>1)
{
if(cmp()) strcpy(ans, t);
}
else strcpy(ans, t);
// printf("this t is %s\n",t);
}
printf("%s\n",ans);
// system("pause");
}