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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
long long n, a, res = 0;
long long digitsNum, suff, prevNum;

long long numOfDigits(long long a) {
  long long res = 0;
  if (a == 0) {
    return 1;
  }
  while(a > 0) {
    res++;
    a /= 10;
  }
  return res;
}

bool pref(long long small, long long big) {
  while(big > small) {
    big /= 10;
  }
  return (big == small);
}

bool smallerPref(long long a, long long b) {
  vector<int>va;
  vector<int>vb;
  // cout << "(" << a << " smaller than " << b << ")";
  while(a > 0) {
    va.push_back(a % 10);
    a /= 10;
  }
  while(b > 0) {
    vb.push_back(b % 10);
    b /= 10;
  }
  while (!va.empty() && !vb.empty()) {
    if (va.back() < vb.back()) {
      return true;
    }
    if (va.back() > vb.back()) {
      return false;
    }
    va.pop_back();
    vb.pop_back();
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  bool small = true;
  for (int i = 0; i < n; i++) {
    long long prev_res = res;
    cin >> a;
    // cout << a << " ";
    // cout << "prevNum " << prevNum << " ";
    if (digitsNum < 13) {
      long long digits = numOfDigits(a);
      long long old_a = a;
      if (prevNum == a) {
        res++;
        digitsNum++;
        a *= 10;
      }
      else
      if (prevNum >= a) {
        if (pref(a, prevNum + 1)) {
          a = prevNum + 1;
          res += digitsNum - digits;
        }
        else {
          while(prevNum >= a) {
            a *= 10;
            res++;
          }
        }
      }
      prevNum = a;
      digitsNum = numOfDigits(a);
      if (digitsNum == 13) {
        prevNum = old_a;
      }
    }
    else {
      if (smallerPref(a, prevNum)) {
          digitsNum++;
      }
      res += digitsNum - numOfDigits(a);
      prevNum = a;
    }
    // cout << "num " << a << " " << res - prev_res << " digitsNum " << digitsNum << endl;
  }
  cout << res << endl;
}