D. Not a Cheap String(Codeforces Round #805 (Div. 3))

Let ss be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is 1+2+3+1=71+2+3+1=7.

The string ww and the integer pp are given. Remove the minimal number of letters from ww so that its price becomes less than or equal to pp and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string ww is less than or equal to pp, then nothing needs to be deleted and ww must be output.

Note that when you delete a letter from ww, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.

Input

The first line of input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. The following are descriptions of tt test cases.

Each case consists of two lines.

The first of them is the string ww, it is non-empty and consists of lowercase Latin letters. Its length does not exceed 2⋅1052⋅105.

The second line contains an integer pp (1≤p≤52000001≤p≤5200000).

It is guaranteed that the sum of string lengths ww over all test cases does not exceed 2⋅1052⋅105.

Output

Output exactly tt rows, the ii-th of them should contain the answer to the ii-th set of input data. Print the longest string that is obtained from ww by deleting letters such that its price is less or equal to pp. If there are several answers, then output any of them.

Note that the empty string  — is one of the possible answers. In this case, just output an empty string.

Example

input

Copy

5
abca
2
abca
6
codeforces
1
codeforces
10
codeforces
100

output

Copy

aa
abc

cdc
codeforces
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<utility>
#include<stack>
#include<vector>
#include<math.h>
#include<map>
#include<queue>	
using namespace std;
int t;
int a[27];     //记录字符的总数
int main() {
    ios::sync_with_stdio(false);	
	cin >> t;
	while (t--)
	{
		string w;
		int p;
		string ans="";
		cin >> w>>p;
		int zong = 0;
		memset(a, 0, sizeof(a));
		for (int i = 0; i < w.size(); i++)
		{
			int q = w[i] - 'a' + 1;
			zong += q;
			a[q]++;
		}
		if (zong <= p)cout << w << endl;
		else {
			int cha = zong - p;
			for (int i = 26; i >= 1; i--)   //贪心获得最长的字符串,应该先删除值大的字符
			{                                //同时将字符的数量从a[]中减去
				if (cha <= 0)break;
				int k = cha / i;
				if (k >= a[i]) {
					cha -= a[i] * i;
					a[i] = 0;
				}
				else {
					if(cha%i!=0)
						k = k + 1;
					a[i]=a[i] - k;
					cha -= i * k;
				}
			}
			for (int i = 0; i < w.size(); i++)      
			{
				int q = w[i] - 'a' + 1;
				if (a[q]) {
					ans += w[i];
					a[q]--;
				}
			}
			cout << ans << endl;
		}
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值