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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <deque>
#include <map>
#include <vector>
using namespace std;

long long result = 0;
int c_idx = 0;
int a_idx = 1;
int numbers[2][300000];
int lengths[2] = { 0, 0 };

vector<int> as_vector(long long number) {
    vector<int> digits;
    while (number) {
      long digit = number % 10;
      number /= 10;
      digits.insert(digits.begin(), digit);
    }
    return digits;
}

void current_v_add_zeros(int k) {
  for (int i=0; i < k; i++) {
    numbers[a_idx][lengths[a_idx] + i] = 0;
  }
  lengths[a_idx] += k;
}

void current_v_assign_add_zeros(int k) {
  result += k;
  current_v_add_zeros(k);
}

void print_v(int idx) {
  cout << "numbers[" << idx << "]=";
  for(int i=0; i< lengths[idx]; i++) {
    cout << numbers[idx][i] << ",";
  }
  cout << endl;
}

void compute_next() {
  if (lengths[a_idx] > lengths[c_idx]) {
    current_v_assign_add_zeros(0);
    return;
  }

  if (lengths[a_idx] == lengths[c_idx]) {
    int i = 0;
    while (i < lengths[c_idx]) {
      if (numbers[a_idx][i] > numbers[c_idx][i]) {
        current_v_assign_add_zeros(0);
        return;
      } else if (numbers[a_idx][i] < numbers[c_idx][i]) {
        current_v_assign_add_zeros(1);
        return;
      } else {
        i++;
      }
    }
    current_v_assign_add_zeros(1);
    return;
  }

  int i = 0;
  while (i < lengths[a_idx]) {
    if (numbers[c_idx][i] < numbers[a_idx][i]) {
      int k = lengths[c_idx] - lengths[a_idx];
      current_v_assign_add_zeros(k);
      return;
    } else if (numbers[c_idx][i] > numbers[a_idx][i]) {
      int k = lengths[c_idx] - lengths[a_idx] + 1;
      current_v_assign_add_zeros(k);
      return;
    } else {
      i++;
    }
  }

  bool only_nines = true;
  for (int j = i; j < lengths[c_idx] ; j++) {
    if (numbers[c_idx][j] != 9) {
      only_nines = false;
    }
  }
  if (only_nines) {
    int k = lengths[c_idx] - lengths[a_idx] + 1;
    current_v_assign_add_zeros(k);
    return;
  } else {
    bool bumped = false;
    for(int j = lengths[c_idx] - 1; j>=i; j--) {
      int d = numbers[c_idx][j];
      if (bumped) {
        numbers[a_idx][j] = d; 
  //      a_v.insert(a_v.begin() + i, d);
      } else if (d == 9) {
        numbers[a_idx][j] = 0; 
       // a_v.insert(a_v.begin() + i, 0);
      } else {
        numbers[a_idx][j] = d + 1;
       // a_v.insert(a_v.begin() + i, d + 1);
        bumped = true;        
      }
    }
    lengths[a_idx] = lengths[c_idx];
    result += lengths[c_idx] - i;
    return;
  }
}

void read(int idx) {
  long long x;
  scanf("%lld\n", &x);
  vector<int> tmp = as_vector(x);
  for (int i=0; i< tmp.size(); i++) {
    numbers[idx][i] = tmp[i];
  }  
  lengths[idx] = tmp.size();
}

int main() {
  long n;
  scanf("%ld\n", &n);
  read(c_idx);
  for (int i=1; i<n; i++) {
    read(a_idx);
    //cout << "current: ";
   // print_v(c_idx);
   // cout << "a: ";
    //print_v(a_idx);
    compute_next();
    int tmp_idx = a_idx;
    a_idx = c_idx;
    c_idx = tmp_idx;
  //  cout << result << endl;
  }
  cout << result << endl;
  return 0;
}