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
124
125
126
127
128
129
130
131
132
133
#include <cstdio>
#include <cstring>

char c[2][250000];

int main() {
    int n;
    scanf("%d",&n);
    char *a = c[0], *b;
    scanf("%s", a);
    int n_a, n_b;
    n_a = strlen(a);
    int b_ind=1;
    long long res=0;
    for(int i=0;i<n-1;i++) {
        b=c[b_ind];
        scanf("%s", b);
        n_b = strlen(b);
        
        if(n_a < n_b) {
            b_ind = 1-b_ind;
            a = c[1-b_ind];
            n_a = n_b;
            continue;
        }
        
        if(n_a == n_b) {
            //printf("n_a==n_b %s %s %d %d\n", a, b, n_a, n_b);
            bool is_a_lower = false;
            for(int i=0;i<n_a;i++)
                if(a[i]>b[i]) 
                    break;
                else if(a[i]<b[i]) {
                    is_a_lower = true;
                    break;
                }
            if(is_a_lower) {
                b_ind = 1-b_ind;
                a = c[1-b_ind];
                n_a = n_b;
                //printf("is_a_lower res:%lld\n", res);
                continue;
            }
            else {
                b[n_b] = '0';
                b[n_b+1] = '\0';
                b_ind = 1-b_ind;
                a = c[1-b_ind];
                n_a = n_b+1;
                res += 1;
                //printf("!is_a_lower res:%lld\n", res);
                continue;
            }
        }
        
        if(n_a > n_b) {//for clarity
            //printf("n_a>n_b %s %s %d %d\n", a, b, n_a, n_b);
            
            bool common_prefix = true;
            for(int j=0;j<n_b;j++)
                if(a[j]!=b[j]) {
                    common_prefix=false;
                    break;
                }
            
            if(common_prefix) {
                bool is_greater=false;
                int j=n_a-1;
                while(j>=n_b) {
                    if(a[j]!='9') {
                        b[j]=a[j]+1;
                        is_greater=true;
                        while(--j >= n_b) {
                            b[j]=a[j];
                            j--;
                        }
                    }
                    else
                        b[j]='0';
                    j--;
                }
                if(!is_greater) {
                    for(int j=n_b;j<=n_a;j++)
                        b[j]='0';
                    b[n_a+1] = '\0';
                    res += n_a-n_b+1;
                    b_ind = 1-b_ind;
                    a = c[1-b_ind];
                    n_a = n_a+1;
                }
                else {
                    res += n_a-n_b;
                    b_ind = 1-b_ind;
                    a = c[1-b_ind];
                    n_a = n_a;
                }
            }
            else {
                bool is_a_greater=true;
                for(int j=0;j<n_b;j++)
                    if(a[j]<b[j]) {
                        is_a_greater=false;
                        break;
                    }
                    else if(a[j]>b[j])
                        break;
                if(is_a_greater) {
                    for(int j=n_b;j<=n_a;j++) 
                        b[j]='0';
                    b[n_a+1]='\0';
                    res += n_a-n_b+1;
                    b_ind = 1-b_ind;
                    a = c[1-b_ind];
                    n_a = n_a+1;
                }
                else {
                    for(int j=n_b;j<n_a;j++) 
                        b[j]='0';
                    b[n_a]='\0';
                    res += n_a-n_b;
                    b_ind = 1-b_ind;
                    a = c[1-b_ind];
                    n_a = n_a;
                }
                
            }
            //printf("res:%lld %s\n", res, a);
        }
    }
    printf("%lld\n", res);
    
    return 0;
}