#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, mx = 0;
  cin >> n;
  int c = n - 1;
  int a, sum = 0;
  cin >> a;
  mx = a;
  while (c--) {
    int b;
    cin >> b;
    if (b < mx) {
      sum += mx - b;
    } else if (b > mx) {
      mx = b;
    }
  }
  cout << sum;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
