#include<bits/stdc++.h>
#define int long long
#define endl "\n";

using namespace std;

signed main() {
	
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n;
	cin >> n;
	int cost = 0;
	int buys = 0;
	string str = "";

	for(int i = 0; i < n; i++) {
		char temp;
		cin >> temp;
		str += temp;
	}

	int buys1 = 0;

	for(int i = str.length() - 1; i >= 0; i--) {
		
		int numb = str[i] - '0';
		if(numb - buys1 <= 0) continue;
		int y = 1;
		while(i > 0 && str[i-1] == str[i]) {
			y++;
			buys++;
			i--;
		}
			
			cost += (numb - buys1)*y;
			buys++;
			buys1 = buys;
	}
 	
	cout << cost;
	
	return 0;
}