#include <bits/stdc++.h>
using namespace std;
int main() {
  int size;
  cin >> size;
  vector<int> values;
  for (int i = 0; i < size; i++) {
    int next;
    cin >> next;
    values.push_back(next);
  }
  int maxx = values.at(0);
  int answer = 0;
  for (int i = 1; i < size; i++) {
    maxx = max(maxx, values.at(i));
    answer += (maxx - values.at(i));
  }
  cout << answer;
}
