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

#define REP(a, b) for(int (a)=0; (a)<(b); (a)++)

#define MAX_N 200100

typedef struct {
  int len;
  char a[MAX_N];
} num_t;

num_t num1, num2, num3;

void parse_num(char* s, num_t* num) {
  num->len = strlen(s);
  int n = num->len;
  REP(i, n) num->a[n-i-1] = s[i]-'0';
}

void print_num(num_t* num) {
  int n = num->len;
  REP(i, n) printf("%d", num->a[n-i-1]);
  printf("\n");
}

int aux_cmp(int a, int b) {
  return (a != b) ? a-b : 0;
}

int num_cmp() {
  int aux = aux_cmp(num1.len, num2.len);
  if (aux != 0) return aux;
  int n = num1.len;
  REP(i, n) {
    aux = aux_cmp(num1.a[n-i-1], num2.a[n-i-1]);
    if (aux != 0) return aux;
  }
  return 0;
}

int add_one(num_t* num) {
  int i = 0;
  int n = num->len;
  int added = 0;

  while (i < n && num->a[i] == 9) {
    num->a[i] = 0;
    i++;
  }

  if (i == n) {
    n++;
    num->len++;
    num->a[i] = 0;
    added = 1;
  }

  num->a[i]++;

  return added;
}

void append_zeros(num_t* num, int sz) {
  int n = num->len;
  REP(i, n) num->a[n-1-i+sz] = num->a[n-1-i];
  REP(i, sz) num->a[i] = 0;
  num->len = n + sz;
}

int pref_cmp(int n) {
  int n1 = num1.len;
  int n2 = num2.len;
  REP(i, n) {
    int aux = aux_cmp(num1.a[n1-i-1], num2.a[n2-i-1]);
    if (aux != 0) return aux;
  }
  return 0;
}

bool all9(num_t* num, int dlen) {
  REP(i, dlen) {
    if (num->a[i] != 9) {
      return false;
    }
  }
  return true;
}

void crop_num(num_t* num, int dlen) {
  REP(i, dlen) num3.a[i] = num->a[i];
  num3.len = dlen;
}

void append_num(num_t* num1, num_t* num2) {
  int n1 = num1->len;
  int n2 = num2->len;
  REP(i, n1) num1->a[n1+n2-i-1] = num1->a[n1-i-1];
  REP(i, n2) num1->a[i] = num2->a[i];
  num1->len = n1+n2;
}

int complement() {
  if (num_cmp() < 0) return 0;
  int dlen = num1.len - num2.len;
  if (dlen == 0) {
    append_zeros(&num2, 1);
    return 1;
  }
  int cmp = pref_cmp(num1.len-dlen);
  if (cmp < 0) {
    append_zeros(&num2, dlen);
    return dlen;
  }
  if (cmp > 0) {
    append_zeros(&num2, dlen+1);
    return dlen+1;
  }
  if (all9(&num1, dlen)) {
    append_zeros(&num2, dlen+1);
    return dlen+1;
  }
  crop_num(&num1, dlen);
  add_one(&num3);
  append_num(&num2, &num3);
  return dlen;
}

void num_cpy() {
  num1.len = num2.len; 
  REP(i, num1.len) num1.a[i] = num2.a[i];
}

int main() {
  char s[11];
  int n;
  long long res = 0;

  scanf("%d %s", &n, s);

  parse_num(s, &num1);

  REP(ii, n-1) {
    scanf("%s", s);
    parse_num(s, &num2);
    res += complement();
    num_cpy();
  }

  printf("%lld\n", res);

  return 0;
}