#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int x;
  int max;
  cin >> max;
  int answer = 0;
  for (int i = 1; i < n; i++) {
    cin >> x;
    if (x > max)
      max = x;
    else
      answer += (max - x);
  }
  cout << answer;
}
