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
//Zadanie "Nowy kontrakt", autor rozwiazania: Pawel Talar

#include <bits/stdc++.h>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    string current, prev;
    int t;
    cin >> t;
    t--;
    long long res = 0;
    cin >> prev;
    while(t--)
    {
        cin >> current;
        if(current.size() == prev.size())
        {
            //cout << prev.compare(current) << "\n";
            if(prev >= current)
            {
                prev = current + '0';
                res++;
            }
            else
            {
                prev = current;
            }
        }
        else if(current.size() > prev.size())
        {
            prev = current;
        }
        else
        {
            int a = prev.size();
            int b = current.size();
            int k = a-b;
            res += k;
            bool is_the_same = true;
            string temp;
            for(int i = 0; i < b; i++)
            {
                temp += prev[i];
            }
            if(temp != current)
                is_the_same = false; 
            if(is_the_same)
            {
                bool is_only_nines = true;
                int last_non_nine;
                for(int i = b; i < a; i++)
                {
                    if(prev[i] != '9')
                    {
                        last_non_nine = i;
                        is_only_nines = false;
                    }
                }
                if(!is_only_nines)
                {
                    for(int i = b; i < last_non_nine; i++)
                    {
                        current += prev[i];
                    }
                    current += (char)((int)prev[last_non_nine] + 1);
                    for(int i = last_non_nine+1; i < a; i++)
                        current += '0';
                }
                else
                {
                    for(int i = b; i <= a; i++)
                        current += '0';
                    res++;
                }
            }
            else if(temp < current)
            {
                for(int i = b; i < a; i++)
                    current += '0';
            }
            else
            {
                for(int i = b; i <= a; i++)
                    current += '0';
                res++;
            }
            prev = current;
        }
        //cout << endl << prev << " " << res << endl;
      //cout << res << "\n"; 
    }
    cout << res << "\n";
    return 0;
}