#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int& i : A) cin >> i;
  int height = 0;
  for (int i = 0, maxheight = 0; i < N; i++) {
    maxheight = max(maxheight, A[i]);
    height += maxheight - A[i];
  }
  cout << height;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
