p296.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int k;
string n;

void read() { cin >> n >> k; }

vector<int> counting_sort(vector<int> a) {
    int n = a.size();
    int mx = *max_element(a.begin(), a.end());

    vector<int> cnt(mx + 1, 0);
    for(int i = 0; i < n; i++) {
        cnt[a[i]]++;
    }

    vector<int> res;
    for(int i = 0; i <= mx; i++) {
        for(int j = 0; j < cnt[i]; j++) {
            res.push_back(i);
        }
    }

    return res;
}

void solve() {
    string ans;
    vector<int> valid_states;

    char best = 0;
    for(int i = 0; i <= k; i++) {
        best = max(best, n[i]);
    }

    for(int i = 0; i <= k; i++) {
        if(n[i] == best) {
            valid_states.push_back(i);
        }
    }

    ans.push_back(best);

    int len = n.size() - k;
    for(int l = 1; l < len; l++) {
        vector<bool> can_visit(n.size(), false);
        for(int i: valid_states) {
            int ck = i - l - 1;
            for(int j = 0; j <= k - ck; j++) {
                int nxt = i + j + 1;
                if(nxt >= (int)n.size() || can_visit[nxt]) {
                    break;
                }
                can_visit[nxt] = true;
            }
        }

        char best = 0;
        for(int i = 0; i < (int)n.size(); i++) {
            if(can_visit[i]) {
                best = max(best, n[i]);
            }
        }

        vector<int> new_valid_states;
        for(int i = 0; i < (int)n.size(); i++) {
            if(n[i] == best) {
                new_valid_states.push_back(i);
            }
        }

        ans.push_back(best);
        valid_states = new_valid_states;
    }

    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
296. Sasha vs. Kate
Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



During the regular Mars's World Finals Subregional Programming Contest a boy Sasha lost N "Mars" bars of chocolate to a girl Kate. But for two years already Sasha does not hurry to pay his debt. And now Sasha and Kate decided that Sasha will give Kate P chocolate bars, where number P can be obtained from the number N by removing exactly K decimal digits. Sasha generously let Kate to choose digits to be removed. Your task is to find out how many bars Sasha will give Kate. Of course Kate will choose K digits from the number N in such a way that the resulting number P would be maximal.

Input
The first line of the input file contains two integer numbers N and K (1≤ N≤ 101000; 0≤ K≤ 999). Number K is strictly less than the number of digits in N. N will not have any leading zeros.

Output
Output the unknown P.

Example(s)
sample input
sample output
1992 2
99

sample input
sample output
1000 2
10

=================
