#include <bits/stdc++.h>
using namespace std;
int CountHeight(vector<int>& heights) {
  int sum = 0;
  for (int i = 1; i < heights.size(); ++i) {
    if (heights[i] < heights[i - 1]) {
      int diff = heights[i - 1] - heights[i];
      sum += diff;
      heights[i] += diff;
    }
  }
  return sum;
}
int main() {
  int N;
  cin >> N;
  vector<int> arr;
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    arr.push_back(x);
  }
  cout << CountHeight(arr) << endl;
}
