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

#define DBG(...) fprintf(stderr, __VA_ARGS__)

typedef unsigned I;
typedef unsigned long long L;
static const I TL = 16;

struct S {
        I len;
        char txt[TL];

        bool operator<(const S& r) const {
                if (len < r.len)
                        return true;
                if (len > r.len)
                        return false;
                for (I i=0; i<len; ++i) {
                        if (i >= TL)
                                return false;
                        if (txt[i] < r.txt[i])
                                return true;
                        if (txt[i] > r.txt[i])
                                return false;
                }
                return false;
        }

        bool caninc(const S& s) const {
                if (len == s.len)
                        return false;
                if (0 != memcmp(txt, s.txt, len > TL ? TL : len))
                        return false;
                for (I i=len; i<s.len; ++i) {
                        if (i >= TL)
                                return true;
                        if (s.txt[i] != '9')
                                return true;
                }
                return false;
        }

        S inc() const {
                S ret = *this;
                if (ret.len > TL)
                        return ret;
                I i = ret.len - 1;
                for (;;) {
                        if (ret.txt[i] != '9') {
                                ret.txt[i] ++;
                                break;
                        }
                        ret.txt[i--] = '0';
                }
                return ret;
        }

        void app0(I cnt) {
                for (I i=0; i<cnt; ++i) {
                        if (len + i >= TL)
                                break;
                        txt[len + i] = '0';
                }
                len += cnt;
        }

        S ext(const S& s) const {
                S ret = *this;
                if (ret.len == s.len) {
                        ret.app0(1);
                } else {
                        ret.app0(s.len - ret.len);
                        if (ret < s)
                                ret.app0(1);
                }
                return ret;
        }
};

static inline S calc(const S& cur, const S& last) {
        if (last < cur)
                return cur;
        if (cur.caninc(last))
                return last.inc();
        return cur.ext(last);
}


int main() {
        I N;
        scanf("%u\n", &N);

        L ret = 0;
        S last;
        last.len = 0;
        for (I n = 0; n < N; ++n) {
                S cur;
                scanf("%s\n", cur.txt);
                cur.len = strlen(cur.txt);
                last = calc(cur, last);
                ret += last.len - cur.len;
        }
        printf("%llu\n", ret);
        return 0;
}