An internal audit tool displays a ledger entry as a string of decimal digits, optionally preceded by a single minus sign marking the entry as a debit (negative balance). Every digit in the entry is drawn from 1 through 9 — this ledger format never uses the digit 0. You have exactly one correction chip bearing a single digit from 1 through 9, and you must splice it into the entry at some position. You may place the chip anywhere among the digits, but you may never place it to the left of a leading minus sign. Determine the resulting entry, written in the same signed-string format, that has the greatest possible numeric value once the chip is inserted.
Line 1: the ledger entry s — a string that optionally starts with - followed by one or more digits, each digit between 1 and 9 (never 0).
Line 2: a single integer x (1 <= x <= 9), the digit on the correction chip.
Print the resulting ledger entry, in the same signed-string format, after inserting x at the position that maximizes its numeric value.
s <= 100000s other than an optional leading - is a digit from 1 to 9Example 1
Input
73 6
Expected
763
Explanation
The leading digit 7 is not smaller than 6, but the next digit 3 is, so the chip is inserted immediately before that 3, giving 763. Checking every other placement (673 or 736) confirms 763 is the largest.
Example 2
Input
-55 2
Expected
-255
Explanation
This entry is negative, so the goal is to keep the magnitude as small as possible. Scanning the digits after the sign, the first one greater than 2 is the leading 5, so the chip is inserted immediately before it, giving -255. Placing it later (-525 or -552) would make the magnitude larger, i.e. the value more negative.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →