Password
Limits: 2 sec., 256 MiB
Zenyk wants to know the password to Marichka’s Facebook account.
There are \(n\) digits (1 to 9) lying on the floor. Zenyk knows that he should form \(k\) integers using those digits, in such a way the greatest integer is minimum possible — that would be the password. Of course, you have to use all \(n\) digits.
Please help Zenyk to find out the password.
Input
The first line contains two integers \(n\) and \(k\). The second line contains string of \(n\) digits 1 to 9.
Output
Print a single integer — the password.
Constraints
\(1 \le N \le 10^5\),
\(1 \le K \le N\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4 4412377 | 34 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Password - Editorial
The length of the largest number is \(\left \lceil \frac{n}{k} \right \rceil\).
We build the largest number from left to right, keeping \(c\), the number of numbers that can still be the largest. Initially \(c\) is the number of numbers of length \(\left \lceil \frac{n}{k} \right \rceil\): \(c = n \% k\) if this value is non-zero, and \(c=k\) otherwise.
\(\left \lceil \frac{n}{k} \right \rceil\) times we need to append the smallest possible digit to the current largest number. We have \(c\) numbers that are the largest so far. Take the \(c\) smallest unused digits and append them to these largest numbers. Let the largest digit we just used be \(d\), used \(c'\) times. \(c'\) becomes the new value of \(c\). We print the digit \(d\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|