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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdint.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

namespace {

int64_t gCounter = 0;

struct D {
  int64_t num;
  std::vector<int64_t> digits;
  int64_t added_digits;
  int64_t last_digit;

  int64_t AllDigits() const { return digits.size() + added_digits; }

  int64_t MulBy10(int64_t n) const {
    int64_t res = num;
    for (int64_t i = 0; i < n - (int64_t)digits.size(); ++i) {
      res *= 10;
    };
    if (n == AllDigits()) {
      res += last_digit;
    }
    return res;
  }
};

std::vector<int64_t> num2digits(int64_t n) {
  std::vector<int64_t> result;
  result.reserve(10);
  while (n > 0) {
    result.push_back(n % 10);
    n /= 10;
  }
  std::reverse(result.begin(), result.end());
  return result;
}

void print(const D &d) {
  fprintf(stderr, "%ld: ", d.num);
  for (auto i : d.digits) {
    fprintf(stderr, "%ld ", i);
  }
  fprintf(stderr, "\n[%ld] [%ld]\n\n", d.added_digits, d.last_digit);
}

int64_t pow10(int64_t n) {
  int64_t res = 1;
  while (n > 0) {
    res *= 10;
    n--;
  }
  return res;
}

int64_t compare(const D &previous, const D &current) {
  if (previous.AllDigits() < current.AllDigits()) {
    return 1;
  }

  if (previous.AllDigits() == current.AllDigits()) {
    int64_t m = std::max(previous.digits.size(), current.digits.size());

    int64_t p_val = previous.MulBy10(m);
    int64_t c_val = current.MulBy10(m);

    // DEBUG
//    fprintf(stderr, "m: %ld, p_val: [%ld], c_val: [%ld]\n", m, p_val, c_val);

    if (p_val < c_val) {
      return 1;
    }
    if (p_val > c_val) {
      return -1;
    }
    return 0;
  }

  return -1;
}

int64_t compare_vectors(const std::vector<int64_t> &a,
                        const std::vector<int64_t> &b) {
  for (int64_t i = 0; i < a.size(); ++i) {
    if (b[i] > a[i]) {
      return 1;
    }
    if (a[i] > b[i]) {
      return -1;
    }
  }
  return 0;
}

void process(D &previous, D &current) {
  int64_t comp = compare(previous, current);

  if (comp > 0) {
    // DEBUG
//    fprintf(stderr, "#1\n");
//    print(previous);
//    print(current);

    return;
  }

  if (comp == 0) {
    current.added_digits += 1;
    current.last_digit = 0;
    gCounter++;

    // DEBUG
//    fprintf(stderr, "#2\n");

    return;
  }

  //
  //
  //
  if (previous.digits.size() < current.digits.size() &&
      previous.added_digits == 1) {
    previous.num = previous.num * 10 + previous.last_digit;
    previous.digits.push_back(previous.last_digit);
    previous.added_digits = 0;
    previous.last_digit = 0;
  } else if (previous.digits.size() < current.digits.size()) {
    int64_t temp = current.digits.size() - previous.digits.size();
    previous.num *= pow10(temp);
    for (int i = 0; i < temp; ++i) {
      previous.digits.push_back(0);
    }
    previous.added_digits -= temp;
  } else if (previous.digits.size() > current.digits.size()) {
      int64_t temp = previous.digits.size() - current.digits.size();
      for (int i = 0; i < temp; ++i) {
          previous.digits.pop_back();
          previous.num /= 10;
          previous.added_digits += 1;
      }
  }

  // DEBUG
//  fprintf(stderr, "dupa\n");
//  print(previous);
//  print(current);

  comp = compare_vectors(previous.digits, current.digits);

//  fprintf(stderr, "comp: [%d]\n", comp);

  if (comp > 0) {
    int64_t temp = previous.AllDigits() - current.AllDigits();
    current.added_digits += temp;
    current.last_digit = 0;
    gCounter += temp;

    // DEBUG
//    fprintf(stderr, "#3\n");

    return;
  }

  if (comp < 0 || previous.last_digit == 9) {
    int64_t temp = previous.AllDigits() - current.AllDigits();
    current.added_digits += temp + 1;
    current.last_digit = 0;
    gCounter += temp + 1;

    // DEBUG
//    fprintf(stderr, "#4\n");

    return;
  }

  //
  //
  //
  {
    int64_t temp = previous.AllDigits() - current.AllDigits();
    current.added_digits += temp;
    current.last_digit = previous.last_digit + 1;
    gCounter += temp;

    // DEBUG
//    fprintf(stderr, "#5\n");
//    print(previous);
//    print(current);

    return;
  }
}

}  // namespace anonymous

int main() {
  int64_t N = 0;
  std::cin >> N;

  //  fprintf(stderr, "N: %ld\n", N);

  int64_t temp = 0;
  std::cin >> temp;

  D previous;

  if (temp > 10) {
    previous = {temp / 10, num2digits(temp / 10), 1, temp % 10};
  } else {
    previous = {temp, num2digits(temp), 0, 0};
  }

  // DEBUG
  //  print64_t(previous);

  for (int64_t i = 0; i < N - 1; ++i) {
    int64_t temp = 0;
    std::cin >> temp;

    D current = {temp, num2digits(temp), 0, 0};

    // DEBUG
//    print(previous);
//    print(current);

    process(previous, current);

    // DEBUG
//        print(previous);
//        print(current);

    previous = std::move(current);
  }

  std::cout << gCounter << std::endl;
  return 0;
}