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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static inline int stringPartCompare(const string* s1, const string* s2, unsigned long length) {
  int diff;
  for (unsigned long i = 0; i < length; i++) {
    diff = s1->at(i) - s2->at(i);
    if (diff == 0) continue;
    return diff;
  }
  return 0;
}

static inline void appendZeros(string* s, unsigned long length) {
  s->resize(s->size() + length, '0');
}

static inline int increment(string* s, unsigned long startPos) {
  for (long long i = s->size() - 1; i >= startPos; i--) {
    if (s->at(i) < '9') {
      s->at(i)++;
      return 0;
    }
    s->at(i) = '0';
  }
  appendZeros(s, 1);
  return 1;
}

int main(void) {
  unsigned long long result = 0;
  int n;
  string s1, s2;
  string *prev = &s1, *current = &s2;
  long long sizeDiff;
  int commonPartDiff;

  cin >> n;
  cin >> *current;
  for (int i = 1; i < n; i++) {
    swap(current, prev);
    cin >> *current;
    sizeDiff = prev->size() - current->size();
    if (sizeDiff > 0) {
      commonPartDiff = stringPartCompare(prev, current, current->size());
      if (commonPartDiff == 0) {
        swap(current, prev);
        result += sizeDiff + increment(current, prev->size());
      } else {
        if (commonPartDiff > 0) sizeDiff++;
        appendZeros(current, sizeDiff);
        result += sizeDiff;
      }
    } else if (sizeDiff == 0) {
      if (stringPartCompare(prev, current, current->size()) >= 0) {
        appendZeros(current, 1);
        result++;
      }
    }
  }

  cout << result;

  return 0;
}