#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000009;
const long long MOD = 1000000007;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vect;
typedef vector<pair<ll,ll>> vll;
ll t,n,m,ind1,ind2,row,x;
ll a[100007],p[100007];
set<ll> se;
map<ll,ll> mapl;
int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        mapl[a[i]]++;
    }

    ll ans = mapl[a[n]]*a[n];

    p[n-mapl[a[n]]] = mapl[a[n]];

    ll k = a[n-mapl[a[n]]];
    for(int i=n-mapl[a[n]]-1;i>=1;i--){
        if(a[i]!=k){
            k=a[i];
            p[i]=p[i+1] + mapl[a[i+1]];
        }
        else p[i] = p[i+1];
    }

    for(int i = n-mapl[a[n]]; i>=1; i--){
        if(a[i]-p[i] >= 1) ans+=a[i]-p[i];
        else continue;
    }
    cout << ans << endl;
}




