#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout << ‘>’ << #x << ‘:’ << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i–)
typedef long long ll;
int main()
{
ll n ,a[100000] , left[100000] = {0} ,right[100000] = {0} , mi[100000];
cin>>n;
REP(i,n){
cin>>a[i];
}
int high = INT_MIN ;
REP(i,n){
if(a[i]>high)
high = a[i];
left[i] = high;
}
high = INT_MIN ;
for(int i=n-1 ; i>=0 ; i--){
if(a[i]>high)
high = a[i];
right[i] = high ;
// DEBUG(right[i])
}
int count = 0 , ans = 0 ;
REP(i,n){
mi[i] = min(left[i],right[i]);
count = mi[i]-a[i];
ans+=count;
}
cout<<ans<<endl;
return 0;
}