#include <bits/stdc++.h>
using namespace std;
int CountHeight(vector<int>& heights) {
  int sum = 0;
  int max_height = 0;
  for (int i = 0; i < heights.size(); ++i) {
    max_height = max(max_height, heights[i]);
    sum += (max_height - heights[i]);
  }
  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;
}
