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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_N = 2e5;

int tab[MAX_N + 3];
vector<int> A[MAX_N + 3];

int main()
{
    int N;
    scanf("%d", &N);
    tab[0] = -1;
    int wynik = 0;
    for(int i = 1; i <= N; i++)
    {
        int a;
        scanf("%d", &a);
        tab[i] = a;
        if(a == 0) A[i].push_back(a);
        while(a > 0)
        {
            A[i].push_back(a % 10);
            a /= 10;
        }
        reverse(A[i].begin(), A[i].end());

        bool wieksze = false;
        bool mniejsze = false;
        bool rowne = true;
        for(int j = 0; j < min(A[i].size(), A[i - 1].size()); j++)
        {
            if(A[i - 1][j] > A[i][j])
            {
                wieksze = true;
                rowne = false;
                break;
            }
            if(A[i - 1][j] < A[i][j])
            {
                mniejsze = true;
                rowne = false;
                break;
            }
        } 

        int poc = A[i].size();
        if(A[i - 1].size() > A[i].size())
        {
            int dl = A[i - 1].size();
            int kr = A[i].size();

            if(!rowne)
                while(A[i - 1].size() > A[i].size())
                    A[i].push_back(0);
            if(wieksze) A[i].push_back(0);
        
            if(rowne)
            {
                int x = 0;
                for(int j = kr - 1; j < dl; j++)
                    if(A[i - 1][j] != 9) x = j;

                for(int j = kr; j < dl; j++)
                {
                    if(j <= x)
                        A[i].push_back(A[i - 1][j] + (j == x));
                    else
                        A[i].push_back(0);
                }
            }
        }
        else if(A[i - 1].size() == A[i].size() && wieksze)
            A[i].push_back(0);

        wynik += A[i].size() - poc;
    }
    printf("%d", wynik);
}