#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s = 0;
  cin >> n;
  int maxh, m;
  cin >> m;
  maxh = m;
  for (int i = 1; i < n; i++) {
    int x;
    cin >> x;
    if (x < maxh)
      s += maxh - x;
    else
      maxh = x;
  }
  cout << s;
  return 0;
}
