/*
  Author: @yash_31
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional>
#define ll long long int
#define eb emplace_back
#define fatafat ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MOD 1000000007
#define ft first
#define sd second
#define deb(x) std::cout<<#x<<":"<<x<<endl;
#define endl "\n"
 
using namespace std;

const int mlt=(1<<18)+69;
const ll inf=1e18;
//TTBDC

int main()
{
  fatafat
  int n;
  cin>>n;
  int ans=0,x;
  int mx=0;
  for(int i=0;i<n;i++){
    cin>>x;
    if(x<mx)ans+=mx-x;
    mx=max(mx,x);
  }
  cout<<ans;
  return 0;
}