E - Erase and Extend (Easy Version)

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: 

  • Delete the last character of the string. 
  • Duplicate the string: s:=s+ss:=s+s, where ++ denotes concatenation. 

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:

  • aa is a prefix of bb, but a≠ba≠b; 
  • In the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb. 

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");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值