BubbleTractor
In a galaxy far, far away, it is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. However, the Empire stroke back and the main Rebel spaceship has been trapped by a positronic tractor beam. Therefore, the Rebels need an ionic tractor disruptor to set the main spaceship free. Trouble is, a regular ionic tractor disruptor, which they have plenty of, will not do. No, no, Rebels actually need a negative ionic tractor disruptor and there is only one in the whole galaxy. The problem is that the spaceship that carries the negative ionic tractor disruptor has a broken odometer and Rebels are not sure if they have enough fuel to get to the destination. So, they want to calculate the correct number of light-years they have traveled.
The odometer is a device made of gears that rotate along the same axis and every gear has digits attached to it (a consecutive array of digits from 0 to max_digit). When all current digits on every gear are looked at as one number, the odometer displays the number of light-years traveled as an integer. However, the odometer has a defect – one (same) digit is broken on all gears.
Your task is to calculate what would the odometer reading be if no digit was broken. You are provided with the incorrect odometer reading, the broken digit and the odometer description (maximal digit per every gear). Note that the broken digit can be larger than some maximum digits. On these positions odometer works just fine. Also, if broken digit is 0, odometer does not make mistakes on leading zeros.
Second line contains 19 maximum digits from (least significant) position 0 to (most significant) position 18, with 1 ≤ max_digiti ≤ 9. Note that first maximal digit matches the right-most gear and so on.
Input:
45 3
8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 2 2 2
Output:
31 Input:
45 0
8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 2 2 2
Output:
41 Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.