#K70317. Lexicographically Smallest Subsequence

    ID: 33282 Type: Default 1000ms 256MiB

Lexicographically Smallest Subsequence

Lexicographically Smallest Subsequence

Snuke loves playing with strings. Given a string \(S\) of length \(N\) consisting of lowercase English letters, your task is to construct a new string \(T\) which is a subsequence of \(S\) using the following operations:

  • Select any character from \(S\) and append it to the end of \(T\).
  • Remove the selected character from \(S\).

The string \(T\) must satisfy the condition that it contains all distinct characters present in \(S\) and, among all such subsequences, it must be lexicographically smallest.

Note: A subsequence is a sequence that can be derived from the string \(S\) by deleting some or no characters without changing the order of the remaining characters.

For example, if \(S = \texttt{bcabcac}\) then the answer is \(\texttt{abc}\); if \(S = \texttt{cbacdcbc}\) then the answer is \(\texttt{acdb}\).

inputFormat

The input is read from stdin and consists of two lines:

  1. The first line contains an integer \(N\) — the length of the string \(S\).
  2. The second line contains the string \(S\), composed only of lowercase English letters.

outputFormat

Output the lexicographically smallest subsequence that contains all distinct characters from \(S\) to stdout.

## sample
7
bcabcac
abc

</p>