E - Addition and Multiplication 2 Editorial by spheniscine


If \(x\) is viewed as a string of its base \(10\) representation without leading zeros, the operation is simply appending digit \(i\) to the end of it.

Assume array \(C\) is non-decreasing. To maximize \(x\), you want to first make it as long as possible, therefore its length is \(l = \lfloor \frac N {C_1}\rfloor\)

You can then initialize an array of \(l\) ones, and keep track of the money you have left. Greedily increase the digits from the most significant (left) to the least (right) as long as you have money to cover the cost increases.

For the case that \(C\) is not non-decreasing, you can simply set \(C'_i := \min C_{i..9}\), it’s not possible to get “stuck” on an invalid value as the cost increase would be zero at that point.

posted:
last update: