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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <vector>
#include <algorithm>
#include <string>
#include <cstdio>

using namespace std;

#define ll long long
#define PII pair<int, int>
#define VP vector< PII >

VP* compact(VP* ps) {
  VP* res = new VP;
  PII* prev = 0;
  PII* cur = 0;
  for(VP::iterator it = ps->begin();it != ps->end();it++) {
    if(prev != 0) {
      if(it->first == prev->first) {
        cur->second += it->second;
      } else {
        res->push_back(*cur);
        cur = &(*it);
      }
    } else {
      cur = &(*it);
    }
    prev = &(*it);
  }
  if(cur != 0) {
    res->push_back(*cur);
  }
  delete ps;
  return res;
}

struct Numero {
  VP* ps;

  Numero(): ps(new VP) {}

  Numero(int x): ps(new VP) {
    string s = to_string(x);
    char prev = 'x';
    int cnt = 0;
    for(int i=0;i < s.length();i++) {
      char c = s[i];
      if(c != prev) {
        if(cnt > 0) {
          ps->push_back(make_pair(prev-'0', cnt));
        }
        prev = c;
        cnt = 1;
      } else {
        cnt++;
      }
    }
    if(cnt > 0) {
      ps->push_back(make_pair(prev-'0', cnt));
    }
  }

  void append(PII p) {
    if(p.second > 0) {
      ps->push_back(p);
    }
  }

  int length() {
    int res = 0;
    for(VP::iterator it = ps->begin();it != ps->end();it++) {
      res += it->second;
    }
    return res;
  }

  void inc() {
    if(ps->empty()) {
      ps->push_back(make_pair(0,1));
    } else if((ps->size() == 1) && (ps->back().first == 9)) {
      ps->back().first = 0;
      ps->back().second++;
    } else {
      VP* res = new VP;
      int s = ps->size() - 1;
      bool copy = false;
      while(s >= 0) {
        PII& p = ps->at(s);
        if(copy) {
          res->push_back(p);
        } else {
          if(p.first < 9) {
            if(p.second == 1) {
              p.first++;
              res->push_back(p);
            } else {
              res->push_back(make_pair(p.first+1,1));
              res->push_back(make_pair(p.first,p.second-1));
            }
            copy = true;
          } else {
            p.first = 0;
            res->push_back(p);
          }
        }
        s--;
      }
      reverse(res->begin(), res->end());
      delete ps;
      ps = compact(res);
    }
  }

};

struct PairIterator;

struct Duo {
  Numero* prefix;
  Numero* suffix;

  Duo(Numero* prefix): prefix(prefix), suffix(new Numero()) {}

  int length() {
    return prefix->length() + suffix->length();
  }

  Numero* subNum(int from);

};

struct PairIterator {
  Duo* duo;
  bool itPre;
  bool hasNext;
  int index;

  PairIterator(Duo* duo): duo(duo), itPre(true),
      hasNext(!duo->prefix->ps->empty() || !duo->suffix->ps->empty()), index(0) {}
  
  PII next() {
    Numero* num = itPre?duo->prefix:duo->suffix;
    PII res = num->ps->at(index);
    index++;
    if(index >= num->ps->size()) {
      index = 0;
      if(itPre) {
        itPre = false;
        hasNext = !duo->suffix->ps->empty();
      } else {
        hasNext = false;
      }
    }
    return res;
  }
};

Numero* Duo::subNum(int from) {
  Numero* res = new Numero();
  PairIterator pit(this);
  int a = 0;
  while(pit.hasNext) {
    PII p = pit.next();
    int df = a + p.second - from;
    if(df <= 0){
      a += p.second;
    } else {
      res->append(make_pair(p.first, df));
      while(pit.hasNext) {
        res->append(pit.next());
      }
    }
  }
  res->ps = compact(res->ps);
  return res;
}

vector<Duo*>* cnvToDuoVector(vector<int>& ps) {
  vector<Duo*>* res = new vector<Duo*>;
  for(vector<int>::iterator it = ps.begin();it != ps.end();it++) {
    res->push_back(new Duo(new Numero(*it)));
  }
  return res;
}

struct Ptr {
  PairIterator& it;
  PII p;
  int cons;

  Ptr(PairIterator& it, int cons): it(it), p(it.next()), cons(cons) {}

  bool advance(int x) {
    cons += x;
    if(cons >= p.second) {
      if(!it.hasNext) {
        return false;
      }
      p = it.next();
      cons = 0;
    }
    return true;
  }
};

ll solve(vector<int>& v) {
  vector<Duo*>* b = cnvToDuoVector(v);
  ll res = 0;
  int n = b->size();
  for(int i=1;i<n;i++) {
    Duo* u = b->at(i-1);
    Duo* v = b->at(i);
    int ul = u->length();
    int vl = v->length();
    if(ul >= vl) {
      bool processed = false;
      PairIterator uit(u);
      PairIterator vit(v);
      Ptr uPtr(uit, 0);
      Ptr vPtr(vit, 0);
      for(;;) {
        if(uPtr.p.first > vPtr.p.first) {
          v->suffix->append(make_pair(0, ul - vl + 1));
          processed = true;
          break;
        } else if(uPtr.p.first < vPtr.p.first) {
          if(ul - vl > 0) {
            v->suffix->append(make_pair(0, ul - vl));
          }
          processed = true;
          break;
        } else {
          int uRest = uPtr.p.second - uPtr.cons;
          int vRest = vPtr.p.second - vPtr.cons;
          int toCons = min(uRest, vRest);
          bool uAdv = uPtr.advance(toCons);
          bool vAdv = vPtr.advance(toCons);
          if(!uAdv || !vAdv) {
            break;
          }
        }
      }
      if(!processed) {
        v->suffix = u->subNum(vl);
        v->suffix->inc();
      }
    }
    res += v->suffix->length();
  }
  return res;
}

int main(int argc, char** argv) {
  int n;
  vector<int> ps;
  scanf("%d", &n);
  for(int i=0;i<n;i++) {
    int a;
    scanf("%d", &a);
    ps.push_back(a);
  }

  ll res = solve(ps);

  printf("%lld\n",res);
  return 0;
}