#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  std::vector<int> vi;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    vi.push_back(num);
  }
  int min, diff;
  int sum = 0;
  for (int i = 1; i < N; i++) {
    min = vi[i - 1];
    if (min >= vi[i]) {
      diff = min - vi[i];
      vi[i] += diff;
      sum += diff;
    }
  }
  cout << sum;
}
