#P8801. Maximize Number by Digit Operations

    ID: 21965 Type: Default 1000ms 256MiB

Maximize Number by Digit Operations

Maximize Number by Digit Operations

You are given a positive integer \(N\). You are allowed to perform two types of operations on any digit (each digit is considered independently) any number of times:

  • Operation 1: Increase the digit by 1. If the digit is \(9\), adding 1 makes it \(0\).
  • Operation 2: Decrease the digit by 1. If the digit is \(0\), subtracting 1 makes it \(9\).

You can perform Operation 1 at most \(A\) times in total and Operation 2 at most \(B\) times in total. Your goal is to maximize the integer \(N\) by applying these operations optimally across its digits.

Notes on Operations:

For any digit \(d\) (where \(0 \le d \le 9\)) the effect of the operations is modulo 10. In particular, if you want to change \(d\) to \(9\) you have two direct ways:

  • Using Operation 1 (increments): The number of increments required is \[ \text{cost}_{inc} = (9-d) \quad \text{(if } d \le 9 \text{)}. \]
  • Using Operation 2 (decrements): The number of decrements required is \[ \text{cost}_{dec} = \begin{cases} 0, & d = 9,\\[6pt] 1, & d = 0,\\[6pt] d+1, & 1 \le d \le 8. \end{cases} \]

You may fully convert a digit \(d\) into \(9\) if you have enough remaining operations in either type. If both types are possible, it is optimal to choose the method that uses the fewer number of operations. If neither type can be fully used on a digit, you are allowed to perform a partial operation using Operation 1 (increments) without causing a wrap‐around. (Notice that a partial use of Operation 2 will simply decrease the digit and is not beneficial for maximization, except for the special case when \(d=0\) where a full use requires only 1 decrement.)

Task: Given \(N\), \(A\) and \(B\), compute the maximum possible integer obtainable by applying the allowed operations optimally.

inputFormat

The input consists of a single line containing three space‐separated integers:

  • \(N\) \( (1 \le N \le 10^{1000})\): the positive integer.
  • \(A\) \((0 \le A \le 10^9)\): the maximum number of times you can use Operation 1 (increments).
  • \(B\) \((0 \le B \le 10^9)\): the maximum number of times you can use Operation 2 (decrements).

outputFormat

Output a single line containing the maximum integer you can obtain after performing the operations optimally.

sample

123 2 0
323