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
#include <iostream>
#include <stack>
using namespace std;

int main()
{
  int n, i, count, a, b, z;
  long result = 0;
  bool samo = false;
  unsigned long long l, tmp, lastl = 0;
  
  stack < int > akt;
  stack < int > last;
  cin >> n;
  count = 0;
  z = 0;
  for(i = 0; i < n; ++i){
    count = 0;
    if(lastl > 100000000000000000){
	lastl/=1000000;
	z+=6;
    }
    cin >> l;
    if(l > lastl) {
	lastl = l;
	result += z;
	continue;
    }
    if(l == lastl){
	if(z == 0){
	    lastl = l*10;
	    count++;
	}
	result+=(count+z);
	continue;
    }
    tmp = lastl;
    while(tmp > 0){
	last.push(tmp % 10);
	tmp = tmp / 10;
    }
    tmp = l;
    while(tmp > 0){
	akt.push(tmp % 10);
	tmp = tmp / 10;
    }
    samo = false;
    while(!last.empty()){
	if(akt.empty()){
	    while(!last.empty()){
		tmp = last.top();
		last.pop();
		samo = samo || (tmp!=9);
		count++;
	    }
	    if(!samo){
		count++;
	    }
	    if(z == 0) lastl=lastl+1;
	    result+=(count+z);
	} else {
    	    a = last.top();
	    b = akt.top();
	    last.pop();
	    akt.pop();
	    if(a < b){
		while(!last.empty()){
		    if(akt.empty()){
			count++;
			l*=10;
		    } else {
			akt.pop();
		    }
		    last.pop();
		}
		lastl = l;
		result+=(count+z);
	    } else if (a > b) {
		while(!last.empty()){
		    if(akt.empty()){
			count++;
			l*=10;
		    } else {
			akt.pop();
		    }
		    last.pop();
		}
		lastl=l*10;
		count++;
		result+=count+z;
	    }
	}
    }
  }
  cout << result;
  return 0;
}