cf570A
题意 一个数的数位和%4如果为 0 是我们要的答案 问加几次能使得他数位和 %4 为0 暴力即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
scanf("%d",&n);
while(1)
{
int tmp = 0;
int n_ = n;
while(n_)
{
tmp+=n_%10;
n_/=10;
}
if(tmp%4==0) break;
else n++;
}
printf("%d\n",n);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf570B
题意 给你n个商品 每个商品都有价格 然后商品后来价格和之前价格不能相差大于k
问你是否有可能所有商品价格一致 一致就取最大 否则输出-1
那么我们知道每个商品由于k 都有一个取值区间 max(0,val - k) 和 val+k
我们再用一个minn和一个maxx去缩小这边界判断即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
int arr[105],l[105],r[105];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n,k,minn = 2e8+5,maxx = -1;
scanf("%d%d",&n,&k);
for(int i = 1;i<=n;++i)
{
scanf("%d",&arr[i]);
l[i] = max(0,arr[i]-k);
r[i] = arr[i]+k;
maxx = max(maxx,l[i]);
minn = min(minn,r[i]);
}
if(maxx<=minn) printf("%d\n",minn);
else printf("-1\n");
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
CF570C
这题也蛮有意思 电脑有k电量 如果 k > a 那么电脑可以运行 电量-a
如果 k > b(其中b<a)那么电脑可以充电运行 电量-b
问你电脑能否一直运行 能则输出 a电量最多运行的情况
因为是 > 不是 >= 所以我们先把 k - 1 然后二分使用a的次数即可求出答案
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
ll k,n,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld%lld",&k,&n,&a,&b);
k--;
ll l = 0,r = n;
while(l<=r)
{
ll mid = (l+r)>>1;
if((n-mid)*b+mid*a<=k) l = mid + 1;
else r = mid - 1;
}
printf("%lld\n",r);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf570D
给你n个糖 每个糖有不同种类 做蛋糕用的不同种类糖数量必须不同 问你蛋糕最多多大
我们先知道每个种类糖数量 然后从大到小 如果可以做 就直接做 如果不行 找小于他的最大的能放进去的数目 相加 利用set完成我们想要的操作
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 200025;
set<int > st,ST;
multiset<int> st_;
int cnt[MAX_N],arr[MAX_N];
bool cmp(int a,int b)
{
return a >b;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n;
long long ans = 0;
scanf("%d",&n);
st.clear();
ST.clear();
st_.clear();
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),st.insert(arr[i]),cnt[arr[i]]++;
for(set<int>::iterator it = st.begin();it!=st.end();++it)
{
st_.insert(-cnt[*it]);
cnt[*it] = 0;
}
bool flag = true;
int cnt = 0;
for(set<int>::iterator it = st_.begin();it!=st_.end();++it)
{
if(flag)
{
ll now = -(*it);
for(int i = 1;i<=now;++i) ST.insert(i);
flag = false;
ans+=now;
ST.erase(now);
}
else
{
ll now = -(*it);
if(ST.count(now))
{
ans+=now;
ST.erase(now);
}
else
{
set<int>::iterator it_ = ST.lower_bound(now);
if(it_==ST.begin()) break;
else it_--;
ans+=*it_;
int tmp = *it_;
ST.erase(tmp);
}
}
}
printf("%lld\n",ans);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf570E
给你n长度的字符串 删i个字符形成的子序列的代价是i 问形成k个不同字符串的代价是多少
利用bfs 把不断能删的串从大到小放进一个队列里面 也就是用一个队列维护现在当前串
然后用一个集合set判断这个串删除一个字符是不是已经出现过 然后进行维护 因为k<100
所以bfs是可行的
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
queue<string > q;
set<string > st;
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
long long k,ans = 0;
string str;
cin >> n>>k;
cin >>str;
st.insert(str);
q.push(str);
while(!q.empty()&&st.size()<k)
{
string v = q.front();
q.pop();
int len = v.length();
for(int i = 0;i<len;++i)
{
string nv = v;
nv.erase(i,1);
if(!st.count(nv)&&st.size()+1<=k)
{
q.push(nv);
st.insert(nv);
ans+=n-len+1;
}
}
}
if(st.size()<k) cout << "-1"<<endl;
else cout << ans<<endl;
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
CF1183 F
给你n个数 问你最多选3个 选出互不整除的数使得和最大
做法是从大到小 枚举一个选的数 x
然后从不整除他的所有数里面选最大的数 y
然后从不整除这两个数 x 和 y 里面选一个最大的数 z
为什么一定要选最大的?
从这个角度考虑
假设最大的数M 不是次大的数m的倍数 那么你直接取最大和次大显然可以
如果最大的数M是次大的数m的倍数 就算最小的倍数2
次次大 + 次大 < 次大 + 次大 <= 最大
所以要选最大的
然后离散化一样 因为一样的肯定会互相整除
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 200025;
int arr[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n,ans = 0;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
sort(arr+1,arr+1+n);
int sz = unique(arr+1,arr+1+n)-arr-1;
for(int i = sz;i>=1;--i)
{
ans = max(ans,arr[i]);
for(int j = i-1;j>=1;--j)
{
if(arr[i]%arr[j]!=0)
{
ans = max(ans,arr[i]+arr[j]);
for(int k = j-1;k>=1;--k)
{
if(arr[i]%arr[k]!=0&&arr[j]%arr[k]!=0)
{
ans = max(ans,arr[i]+arr[j]+arr[k]);
break;
}
}
break;
}
}
}
printf("%d\n",ans);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf570G
D的升级版本 就是问如果每个糖也有个愉悦度 0 1, 1代表想吃 问你数目最大 1 最多的情况是什么
这样我们按照刚才的做法会出现问题
所以我们借助一个优先队列存1的个数 然后从大到小塞
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 200025;
int a[MAX_N],b[MAX_N],cnt[MAX_N],cnt_[MAX_N];
set<int> st;
struct node
{
int a,b;
bool operator < (const node other) const&
{
if(a==other.a) return b >other.b;
return a >other.a;
}
}arr[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n,now = 0;
long long ans = 0,ans_ = 0;
scanf("%d",&n);
st.clear();
for(int i = 1;i<=n;++i)
{
scanf("%d%d",&a[i],&b[i]);
st.insert(a[i]);
cnt[a[i]]++;
if(b[i]==1) cnt_[a[i]]++;
}
for(set<int>::iterator it = st.begin();it!=st.end();++it)
{
arr[++now].a = cnt[*it];
arr[now].b = cnt_[*it];
cnt[*it] = cnt_[*it] = 0;
}
vector<int> vt[n+1];
priority_queue<int > q;
for(int i = 1;i<=now;++i)
{
vt[arr[i].a].push_back(arr[i].b);
}
for(int i = n;i>=1;--i)
{
int sz = vt[i].size();
for(int j = 0 ;j<sz;++j) q.push(vt[i][j]);
if(!q.empty())
{
int top = q.top();
q.pop();
ans+=i;
ans_+=min(i,top);
}
}
printf("%lld %lld\n",ans,ans_);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf570H
E题的升级版 k是1e12 所以必须用dp
dp[i][j] 代表到 i 删除了 j个字符的种类数
那么 dp[i][j] = dp[i-1][j-1]+dp[i-1][j]
但是如果 aba 你删除 ba 和之前删除 ab 的情况是一样的
所以我们要把之前的情况回溯 利用一个pre 数组回溯删除
为了不爆边界 如果dp值大于k dp值等于k
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 105;
ll dp[MAX_N][MAX_N];
int pre[35];
char str[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
ll k;
scanf("%d%lld",&n,&k);
scanf("%s",str+1);
dp[0][0] = 1;
for(int i = 1;i<=n;++i)
{
dp[i][0] = 1;
for(int j = 1;j<=i;++j)
{
dp[i][j]+=dp[i-1][j-1]+dp[i-1][j];
if(pre[str[i]-'a'+1]&&j-(i-pre[str[i]-'a'+1])>=0) dp[i][j] -= dp[pre[str[i]-'a'+1]-1][j-(i-pre[str[i]-'a'+1])];
if(dp[i][j]>k) dp[i][j] = k;
}
pre[str[i]-'a'+1] = i;
}
ll ans = 0;
for(int i = 0;i<=n;++i)
{
ans+=min(k,dp[n][i])*i;
k-=dp[n][i];
if(k<=0) break;
}
if(k>0) printf("-1\n");
else printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}