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

#define REP(i, n) for (int i = 0; i < (n); i++)

using LL = long long;

static char A[50], B[50];

int fix_sequence(char A[], char B[], bool f)
{
  int n = std::strlen(A);
  int m = std::strlen(B);
  int l = m;

  if (f && (n < m || (n == m && strcmp(A, B) < 0)));
  else {
    int d = n - m;

    char c = A[m]; A[m] = '\0';
    int cmp = strcmp(A, B);
    A[m] = c;

    if (cmp != 0) {
      REP (i, d) B[m++] = '0';
      if (cmp > 0) B[m++] = '0';
      B[m] = '\0';
    }
    else {
      int k = m - 1, j = n - 1;
      while (k < j && A[j] == '9') B[j] = '0', --j;
      if (k < j) {
        B[j] = A[j] + 1;
        --j;
        while (k < j) B[j] = A[j], --j;
        m = n;
        B[m] = '\0';
      }
      else {
        m = n;
        B[m++] = '0'; B[m] = '\0';
      }
    }
  }

  return m - l;
}

int main()
{
  int n;
  scanf("%i", &n);

  char *a = A, *b = B;

  LL result = 0, zeros = 0;

  scanf("%s", a);
  for (int i = 1; i < n; i++) {
    scanf("%s", b);
    result += zeros + fix_sequence(a, b, zeros == 0);

    int len = std::strlen(b);
    if (len > 15) {
      b[15] = '\0';
      zeros += len - 15;
    }

    //printf("%s\n", b);
    std::swap(a, b);
  }

  printf("%lld\n", result);
}