Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int N, K;
- string s;
- cin >> N >> K >> s;
- s = '0' + s;
- stack<char> S;
- int cnt = 0, poz = N + 1;
- for(int i = 1; i <= N; ++i) {
- while(!S.empty() && S.top() < s[i] && cnt < K) {
- ++cnt;
- S.pop();
- }
- if(cnt == K) {
- poz = i;
- break;
- }
- S.emplace(s[i]);
- }
- string sol;
- while(!S.empty()) {
- sol.push_back(S.top());
- S.pop();
- }
- reverse(sol.begin(), sol.end());
- for(int i = poz; i <= N; ++i)
- sol.push_back(s[i]);
- while(cnt < K) {
- sol.pop_back();
- ++cnt;
- }
- cout << sol;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement