-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNot a cheap string_CF.cpp
More file actions
49 lines (46 loc) · 1.08 KB
/
Not a cheap string_CF.cpp
File metadata and controls
49 lines (46 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
string s;
cin>>s;
int p;
cin>>p;
int n=s.size();
int value=0;
for(int i=0;i<n;i++)
value+=(s[i]-96);
if(value<=p)
cout<<s<<endl;
else{
string temp=s;
sort(temp.begin(),temp.end());
int i=n-1;
string res="";
while(i>=0){
value-=(temp[i]-96);
if(value<=p)
break;
i--;
}
if(i<0)
cout<<res<<endl;
else{
map<char,int>mp;
for(int j=0;j<i;j++){
mp[temp[j]]++;
}
for(int i=0;i<n;i++){
if(mp.find(s[i])!=mp.end() && mp[s[i]]!=0){
res+=s[i];
mp[s[i]]--;
}
}
cout<<res<<endl;
}
}
}
}