#include<iostream>
#include<stdio.h>
using namespace std;
using ll=long long;
#include<algorithm>
#include<set>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)
using P=pair<int,int>;
const int mod=1000000007;

int main(){
  int n;
cin>>n;
vector<int>a[n];
rep(i,n)cin>>a[i];
rep(i,n){
  long long min=min(min,a[i]);
  long long sum+=min-a[i];
}
cout<<sum<<endl;
}


