#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T, class T2> void smin(T& a, T2 val) {if (a > val) a = val;}template<class T, class T2> void smax(T& a, T2 val) {if (a < val) a = val;}
const int N = 5*(int)1e5 + 10;


void yes(){
   cout<<"Yes";
   // exit(0);
}

void no(){
   cout<<"No";
   // exit(0);
}


int n, m;
string s, t;

int32_t main(){_
   
   cin >> n;
   vector<int> a(n);

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

   int ans = 0;

   int mx = a[0];
   for (int i = 1; i < n; ++i)
   {
      ans += max(mx - a[i], 0LL);
      smax(mx, a[i]);
   }


   cout<<ans;

   return 0;
}#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T, class T2> void smin(T& a, T2 val) {if (a > val) a = val;}template<class T, class T2> void smax(T& a, T2 val) {if (a < val) a = val;}
const int N = 5*(int)1e5 + 10;


void yes(){
   cout<<"Yes";
   // exit(0);
}

void no(){
   cout<<"No";
   // exit(0);
}


int n, m;
string s, t;

int32_t main(){_
   
   cin >> n;
   vector<int> a(n);

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

   int ans = 0;

   int mx = a[0];
   for (int i = 1; i < n; ++i)
   {
      ans += max(mx - a[i], 0LL);
      smax(mx, a[i]);
   }


   cout<<ans;

   return 0;
}