#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second
#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
using namespace std;
const ll inf = 1e18;
constexpr ll mod = 998244353;

void solve(){
	int n;
	cin >> n;
	vector <int> a(n);
	for (int i = 0; i < n; ++i) cin >> a[i];
	map <int, int> mp;
	for (auto i : a) ++mp[i];
	
	int ans = 0, l = 0, r = n - 1, cnt = 0, cnt1 = 0;
	while (l <= r){
		if (r + 1 < n && a[r] != a[r+1]) cnt1 += mp[a[r+1]];
		ans += a[r] - cnt1;
		++cnt;
		while (l <= r && a[l] <= cnt){
			++l;
		}
		--r;
	}
	cout << ans;
}
 
signed main(){
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    solve();
}