danhthaio21
Giới thiệu
include <bits/stdc++.h>
using namespace std;
string MaxNum(string s, int k){
vector<vector\<int>> a(10);
for(int i = 0; i < (int)s.length(); ++i){
a[s[i] - '0'].push_back(i);
}
string res = "";
int start = 0, finish = (int)s.length() - k;
while(k != 0){
if((int)s.length() - start == k){
res += s.substr(start);
break;
}
for(int i = 9; i >= 0; --i){
auto it = lower_bound(a[i].begin(), a[i].end(), start);
if(it != a[i].end()){
int idx = it - a[i].begin();
if(a[i][idx] <= finish){
res.push_back(i + 48);
start = a[i][idx] + 1;
finish++;
k--;
break;
}
}
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
int k;
cin >> s >> k;
cout << MaxNum(s, s.length() - k);
return 0;
}