Problem:
Score : 600 points

Problem Statement

A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters.
For example, arc, artistic and (an empty string) are all subsequences of artistic; abc and ci are not.

You are given a string A consisting of lowercase English letters.
Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A.
If there are more than one such string, find the lexicographically smallest one among them.

Constraints

1 \leq |A| \leq 2 \times 10^5

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a as a subsequence, but not b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

aa

Sample Input 3

frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn

Sample Output 3

aca

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <assert.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>

//#define DEBUG 1

#define int long long
#define for0(i,n) for (int i=0; i<n; i++)
#define iter(c) for(auto it=c.begin(); it!=c.end(); it++)
#define vec(x) vector< x >
#define pb push_back
#define ms(a,z) memset(a,z,sizeof(a));
#define mp make_pair
#define X first
#define Y second
#define sqr(x) 1LL*(x)*(x)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define all(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#ifdef int
#define read(x) scanf("%lld",&x);
#else
#define read(x) scanf("%d",&x);
#endif

#ifdef DEBUG
#define nl cout<<"\n";
#define pr(x) cout<<(x)<<" ";
#define prl(x) cout<<#x " = "<<x<<endl;
#define prp(x) cout<<"("<<(x).first<<" "<<(x).second<<") ";
#define printv(v) {for(int _=0; _<sz(v); _++) cout<<v[_]<<" "; cout<<"\n";}
#define printa(a,s) {for (int _=0; _<s; _++) cout<<a[_]<<" "; cout<<"\n";}
#define print2D(a,m,n) {for (int _=0; _<m; _++) {for (int __=0; __<n; __++) cout<<a[_][__]<<" "; cout<<"\n";} cout<<"\n";}
#define debug cout<<"ok at line "<<__LINE__<<endl;
#else
#define nl
#define pr(x)
#define prl(x)
#define prp(x)
#define printv(v)
#define printa(a,s)
#define print2D(a,m,n)
#define debug
#endif
#define ALPH_SIZE 3

using namespace std;

typedef long long ll;

const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;

string s;
int freq[ALPH_SIZE];
bool processed[ALPH_SIZE];
bool is_option[10000][26];
vector<int> children[10000][26];
int child[10000][26];

bool check_zero()
{
    for0(i,ALPH_SIZE) {
        if (freq[i] == 0)
            return true;
    }
    return false;
}

void process_segment(int seg, int i, int j)
{
    ms(processed,false);
    for (int k=i; k<j; k++) {
        int c = s[k]-'a';
        freq[c]--;
        if (not processed[c] and check_zero()) {
            is_option[seg][c] = true;
            for0(c2,ALPH_SIZE) {
                if (freq[c2] == 0)
                    children[seg][c].pb(c2);
            }
        }
        processed[c] = true;
    }
}


string search(int num_segs)
{    
    ms(child,-1);
    for (int seg=num_segs-2; seg>=0; seg--) {
        for0(c,ALPH_SIZE) {
            if (is_option[seg][c]) {
                bool found = false;
                iter(children[seg][c]) {
                    if (is_option[seg+1][*it]) {
                        found = true;
                        child[seg][c] = *it;
                        break;
                    }
                }
                if (!found)
                    is_option[seg][c] = false;
            }
        }
    }
    print2D(is_option,num_segs,ALPH_SIZE)
    print2D(child,num_segs,ALPH_SIZE)
    
    string ret = "";
    int cur;
    for0(c,ALPH_SIZE) {
        if (is_option[0][c]) {
            cur = c;
            break;
        }
    }
    for (int seg=0; seg<num_segs; seg++) {
        ret += 'a'+cur;
        cur = child[seg][cur];
    }
    return ret;
}

int32_t main()
{
    #ifdef DEBUG
    freopen("C.txt","r",stdin);
    //freopen("","w",stdout);
    #endif

    ms(is_option,false);
    cin >> s;
    int n = s.length();
    
    int i = 0;
    bool done = false;
    int seg = 0;
    
    while (i < n) {
        ms(freq,0);
        int j = i;
        
        done = false;
        while (j < n and not done) {
            freq[s[j]-'a']++;
            j++;
            done = not check_zero();
        }
        pr(i) pr(j) nl
        pr(s.substr(i,j-i)) nl
        
        if (done) {
            process_segment(seg,i,j);  
        }
        else {
            for0(c,ALPH_SIZE) {
                if (freq[c] == 0)
                    is_option[seg][c] = true;
            }
        }
        
        i = j;
        seg++;
    }
    
    if (done) {
        for0(c,ALPH_SIZE) 
            is_option[seg][c] = true;
        seg++;
    }
    
    print2D(is_option,seg,ALPH_SIZE)
    string ans = search(seg);    
    cout<<ans<<endl;
    return 0;
}