Under construction

Still being built. Things may change

Inversions in Lexicographical Order

Time limit2sMemory limit512 MB

Summary
Given n with up to 250000 digits, count the inversions in the permutation formed by sorting 1 to n as strings.
Level

Hard9 of 10

Topics
Combinatorics, Dynamic programming, String
Solved
No attempts yet

Problem

An inversion in permutation p=⟨p1,p2,…,pn⟩p = \langle p_1, p_2, \ldots, p_n \rangle is a pair of integers (i,j)(i, j) such that i<ji < j and pi>pjp_i > p_j.

Consider a lexicographical order on positive integers. Under this ordering, integers are compared as strings of digits. For example, 628 comes before 7, 239 comes before 271, and 42 comes before 427.

You are given a positive integer nn. Sort all integers from 1 to nn, inclusive, in lexicographical order. This gives a permutation pp of length nn, where p1p_1 is the smallest integer between 1 and nn in this order (which is always 1), p2p_2 is the second smallest, and so on.

How many inversions does pp contain?

Input

The only line of the input contains a single positive integer nn without leading zeroes. The value of nn is between 1 and 10250 000−110^{250\,000} - 1, inclusive, so nn has at most 250000 decimal digits.

Output

Output a single integer without leading zeroes: the number of inversions in pp.

Hint

In the first test case, p=⟨1,10,11,2,3,4,5,6,7,8,9⟩p = \langle 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9 \rangle contains 16 inversions.

Examples2

  1. Example 1

    Input
    11
    
    Expected output
    16
    
  2. Example 2

    Input
    427
    
    Expected output
    26576