#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int count = 0;
  int sub = 0;
  cin >> n;
  vector<int> human(n);
  for (int i = 0; i < n; i++) {
    cin >> human[i];
  }
  for (int i = 1; i < n; i++) {
    if (human[i] < human[i - 1]) {
      sub = human[i - 1] - human[i];
      count += sub;
      human[i] = human[i] + sub;
    }
  }
  cout << count;
  return 0;
}
