#include <bits/stdc++.h>

using namespace std;
const int inf=1000000007;


long long a[100007],b,c,x,y,n,t,res,mi,ma,k,p,l;
map<long long, long long>mp;
set<long long>st;
string s;

int main()
{
    cin>>n;
    for (int i=1; i<=n; i++){
            cin>>a[i];
            mp[a[i]]++;
            st.insert(a[i]);
    }
    p=1;
    for (auto x : st){
       a[p]=x;
       p++;
    }
    res=0;
    l=0;
    for (int i=p-1; i>=1; i--){
        if (l>a[i])break;
        res+=mp[a[i]]*(a[i]-l);
        l+=mp[a[i]];
        //cout << a[i] << ' ' << mp[a[i]] << ' ' << res << endl;
    }
cout << res << endl;
  return 0;
}
