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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <cmath>
#include <vector>

 using namespace std;

 void pr(long long int x, long long int a, long long int azero, long long int alast) 
 {
     cout << x << "  " << a;// << "'";
     while (azero--) cout << "0";
     cout << "(" << alast << ")" << endl;
     cout << endl;
     cout << endl;
 }

 bool compare(long long int a, long long int azero, long long int alast, long long int adig, long long int p, long long int pzero, long long int plast, long long int pdig)
 {
     
        //cout << "XXXX " << adig << " " << azero << " " << pdig << " " << pzero << " XXXX" << endl;
     
        if (azero < pzero) return true;
        if (azero > pzero) return false;

        if (a < p) return true;
        if (a > p) return false;
        
        return (alast <= plast);    
 }

 int main()
 {
	long long int counter = 0;
	long long int a, azero, alast, adig; 
    long long int p, pzero, plast, pdig;
    
	long long int n;
	
	cin >> n;
	cin >> p;
    pdig = (int)(log10(p)+1.0);
    pzero = 0;
    plast = 0;
	n--;
	
	while (n--)
	{
		cin >> a;
        adig = (int)(log10(a)+1.0);
        azero = 0;
        alast = 0;
        
		if ( compare(a, 0, 0, adig, p, pzero, plast, pdig) ) 
		{
            //a<=p
            //cout << "compare failed " << a << endl; 
			if (adig < pdig+pzero)
			{
				//cout << "to short -- add " << pdig+pzero-adig << "  zeros" << endl;
                azero = pdig+pzero-adig;  // add some zeros a the end
                counter += pdig+pzero-adig;
                int ex;
                if (adig + azero <= 10)
                {
                    ex = azero;
                    a *= pow(10,azero);
                    adig += azero;
                    azero = 0;
                }    
                else
                {
                    ex = 10-adig;
                    a *= pow(10,10-adig);
                    azero -= 10 - adig;
                    adig = 10;
                }
                
                
                //cout << "XX A XX " << a << " " << azero << endl;
                
				if (compare(a, azero, 0, adig, p, pzero, plast, pdig) )
				{  
                   // cout << "still bad " << ex << endl;
                    //cout << "COMPARING " << a*pow(10,azero-pzero)+pow(10,ex)-1 << " " << 0 << " " << pow(10,ex)-1 << " " << p << " " << 0 << " " << plast << endl;
                    //cout << a+(long long int)(pow(10,ex)-1LL) << "  <  " << p << endl;
                    
                    if (a+pow(10,ex)-1LL < p)
                    {    
                        counter++;
                        p = a;
                        pzero = azero+1;
                        plast = 0;
                        pdig = adig;
                    }    
                    else if (a+pow(10,ex)-1 >= p)
                    {
                       // cout << " > " << endl;
                        
                        int ss = (int)(log10(plast+1LL)+1.0) - ex;
                        if (ss > 0)
                        {
                            //cout << ss << endl;
                            counter+=ss;
                            p = a;
                            pzero = azero+ss;
                            plast = 0;
                            pdig = adig;
                        }
                        else
                            plast++;
                    }
				}
                else
                { // now OK
                    
                    //cout << "ok with zeros" << endl;
					p = a;
                    pzero = azero;
                    plast = 0;
                    pdig = adig;
				}
			}
			else  //adig == pdig+pzero -> add 0 at the end
			{
                //cout << "same length add zero" << endl;
				p = a;
				pzero = 1;
                plast = 0;
                pdig = adig;
				counter++;
			}
			
			//int digits = (int)(log10(ap/a));
			
			//counter+=digits;
			
			//cout << prev << endl;
			
			//prev = ap;
			
			//cout << a << " " << prev << " " << digits << " " << endl;
		}
		else
        {
            // nothing to add
			p = a;
            pzero = 0;
            plast = 0;
            pdig = adig;
        }
        
        if (pdig + pzero <= 10)
        {
            p *= pow(10,pzero);
            pdig += pzero;
            pzero = 0;
        }    
        else
        {
            p *= pow(10,10-pdig);
            pdig = 10;
            pzero -= 10-pdig;
        }
                          
        
		//cout << a <<  " ---> ";
        //pr( aa, p , pzero ,plast);
        //cout << " ----   counter: " << counter << endl;
        
		
	}
	
	
	cout << counter << endl;
	return 0;	
 }