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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
int a,b,k,x,da,dx,db;
int pot10[11];

vector<int> va;
vector<int> vx;

int dl(int kek) {
    int ret = 0;
   
    while(kek > 0) {
        kek /= 10;
        ret++;
    }
    return ret;
}

int cyfra(int i, int kek) {
    return (kek / pot10[i])%10;
}

void makev() {
    va.clear();
    vx.clear();
   
    int pom = x;
    while(pom > 0) {
        vx.push_back(pom%10);
        pom /= 10;
    }
    reverse(vx.begin(), vx.end());
   
    int kon = da+k-dx;
   
    for(int i = da+k-1; i >= kon; --i) {
        if(i >= k) {
            va.push_back(cyfra(i-k, a));
        }
        else if(i < db) {
            va.push_back(cyfra(i,b));
        }
        else {
            va.push_back(0);
        }
    }
}


int main() {
   
    pot10[0] = 1;
    for(int i = 1; i <= 9; ++i)
        pot10[i] = 10*pot10[i-1];
   
    scanf("%d",&n);
    scanf("%d",&a);
    
    ll res = 0;
    
    for(int i = 2; i <= n; ++i) {
        scanf("%d",&x);
       
        da = dl(a);
        dx = dl(x);
        db = dl(b);
   
        if(dx > da+k) {
            a = x;
            b = 0;
            k = 0;
            continue;
        }
        
        b++;
        db = dl(b);
        if(db > k) {
			b = 0;
			db = 0;
			a++;
			da = dl(a);
		}
       
        makev();
        
        if(vx == va) {
			res += da+k-dx;
			continue;
		}
        		
        if(vx > va) {
			k = da+k-dx;
            b = 0;
        }
        else if(vx < va) {
			k = da+k-dx+1;
			b = 0;
        }
        
        a = x;
        
        res += k;		
    }
    printf("%lld\n",res);
}