#include <bits/stdc++.h>
using namespace std;
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using edge = struct {
  int to;
  int cost;
};
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;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
int ctoiii(const char c) {
  switch (c) {
    case '0':
      return 0;
    case '1':
      return 1;
    case '2':
      return 2;
    case '3':
      return 3;
    case '4':
      return 4;
    case '5':
      return 5;
    case '6':
      return 6;
    case '7':
      return 7;
    case '8':
      return 8;
    case '9':
      return 9;
    default:
      return -1;
  }
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a.at(i);
  }
  int cur = a.at(0);
  int ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    if (cur > a.at(i)) {
      ans += cur - a.at(i);
    } else if (cur < a.at(i)) {
      cur = a.at(i);
    }
  }
  cout << ans << "\n";
  return 0;
}
