#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O>
void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U>
void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T>
void caut(T a) {
  cout << "(" << a << ") ";
};
using P = pair<ll, ll>;
const ll M = 1e9 + 7;
ll mod_pow(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1) an = an * x % M;
    x = x * x % M;
    a >>= 1;
  }
  return an;
}
void add(ll& x, ll y) {
  x += y;
  x %= M;
};
void mul(ll& x, ll y) {
  x *= y;
  x %= M;
};
template <typename T, typename U>
void chmax(T& x, U y) {
  if (x < y) x = y;
};
template <typename T, typename U>
void chmin(T& x, U y) {
  if (x > y) x = y;
}
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}
int keta(ll a) {
  int res = 0;
  while (a > 0) res += a % 10, a /= 10;
  return res;
}
const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1};
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int an = 0, mx = 0;
  for (int i = 0; i < n; i++) {
    mx = max(a[i], mx);
    an += mx - a[i];
  }
  cout << an << endl;
}
