#include <bits/stdc++.h>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int> > qu2;
priority_queue<int> pq;
static const int NIL = -1;
static const int INF = 1000000007;
deque<int> deq;
const ll MOD = (1e+9) + 7;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
  }
  int temp = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      temp += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << temp << endl;
  return 0;
}
