#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  int sum = 0;
  int maxh = 0;
  for (int i = (0); i < (N); i++) {
    int A;
    cin >> A;
    if (A <= maxh)
      sum += maxh - A;
    else
      maxh = A;
  }
  cout << sum << endl;
  return 0;
}
