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
#include<cstdio>

int N = 200005;
long long M = 1000000000000ll;
bool debug = false;

int n;
long long * t;

void input() {
  scanf("%d", &n);
  t = new long long[n];
  for (int i = 0; i < n; i++) {
    scanf("%lld", t + i);
  }
}

long long last_digit(long long n) {
  return n % 10;
}

bool is_first_prefix_of_second(long long n, long long m) {
  if (n > m) {
    return false;
  }
  while(m > n) {
    m /= 10;
  }
  return n == m;
}

long long increase_not_prefix(long long n, long long m) {
  while(n < m) {
    n *= 10;
  }
  return n;
}

long long increase_prefix(long long n, long long m) {
  if (n == m) {
    return n * 10;
  }
  long long test = m + 1;
  if (is_first_prefix_of_second(n, test)) {
    return test;
  }
  return increase_not_prefix(n, m);
}

long long increased(long long n, long long m) {
  if (n > m) {
    return n;
  }
  if (is_first_prefix_of_second(n, m)) {
    return increase_prefix(n, m);
  } else {
    return increase_not_prefix(n, m);
  }
}

int length(long long n) {
  int length = 0;
  do {
    length++;
    n /= 10;
  } while (n > 0);
  return length;
}

void update_t() {
  long long res = 0;
  for (int i = 1; i < n; i++) {
    long long new_t = increased(t[i], t[i-1]);
    //printf("%lld %lld %d\n", new_t, t[i], length(new_t) - length(t[i]));
    res += (length(new_t) - length(t[i]));
    t[i] = new_t;
  }
  printf("%lld\n", res);
}

long long expand_lo(long long n) {
  while (n < M) {
    n *= 10;
  }
  return n;
}

long long expand_hi(long long n) {
  while (n < M) {
    n = (n * 10) + 9;
  }
  return n;
}

long long update_t_big(int from, long long res) {
  int len = length(t[from - 1]);
  for (int i = from; i < n; i++) {
    long long prev = expand_lo(t[i-1]);
    long long x = expand_hi(t[i]);
    int curr_length = length(t[i]);
    if (x < prev) {
      len++;
    } else {
      if (expand_lo(t[i]) < prev) {
        /*long long next = 1 + t[i-1];
        if (is_first_prefix_of_second(t[i], expand_lo(next))) {
          t[i] = next;
        } else {
          len++;
        }*/
        t[i] = t[i-1];
      }
    }
    res += (len - curr_length);
    if (debug) {
      printf("%lld", t[i]);
      for (int j = 0; j < (len - length(t[i])); j++) {
        printf("0");
      }
      printf(" %lld %d %d", t[i], len, len - length(t[i]));
      printf("\n");
    }
  }
  return res;
}

void update_t_2() {
  long long res = 0;
  if (debug) {
    printf("%lld\n", t[0]);
  }
  for (int i = 1; i < n; i++) {
    long long new_t = increased(t[i], t[i-1]);
    if (debug) {
      printf("%lld\n", new_t);
    }
    res += (length(new_t) - length(t[i]));
    t[i] = new_t;
    if (t[i] >= M) {
      res = update_t_big(i + 1, res);
      break;
    }
  }
  printf("%lld\n", res);
}

int main() {
  input();
  update_t_2();
  //printf("\n");
  //for (int i = 0; i < n; i++) {
  //  printf("%lld\n", t[i]);
  //}
  return 0;
}