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
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cassert>

using namespace std;

const long long EQUAL = 0;
const long long LEFT_GRT = 1;
const long long RIGHT_GRT = 2;


long long compare(string& l, string& r, long long num) {
    if (num == -1) {
        num = max(l.length(), r.length());
        if (l.length() != r.length()) {
            return l.length() < r.length();
        }
    }

    for (long long i = 0; i < num; ++i) {
        if (l[i] < r[i]) {
            return RIGHT_GRT;
        } else if (l[i] > r[i]) {
            return LEFT_GRT;
        }
    }

    return EQUAL;
}

long long leftGrtCase(string& lastContract, string& contract) {
    long long suflen = lastContract.length() - contract.length() + 1;
    string sufix(suflen, '0');
    contract += sufix;
    lastContract = contract;
    return suflen;
}

long long rightGrtCase(string& lastContract, string& contract) {
    long long suflen = lastContract.length() - contract.length();
    string sufix(suflen, '0');
    contract += sufix;
    lastContract = contract;
    return suflen;
}

long long equalCase(string& lastContract, string& contract) {
    long long suflen = lastContract.length() - contract.length();
    string sufix = lastContract.substr(contract.length());

    // edge case
    string hellString(suflen, '9');
    if (compare(hellString, sufix, -1) == EQUAL) {
        return leftGrtCase(lastContract, contract);
    }

    contract += sufix;
    for (long long i = contract.length() - 1; i >= 0; --i) {
        if (contract[i] == '9') {
            contract[i] = '0';
        } else {
            contract[i] += 1;
            break;
        }
    }

    lastContract = contract;
    return suflen;
}

long long modify(string& lastContract, string& contract) {
    if (compare(lastContract, contract, -1) == RIGHT_GRT) {
        lastContract = contract;
        return 0;
    }

    switch (compare(lastContract, contract, contract.length())) {
        // prefix mam mniejszy, wiec dopelniam zerami i dodaję dodatkowe zero
        case LEFT_GRT:
            return  leftGrtCase(lastContract, contract);

        // prefix mam wiekszy, wiec dopelniam zerami
        case RIGHT_GRT:
            return rightGrtCase(lastContract, contract);

        case EQUAL:
            return equalCase(lastContract, contract);
    }


}

int main() {
    long long contractsNo;
    cin >> contractsNo;

    string lastContract;
    cin >> lastContract;

    long long extraDigits = 0;
    for (long long i = 1; i < contractsNo; ++i) {
        string contract;
        cin >> contract;
        
        long long cost = modify(lastContract, contract);
        extraDigits += cost;
    }
    
    cout << extraDigits << endl;

    return 0;
}