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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// debugging stuff {{{
#ifdef DEBUG
  #define bDebug 1
  #define bDebug2 0
#else
  #define bDebug 0
  #define bDebug2 0
#endif
#define deb(a) #a << "=" << (a) << " "
#ifndef HOME
  #define assert(a) {}
#endif

template<class T> ostream& operator<<(ostream& os, vector<T> v) //{{{
{
  for(int i=0; i<v.size(); i++)
    os << v[i] << " ";
  os << endl;
  return os;
}  //}}}
// }}} end of debugging stuff

#ifdef MIKROSOFT
  #define typeof decltype
#else
  //c++11
  #define typeof __typeof__
#endif

#define array(a, type, count) type *a = (type*)calloc(sizeof(type), count)
#define eps 1e-9
#define eq(a, b) ( (a) > (b) - eps && (a) < (b) + eps )
#define eraseAll(v) v.erase(v.begin(), v.end())
#define fi first
#define rep(i,n) for(long i=0; i<(n); i++)
#define rep2(i,a,b) for(long i=(a); i<=(b); i++)
#define rep2d(i,a,b) for(long i=(a); i>=(b); i--)
#define zeroMem(a) memset(a, 0, sizeof(a))
#define zeroMem2(a, n) memset(a, 0, sizeof(*a) * n)
#define fore(it,L) for(typeof(L.begin()) it=L.begin(); it!=L.end(); it++)
#define eraseAll(v) v.erase(v.begin(), v.end())
#define se second
#define setMin(a,b) { typeof(a) rv = (b); if (rv < a) a = rv; }
#define setMinP(a,b) { typeof(a) rv = (b); \
                       if (rv >= 0 && (a < 0 || rv < a)) a = rv; }
#define setMax(a,b) { typeof(a) rv = (b); if (rv > a) a = rv; } 
#define swap(a,b) { typeof(a) swapVar = a; a = b; b = swapVar; }
#define Int long long

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(suppress : 4996)

//*********************** SOLUTION    ******************************
#define M 210000
char buf[300000];
string zera = string("");
long long x = 0;
bool ext = false;

string strPlus1(string &a) {
  string::iterator it = a.end();
  it--;
  int carry = 1;
  while (true) {
    if (carry == 1 && *it == '9') {
      *it = '0';
      carry = 1;
    } else {
      if (carry == 1)
        *it = (*it) + 1;
      break;
    }
    if (carry == 1 && it == a.begin())
      a.insert(a.begin(), '1');
    if (it == a.begin())
      break;
    it--;
  }
  return a;
}

void ustawStringBwA(string &astr, string &bstr) {
  // Kopiujemy poczatek b do a.
  rep(i, bstr.length()) astr[i] = bstr[i];
  // Wypelniamy a zerami.
  rep2(i, bstr.length(), 15) {
    if (i >= astr.length())
      break;
    astr[i] = '0';
  }
  // I koncowke a tez wypelniamy zerami.
  rep2(i, astr.length() - 15, astr.length() - 1) {
    if (i >= bstr.length())
      astr[i] = '0';
  }

}

string f(string &astr, string &bstr) {
  long lena = astr.length();
  long lenb = bstr.length();
  if (lena < lenb)
    return bstr;
  if (lena == lenb) {
    if (astr < bstr) {
      return bstr;
    } else {
      x += 1;
      bstr.push_back('0');
      return bstr;
    }
  }
  if (lena > lenb) {
    int cmpPoczatek = 0;
    rep(i, lenb) {
      if (astr[i] < bstr[i]) {
        cmpPoczatek = -1;
        break;
      }
      if (astr[i] > bstr[i]) {
        cmpPoczatek = 1;
        break;
      }
    }
    if (cmpPoczatek != 0) {
      x += astr.length() - bstr.length();
      ustawStringBwA(astr, bstr);
      if (cmpPoczatek == 1) {
        astr.push_back('0');
        x += 1;
      }
      return astr;
    }
    else { // a2 == b
      // Jezeli a to same dziewiatki
      bool same9 = true;
      for (string::iterator it = astr.begin(); it != astr.end(); it++)
        if (*it != '9')
          same9 = false;
      if (same9) {
        x += astr.length() - bstr.length() + 1;
        ustawStringBwA(astr, bstr);
        astr.push_back('0');
        return astr;
      } else {
        // Czy po wspolnej czesci sa same dziewiatki?
        bool same9poWspolnej = true;
        for(string::iterator it = astr.begin() + lenb; it != astr.end(); it++)
          if (*it != '9')
            same9poWspolnej = false;
        //cerr << "same9 " << deb(astr) << deb(lenb) << deb(astr.substr(lenb)) << deb(same9poWspolnej) << endl;
        if (same9poWspolnej) {
          x += astr.length() - bstr.length();
          ustawStringBwA(astr, bstr);
          astr.push_back('0');
          x += 1;
          return astr;
        } else {
          // W miare normalny przypadek, ze a+1 potrafimy osiagnac.
          x += lena - lenb;
          return strPlus1(astr);
        }
      }
    }
  }
}

void test() {
  string s = string("");
  s = string("9"); strPlus1(s); assert(s == "10");
  s = string("139"); strPlus1(s); assert(s == "140");
  s = string("999999"); strPlus1(s); assert(s == "1000000");
  s = string("123"); strPlus1(s); assert(s == "124");
}

int main (int argc, char **args)
{
  rep(i, M) zera.push_back('0');
  test();
  #ifdef MIKROSOFT
  if (argc > 1) {
    freopen("kon/testy/5.in", "r", stdin);
  }
  #endif
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  long n;
  cin >> n;
  if (n < 0) {
    ext = true;
    n = -n;
  }
  string a, b;
  cin >> a;
  rep(i, n - 1) {
    cin >> b;
    a = f(a, b);
    if (ext)
      cout << a << endl;
    if (bDebug) {
      cerr << deb(i) << deb(a) << deb(x) << endl;
    }
  }
  cout << x << endl;
}