#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5 + 5;
long long inf = 1e15;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    long long need = 0;
    long long ar[n];
    long long mx[n];
    for (int i = 0; i < n; i++) {
      cin >> ar[i];
    }
    mx[0] = ar[0];
    for (int i = 1; i < n; i++) {
      if (ar[i] < ar[i - 1]) {
        need += mx[i - 1] - ar[i];
      }
      mx[i] = max(mx[i - 1], ar[i]);
    }
    cout << need;
  }
  return 0;
}
