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
//Potyczki 2018 - Nowy kontrakt - Korneliusz Litman

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
using namespace std;

stack <char> s;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    string vp, v;
    bool b = false;

    cin >> vp;
    //vp = "1";
    //v = "1";
    ll lz = 0;

    for (int i = 1; i<n; i++)
    {
        cin >> v;
        //if (v.size() >= 11) v = "1000000000";
        b = false;
        //cout << vp << " -> " << v << '\n';
        if (v.size() < vp.size())
        {
            //cout << 1 << '\n';
            for (int j = 0; j<v.size(); j++)
            {
                if (v[j] > vp[j])
                {
                    for (int xd = v.size(); xd < vp.size() - 1; xd++) {v += '0'; lz++;}
                    b = true;
                    break;
                }
                else if (v[j] < vp[j])
                {
                    for (int xd = v.size(); xd <= vp.size() - 1; xd++) {v += '0'; lz++;}
                    b = true;
                    break;
                }
            }
            if (b == false)
            {
                //cout << 3 << '\n';
                if (vp[vp.size()-1] == '9')
                {
                    b = true;
                    s.push('0');
                }
                else
                {
                    s.push(vp[vp.size()-1] + 1);
                }
                for (int j = vp.size() - 2; j >= v.size(); j--)
                {
                    if (vp[j] == '9' && b == true)
                    {
                        s.push('0');
                    }
                    else if (b == true)
                    {
                        b = false;
                        s.push(vp[j] + 1);
                    }
                    else s.push(vp[j]);
                }
            }
            if (b == true)
            {
                b = false;
                s.push('0');
            }
            while (!s.empty())
            {
                lz++;
                //cout << s.top() << '\n';
                v += s.top();
                s.pop();
            }
        }
        else if (v.size() == vp.size())
        {
            //cout << 2 << '\n';
            for (int j = 0; j<v.size(); j++)
            {
                //cout << v.size() << ' ' << v[j] << " por " << vp[j] << '\n';
                if (v[j] < vp[j])
                {
                    v += '0';
                    lz++;
                    b = true;
                    break;
                }
                else if (v[j] > vp[j])
                {
                    b = true;
                    break;
                }
            }
            if (b == false)
            {
                lz++;
                v += '0';
            }
        }
        //cout << vp << " <- " << v << '\n';
        vp = v;
        
    }

    cout << lz;
}