#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  int mem[n];
  for (int i = 0; i < (n); ++i) cin >> mem[i];
  int total = 0;
  int high = 0;
  for (int i = 0; i < (n); ++i) {
    if (mem[i] > high) {
      high = mem[i];
    } else {
      total += high - mem[i];
    }
  }
  cout << total << endl;
  return 0;
}
