D - Sum of Sum of Digits 解説 /

実行時間制限: 5 sec / メモリ制限: 1024 MB

配点 : 800

問題文

正整数 x に対し,その各桁の和を f(x) と表すことにします.例えば f(153) = 1 + 5 + 3 = 9f(2023) = 2 + 0 + 2 + 3 = 7f(1) = 1 です.

正整数列 A = (A_1, \ldots, A_N) が与えられます.x を非負整数とするとき,\sum_{i=1}^N f(A_i + x) としてありうる最小値を求めてください.

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i < 10^9

入力

入力は以下の形式で標準入力から与えられます.

N
A_1 \ldots A_N

出力

x を非負整数とするとき,\sum_{i=1}^N f(A_i + x) としてありうる最小値を出力してください.


入力例 1

4
4 13 8 6

出力例 1

14

例えば x = 7 とすると,\sum_{i=1}^N f(A_i+x) = f(11) + f(20) + f(15) + f(13) = 14 となります.


入力例 2

4
123 45 678 90

出力例 2

34

例えば x = 22 とすると,\sum_{i=1}^N f(A_i+x) = f(145) + f(67) + f(700) + f(112) = 34 となります.


入力例 3

3
1 10 100

出力例 3

3

例えば x = 0 とすると,\sum_{i=1}^N f(A_i+x) = f(1) + f(10) + f(100) = 3 となります.


入力例 4

1
153153153

出力例 4

1

例えば x = 9999846846847 とすると,\sum_{i=1}^N f(A_i+x) = f(10000000000000) = 1 となります.

Score : 800 points

Problem Statement

For a positive integer x, let f(x) denote the sum of its digits. For instance, we have f(153) = 1 + 5 + 3 = 9, f(2023) = 2 + 0 + 2 + 3 = 7, and f(1) = 1.

You are given a sequence of positive integers A = (A_1, \ldots, A_N). Find the minimum possible value of \sum_{i=1}^N f(A_i + x) where x is a non-negative integer.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i < 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum possible value of \sum_{i=1}^N f(A_i + x) where x is a non-negative integer.


Sample Input 1

4
4 13 8 6

Sample Output 1

14

For instance, x = 7 makes \sum_{i=1}^N f(A_i+x) = f(11) + f(20) + f(15) + f(13) = 14.


Sample Input 2

4
123 45 678 90

Sample Output 2

34

For instance, x = 22 makes \sum_{i=1}^N f(A_i+x) = f(145) + f(67) + f(700) + f(112) = 34.


Sample Input 3

3
1 10 100

Sample Output 3

3

For instance, x = 0 makes \sum_{i=1}^N f(A_i+x) = f(1) + f(10) + f(100) = 3.


Sample Input 4

1
153153153

Sample Output 4

1

For instance, x = 9999846846847 makes \sum_{i=1}^N f(A_i+x) = f(10000000000000) = 1.