#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 pr_numb = 0;
	int buys1 = 0;

	for(int i = str.length() - 1; i >= 0; i--) {
		int numb = str[i] - '0';
			if(pr_numb != numb) {
				buys1 = buys;
			}
			if(numb - buys1 <= 0) break;
			cost += numb - buys1;
			buys++;
			pr_numb = numb;
		
	}
 	
	cout << cost;
	
	return 0;
}