codeforces 1183H

题目链接:https://codeforces.com/contest/1183/problem/H

Problem:

The only difference between the easy and the hard versions is constraints.

A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols.

Characters to be deleted are not required to go successively, there can be any gaps between them.

For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string).

But the following strings are not subsequences: "aabaca", "cb" and "bcaa".

You are given a string consisting of lowercase Latin letters.

In one move you can take any subsequence of the given string and add it to the set S. The set S can't contain duplicates.

This move costs n|t|, where |tis the length of the added subsequence (i.e. the price equals to the number of the deleted characters).

Your task is to find out the minimum possible total cost to obtain a set of size or report that it is impossible to do so.

Input

The first line of the input contains two integers and k (1n100,1k1012) — the length of the string and the size of the set, correspondingly.

The second line of the input contains a string consisting of lowercase Latin letters.

Output

Print one integer — if it is impossible to obtain the set S

of size k

, print -1. Otherwise, print the minimum possible total cost to do it.

Examples

Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233

Note

In the first example we can generate S= { "asdf", "asd", "adf", "asf", "sdf" }.

The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.

 

题目大意:给一个长度为n的字符串,有没有至少k个不同的子序列,有的话输出最少的花费。每个子序列的花费为原串长度减去子序列长度。

 

思路:并不是一道复杂的题,首先如果不考虑重复的话就是 2^n 种情况,就要考虑搜索去重,dp[i][j]表示前i个字符中长度为j的子序列的数量。

 

ac代码:

#include <cstdio>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <stack>
#define MAX_N 110
using namespace std;
typedef long long ll;

int n;
ll k,ans;
ll dp[MAX_N][MAX_N];
int pre[MAX_N],last[MAX_N];
char s[MAX_N];

int main()
{
	scanf("%d%lld",&n,&k);
	scanf("%s",s+1);
	for(int i=1;i<=n;i++){
		int x=s[i]-'a';
		if(last[x])	pre[i]-last[x];
		last[x]=i;
	}
	for(int i=0;i<=n;i++)	dp[i][0]=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			dp[i][j]=dp[i-1][j-1]+dp[i][j-1];
			if(pre[i])	dp[i][j]-=dp[pre[i]-1][j];
		}
	}
	for(int i=n;i>=0;i--){
		ll cnt=min(k,dp[n][i]);
		ans+=cnt*(n-i);
		k-=cnt;
		if(k<=0)	break;
	}
	if(k>0)	ans=-1;
	printf("%lld\n",ans);

	return 0;
}

  

posted @ 2020-04-19 22:09  ジャスミン  阅读(426)  评论(0)    收藏  举报