Under construction

Still being built. Things may change

Number Theory

Time limit7sMemory limit1024 MB

Summary
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 oi=1…1⏟i timeso_i = \underbrace{1 \dots 1}_{i\text{ times}} be the number that consists of ii ones in its decimal representation.

Bobo has an integer nn. Find a sequence of possibly negative integers (x1,x2,… )(x_1, x_2, \dots) such that ∑i=1∞oi⋅xi=n\sum_{i = 1}^{\infty} o_i \cdot x_i = n and ∑i=1∞i⋅∣xi∣\sum_{i = 1}^{\infty} i \cdot |x_i| 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 nn.

Output

For each test case, output the minimum value of ∑i=1∞i⋅∣xi∣\sum_{i = 1}^{\infty} i \cdot |x_i|.

Constraints

  • 1≤n<1050001 \leq n < 10^{5000}
  • The sum of the number of decimal digits of nn over all inputs does not exceed 5000050000.

Hint

In the first test case, x1=x2=1x_1 = x_2 = 1 and x3=x4=⋯=0x_3 = x_4 = \dots = 0. The minimum value is 1×1+2×1=31 \times 1 + 2 \times 1 = 3.

In the second test case, x1=0x_1 = 0, x2=−1x_2 = -1, x3=1x_3 = 1, and x4=x5=⋯=0x_4 = x_5 = \dots = 0. The minimum value is 2×1+3×1=52 \times 1 + 3 \times 1 = 5.

Examples1

  1. Example 1

    Input
    12
    100
    998244353
    
    Expected output
    3
    5
    76