#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
void test() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  int high = a[0];
  for (int i = 1; i < n; i++) {
    sum += (high - a[i] > 0) ? high - a[i] : 0;
    high = max(high, a[i]);
  }
  cout << sum;
}
int main() { test(); }
