#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long;
const ll INF = 1000000000000;
const int MOD = 1000000007;
const int MAXR = 100000;
const char EL = '\n';
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename... Args>
void input(Args... args) {
  for (int &e : {args...}) {
    cin >> e;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ;
  int N = 0;
  cin >> N;
  vector<int> A(N);
  for (ll i = 0; i < ll(N); i++) {
    cin >> A[i];
  };
  int _max = 0;
  int c = 0;
  for (ll i = 1; i < ll(N); i++) {
    _max = max(_max, A[i - 1]);
    c += max(_max - A[i], 0);
  }
  cout << c;
}
