Connection Status:
Competition Arena > RoundOfEleven
SRM 372 · 2007-10-17 · by connect4 · Dynamic Programming
Class Name: RoundOfEleven
Return Type: long
Method Name: maxIncome
Arg Types: (int, int)
Problem Statement

Problem Statement

The government of a neighboring country is running a game show called "Let's Make Some Money!". During the final round (the "Round of Eleven"), the contestant is given a number n and some money. The goal is to change the number into a multiple of 11. Contestants are allowed to spend 1 dollar to either increment a digit of n (to a maximum of 9) or decrement a digit of n (to a minimum of 0). Upon reaching a multiple of 11 that has not been selected this week, the contestant can either leave with the money that they have not spent or choose to keep going. For example, if n is 31 and the contestant has 4 dollars, he can change n to 11 (by decrementing the 3 twice), 22 (by decrementing 3 and incrementing 1) or 33 (by incrementing 1 twice). For each one, he would receive 2 dollars.

As ruler of Rainban, you have decided that you will allow each of your subjects to attend the show and collect all of their winnings; fortunately for you, you have so many subjects that you can win every prize on the show. This week, all subjects will get to start with the same values of n and money. Once one subject selects a multiple of 11, that multiple cannot be used by any future subject. In addition, you cannot add any digits to the number (so you cannot change 9 into 19), but you can create leading zeroes (for example, changing 19 into 09). Return the total amount of money that can be won, if your subjects play optimally. See the examples for clarification.

Constraints

  • n will be between 1 and 2^31-1, inclusive, with no leading zeroes.
  • money will be between 1 and 500, inclusive.
Examples
0)
31
4
Returns: 6

The example from the statement.

1)
31
5
Returns: 11

With 1 more dollar in the pot, you reach 33, 22 and 11 for 3 dollars each, as well as 00 and 44 for 1 dollar each.

2)
110
3
Returns: 7
3)
18392091
103
Returns: 660000066
4)
2147483647
500
Returns: 427636364064
5)
2147483645
500
Returns: 428181818610

Close to the maximum possible return.

6)
1555555555
500
Returns: 430727273158

Closer to the max return.

Submissions are judged against all 96 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class RoundOfEleven with a public method long long maxIncome(int n, int money) · 96 test cases · 2 s / 256 MB per case

Submitting as anonymous