1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
typedef long long ull;
const int MAXN = 2e5 + 1;
#define bzero(x) max(0LL, (x))
#define lmaxi(x) min(17LL, x)
ull pw[20];
#define all(x) x.begin(), x.end()

int get_log(ll x) {
  int ans = 0;
  while(x) {
    ++ans;
    x /= 10;
  }

  return ans;
}

int N;
ll answer;

ll a[MAXN];
ll pom[MAXN][3];


int main() {
  pw[0] = 1;
  for(int i = 1; i < 19; ++i)
    pw[i] = pw[i - 1] * 10;

  ios_base::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);

  cin >> N;
  for(int i = 0; i < N; ++i)
    cin >> a[i],
    pom[i][0] = get_log(a[i]);

  for(int i = 1; i < N; ++i) {
    if(pom[i - 1][0] + pom[i - 1][1] < pom[i][0] + pom[i][1])
      continue;

    pom[i][1] = pom[i - 1][0] + pom[i - 1][1] - pom[i][0];
    answer += pom[i][1];

    if((a[i] / pw[bzero(pom[i][0] - pom[i - 1][0])]) > (a[i - 1] / pw[bzero(pom[i - 1][0] - pom[i][0])])) {
      pom[i][2] = (pom[i][1] == 0) ? 0 : pw[min(17LL, pom[i][1])] - 1;
    }
    else if((a[i] / pw[bzero(pom[i][0] - pom[i - 1][0])]) == (a[i - 1] / pw[bzero(pom[i - 1][0] - pom[i][0])])) {
      if(pom[i - 1][2] <= 0) {
        ++answer,
        ++pom[i][1];

        pom[i][2] = (pom[i][1] == 0) ? 0 : pw[min(17LL, pom[i][1])] - 1;
      } else
        pom[i][2] = pom[i - 1][2] - 1;
    } else {
      ++answer,
      ++pom[i][1];


      pom[i][2] = (pom[i][1] == 0) ? 0 : pw[min(17LL, pom[i][1])] - 1;
    }
  }

  cout << answer << endl;
  return 0;
}