#include <bits/stdc++.h>
using namespace std;
void rwFile() {
  freopen(
      "task"
      ".in",
      "r", stdin);
  freopen(
      "task"
      ".out",
      "w", stdout);
}
void solveA() {
  int n, x, t;
  cin >> n >> x >> t;
  cout << (n + x - 1) / x * t;
}
void solveC() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int res = 0;
  for (int i = 1; i < n; ++i) {
    if (arr[i] < arr[i - 1]) {
      res += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solveC();
}
