#include <bits/stdc++.h>
using namespace std;
void solve(int row[], int n);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n{};
  cin >> n;
  int row[n];
  for (int i = 0; i < n; i++) {
    cin >> row[i];
  }
  solve(row, n);
  return 0;
}
void solve(int row[], int n) {
  int mx = row[0];
  int count{};
  for (int i = 0; i < n; i++) {
    mx = max(row[i], mx);
    count += mx - row[i];
  }
  cout << count << "\n";
}
