#include <bits/stdc++.h>
using namespace std;
int recur_depth = 0;
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream&>::type operator<<(
    Ostream& os, const Cont& v) {
  os << "[";
  for (auto& x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
const long long int mod = 1000000000 + 7;
const long long int N = 1000000 + 6;
void inp(long long int* arr, long long int n) {
  for (int i = 0; i < n; i++) cin >> arr[i];
}
void inp_mat(long long int* arr, long long int n, long long int m) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      long long int x;
      cin >> x;
      *((arr + i * m) + j) = x;
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    long long int arr[n];
    inp(arr, n);
    long long int ans = 0;
    long long int ma = arr[0];
    for (int i = 1; i < n; i++) {
      ma = max(ma, arr[i]);
      ans += (arr[i] - ma);
    }
    cout << ans;
  }
}
