Number Theory
Time limit7sMemory limit1024 MB
Given a huge integer n, write it as an integer combination of repunits o_i (the number with i ones) so that the sum of i times |x_i| is minimized.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Dynamic programming
- Solved
- No attempts yet
Problem
Let be the number that consists of ones in its decimal representation.
Bobo has an integer . Find a sequence of possibly negative integers such that and is minimized.
Input
The input consists of several test cases, and it ends at end-of-file. Each test case has one line containing an integer .
Output
For each test case, output the minimum value of .
Constraints
- The sum of the number of decimal digits of over all inputs does not exceed .
Hint
In the first test case, and . The minimum value is .
In the second test case, , , , and . The minimum value is .