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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct num{
  vector <int> front;
  int z;
  int c;
  num(int a, int b, int x){
    while(a){
      front.push_back(a%10);
      a /= 10;
    }
    reverse(front.begin(), front.end());
    z = b;
    c = x;
  }

  int c_size(){
    int ans=0;
    int cp = c;
    while(cp){
      cp /= 10;
      ans++;
    }
    return ans;
  }
};

int n;
int t[200005];

int cmp(num a, num b){
  if(b.front.size() > a.front.size() + a.z + a.c_size())
    return 1;

  vector <int> a_c;
  int cp = a.c;
  while(cp)
  {
    a_c.push_back(cp%10);
    cp /= 10;
  }
  while( b.front.size() > a.front.size() )
  {
    if(a.z > 0)
    {
      a.front.push_back(0);
      a.z--;
    }
    else
    {
      a.front.push_back( a_c[ a_c.size()-1 ] );
      a_c.pop_back();
    }
  }

  for(int i=0; i<b.front.size(); i++){
      if(b.front[i] > a.front[i])
        return 1;
      else if(b.front[i] < a.front[i])
        return -1;
  }
   return 0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  cin>>n;
  for(int i=0; i<n; i++){
    cin>>t[i];
  }

  long long ans=0;

  num prev = num(t[0], 0, 0);

  for(int i=1; i<n; i++){
    num x = num(t[i], 0, 0);
    int cmpr = cmp(prev,x);
    long long ile_zer = (long long)max((int)(prev.front.size() + prev.z + prev.c_size() - x.front.size()), 0);
    if( cmpr == 1 )
    {
          ans += ile_zer;
          prev = x;
          prev.z = ile_zer;
          prev.c = 0;
    }
    else if( cmpr == -1 )
    {
          ans += ile_zer+1;
          prev = x;
          prev.z = ile_zer+1;
          prev.c = 0;
    }
    else
    {
       int limit=ile_zer;//prev.c_size()+prev.z;
       int cpj = prev.c+1;
       int csize = 0;
       while(cpj)
       {
             csize += 1;
             cpj /= 10;
       }
       if( csize > limit )
       {
             prev = x;
             prev.z = ile_zer+1;
             prev.c = 0;//ile_zer+1 - x.front.size();
             ans += ile_zer+1;
       }
       else
       {
             //prev.front = x.front;
            if( csize > prev.c_size() )
            {
                if( prev.z > 0 )
                {
                    prev.z--;
                    prev.c++;
                    ans += ile_zer;
                }
                else
                {
                    int lim_licz = prev.c_size()+1;
                    int i=prev.front.size()-1;
                    while( prev.front[i] == 9 && lim_licz <= limit )
                    {
                        prev.front[i] = 0;
                        lim_licz++;
                        i--;
                    }
                    if( lim_licz <= limit )
                    {
                        prev.front[i]++;
                        prev.z = prev.c_size();
                        prev.c = 0;
                        ans += ile_zer;
                    }
                    else
                    {
                        //nie da size
                        prev = x;
                        prev.z = ile_zer+1;
                        prev.c = 0;//ile_zer+1 - x.front.size();
                        ans += ile_zer+1;
                    }
                }
            }
            else
            {
                prev.c++;
                ans += ile_zer;
            }
       }
    }
  }

  cout<<ans;

  return 0;
}