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
//
//  kon.cpp
//  Nowy kontrakt
//
//

#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
 
int main(int argc, const char * argv[]) {
    
    long n, a;
    long long unsigned digitsCounter {0};
    long long unsigned newValue, digitsToAdd;
    
    cin >> n;
    
    vector<long long unsigned> series;
    vector<long long unsigned> addedDigitsAtPositionVector(static_cast<size_t>(n), 0);
    series.reserve(n);
    
    for(long i = 0; i < n; i++){
        cin >> a;
        series.push_back(static_cast<long long unsigned>(a));
    }
    
    if(n == 1){
        cout << 0 << endl;
        return 0;
    }
    
    auto currentIterator = series.begin();
    auto endIterator = series.end() - 1;
    
    while(currentIterator != endIterator){
        
        long long unsigned index = currentIterator - series.begin();
        
        if(*currentIterator < *(currentIterator + 1)){
            currentIterator++;
            continue;
        }else{
            
            long long unsigned maxModulo = static_cast<long long unsigned>(pow(10, addedDigitsAtPositionVector.at(index)));
            long long unsigned originalValue = *(currentIterator) / max(maxModulo, static_cast<long long unsigned>(1));
            
            if(maxModulo > 0 && originalValue == *(currentIterator + 1) && addedDigitsAtPositionVector[index] > 0 && *(currentIterator) % maxModulo != maxModulo - 1){
                //można dodawać jeśli nie przekracza sie zakresu po dodaniu poprzednich cyfr
                //oraz kolejna
                newValue = (*currentIterator) + 1;
                digitsToAdd = addedDigitsAtPositionVector.at(index);
            }else{
                //trzeba pomnożyć przez potege 10
                newValue = *(currentIterator + 1);
                digitsToAdd = 0;
                do{
                    newValue = newValue * 10;
                    digitsToAdd++;
                    
                    if(newValue < *(currentIterator)
                       && (*(currentIterator) + 1) < (newValue * 10)
                       && *(currentIterator) % 10 != 9
                       && (newValue / 10llu) == ((*(currentIterator) + 1) / 10llu)){
                        newValue = *(currentIterator) + 1;
                        break;
                    }
                    
                }while(newValue < *(currentIterator));
            }

            addedDigitsAtPositionVector.at(index + 1) = digitsToAdd;
            digitsCounter += digitsToAdd;
            *(currentIterator + 1) = newValue;
            currentIterator++;
    
        }
    }
    
    cout << digitsCounter << endl;
    
//    for(auto data: series){
//        cout << data << endl;
//    }

    return 0;
    
}