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
#include <cstdio>
#include <iostream>

// Marcin Regdos | Potyczki Algorytmiczne 2018 | Runda 2B KON Nowy kontrakt
using namespace std;
int main(){
    long long result = 0;
    int n = 0;
    cin>> n ;
    string s1, s2;
    s1 = s2 = "";
    while (n--)
    {
        s1 = s2;
        cin >> s2;

        //cout << result << " " << s1 << " " << s2 << endl;

        if (s1.size() < s2.size()) continue;
        if (s2.size() == s1.size())
        {
            if (s1 < s2) continue;
            s2.append("0");
            ++result;
            continue;
        }

        string s1Pref =  s1.substr (0, s2.size());
        int s2ol = s2.size();
        if (s1Pref > s2)
        {
            // must add a lot of 0
            int t = (s1.size() - s2.size() + 1);
            result += t;
            while (t--) s2.append("0");
            continue;
        }
        if (s1Pref == s2)
        {
            int t = (s1.size() - s2.size());
            result += t;

            s2 = s1;
            int i;
            for (i = s2.size() - 1; i>= s2ol && s2[i] == '9'; --i);

            if (i>=s2ol)
            {
                char c = s2[i];
                s2[i] = ++c;
            } else
            {
                ++result; s2.append("0");
            }
            //cout << "x: " << result << " " << s1Pref << " " << i << " " << s2 << endl;

            continue;
        }
        else
        {
            int t = (s1.size() - s2.size());
            result += t;
            while (t--) s2.append("0");
        }
    }

    cout << result << endl;
    return 0;
  }