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 <bits/stdc++.h>

using namespace std;

int main() {
#ifdef wxh010910
  freopen("input.txt", "r", stdin);
#endif
  int n;
  scanf("%d", &n);
  vector<int> prefix, suffix;
  long long answer = 0;
  int length = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    vector<int> digits;
    while (x) {
      digits.push_back(x % 10);
      x /= 10;
    }
    reverse(digits.begin(), digits.end());
    if (length < digits.size()) {
      length = digits.size();
      prefix = digits;
      suffix.clear();
    } else {
      int type = 0;
      for (int i = 0; i < digits.size(); ++i) {
        if (i < prefix.size()) {
          if (prefix[i] != digits[i]) {
            if (prefix[i] < digits[i]) {
              type = 1;
            } else {
              type = -1;
            }
            break;
          }
        } else if (i >= length - suffix.size()) {
          if (suffix[length - i - 1] != digits[i]) {
            if (suffix[length - i - 1] < digits[i]) {
              type = 1;
            } else {
              type = -1;
            }
            break;
          }
        } else if (digits[i]) {
          type = 1;
          break;
        }
      }
      if (type == 1) {
        prefix = digits;
        suffix.clear();
        answer += length - digits.size();
      } else if (type == 0) {
        bool already = false;
        for (int i = length - 1; i >= digits.size(); --i) {
          if (i < prefix.size()) {
            if (prefix[i] == 9) {
              prefix[i] = 0;
            } else {
              ++prefix[i];
              already = true;
              break;
            }
          } else if (i >= length - suffix.size()) {
            if (suffix[length - i - 1] == 9) {
              suffix[length - i - 1] = 0;
            } else {
              ++suffix[length - i - 1];
              already = true;
              break;
            }
          } else {
            suffix.push_back(1);
            already = true;
            break;
          }
        }
        if (!already) {
          ++length;
          prefix = digits;
          suffix.clear();
        }
        answer += length - digits.size();
      } else {
        ++length;
        prefix = digits;
        suffix.clear();
        answer += length - digits.size();
      }
    }
  }
  printf("%lld\n", answer);
  return 0;
}