#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll  long long int
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define B begin
#define E end
#define mod 1000000007
#define YY cout<<"YES"<<endl
#define NN cout<<"NO"<<endl
#define Yy cout<<"Yes"<<endl
#define Nn cout<<"No"<<endl
#define yy cout<<"yes"<<endl
#define nn cout<<"no"<<endl
#define MX 4*10e18
#define NODES 100000
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;

using namespace __gnu_pbds;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
priority_queue<ll,vector<ll>,greater<ll> > san;

bool sortBysec(pair<ll,ll>a,pair<ll,ll>b)
{
    return a.second>b.second;
}



int main()
{
    FAST();
    ll t=1,tt=0,i,j,k,n,m,x,y,z,a,b,c,d,ans,p,q,f;

    //cin>>t;

    while(t--)
    {
        tt++;
        ordered_set<ll>os;
        cin>>n;
        vector<ll>v;
        for(i=0;i<n;i++)
        {
            cin>>x;
            v.pb(x);
        }
        ll ans=0;
        for(i=0;i<n-1;i++)
        {
            p=os.order_of_key(v[i]);
            x=v[i]-1;
            //cout<<v[i]<<" "<<p<<endl;
            ans+=max(0ll,(x-p));
            os.insert(v[i]);
        }
        cout<<ans<<endl;
    }

    return 0;
}
