#P11100. [ROI 2022] 交换 (Day 2)

[ROI 2022] 交换 (Day 2)

Background

Translated from ROI 2022 D2T1

You are given an nn-digit decimal number xx。You may swap two adjacent digits any number of times, and each swap costs yy。If after kk such swaps you obtain the number xnewx_{new}, then the profit equals xnewk×yx_{new} - k \times y

Definition: if by swapping you obtain a number xnewx_{new} from xx, and during this process you achieve the maximum possible profit, then xnewx_{new} is called an optimal number。

For example, when y=114y = 114, you can move the digit 77 in 33273327 to the front by swapping 33 times, obtaining the maximum profit 73323×114=69907332 - 3 \times 114 = 6990。In this case, 73327332 is called an optimal number。

Problem Description

As is well known, such optimal numbers may be more than one (for the same xx, all its optimal numbers have the same profit, but their values are different)。Given xx and yy, you need to determine the maximum value among all optimal numbers。

Input Format

The first line contains an integer xx consisting of nn decimal digits (1n1051 \le n \le 10^5)。The number xx may have leading zeros。

The second line contains an integer yy, the cost of one swap (1y10161 \le y \le 10^{16})。

Output Format

Output an integer xnewx_{new}, the maximum among all optimal numbers。The length of xnewx_{new} is nn, and it may contain leading zeros。

170
15
710
170
600
170
314599
17713
931459
001
1000
001
3327
114
7332

Hint

Subtask Score nn \le yy \le Special property
11 2727 99 10810^8
22 1313 2020
33 1919 10510^5 11
44 2525 10810^8 xx consists of 1 or 2
55 88
66 101610^{16}

Translated by ChatGPT 5