#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;

    map<int, int> a;
    for(int i = 0;i<n;i++){
        int x; cin >> x;
        a[x]++;
    }

    int cost = 0, start = 0, end = a.rbegin()->first;
    auto it = a.rbegin();
    while(it!=a.rend() && it->first-start > 0){
        cost += it->second*(it->first-start);
        start += it->second;
        end = it->first;
        it++;
    }

    assert(distance(a.lower_bound(start), a.lower_bound(end)) == 0);

    /*if(it->first-start > 0){
        for(auto it1 = a.lower_bound(start);it1 != a.end() && it1->first != end;it1++){
            cost += (it1->first-start)*it1->second;
        }
    }*/

    cout << cost;

    return 0;
}