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
//
//  main.cpp
//  pa_kon
//
//  Created by Michal Kowalski on 12/12/2018.
//  Copyright © 2018 Kowalski Apps. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
using namespace std;

int N;
vector<string> A;

int compare(string & a, string & b) {
    int aLen = (int)a.length();
    int bLen = (int)b.length();
    if (aLen > bLen) return 1;
    if (bLen > aLen) return 2;
    for(int i = 0; i < aLen; ++i) {
        if (a[i] > b[i]) return 1;
        if (b[i] > a[i]) return 2;
    }
    return 0;
}

bool isPrefix(string & a, string & b) {
    return strncmp(a.c_str(), b.c_str(), a.length()) == 0;
}

void addZerosToB(string & a, string & b) {
    int len = abs((int)a.length() - (int)b.length());
    b.append(len, '0');
    int cmp = compare(a, b);
    if (cmp == 1) {
        b.push_back('0');
    }
}

string getSuffix(string & a,int len) {
    int aLen = (int)a.length();
    char suffix[len+1];
    int start = (int)aLen - len;
    a.copy(suffix, len, start);
    suffix[len] = 0;
    return string(suffix);
}

void increase(string & suffix) {
    int len = (int)suffix.length();
    int rest = 1;
    int start = len - 1;
    while(start >= 0 & rest == 1) {
        char c = suffix[start] + rest;
        if (c > '9') {
            rest = 1;
            suffix[start] = '0';
        } else {
            rest = 0;
            suffix[start] = c;
        }
        --start;
    }
    if (rest == 1) {
        suffix.assign(len+1,'0');
    }
}

int main(int argc, const char * argv[]) {
    
    scanf("%d",&N);
    A.resize(N);
    for (int i=0;i<N;++i) {
        char t[50];
        scanf("%s",t);
        A[i] = t;
    }
    string lastB = A[0];
    int i = 1;
    long long S = 0;
    while(i<N) {
        int cmp = compare(lastB, A[i]);
        if (cmp == 2) {
            lastB = A[i];
        } else if( cmp == 0) {
            string newB = A[i];
            newB.push_back('0');
            lastB = newB;
        } else {
            if (isPrefix(A[i], lastB)) { //non standard
                int len = (int)lastB.length() - (int)A[i].length();
                string suffix = getSuffix(lastB, len);
                increase(suffix);
                string newB = A[i];
                newB.append(suffix);
                lastB = newB;
            } else {
                string newB = A[i];
                addZerosToB(lastB, newB);
                lastB = newB;
            }
        }
        S += (long long)(lastB.length() - A[i].length());
        ++i;
    }
    
    printf("%lld\n",S);
    return 0;
}