#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const int INF = 1e9 + 5;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0, mx = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, a[i]);
    int bit = mx;
    sum += mx - a[i];
  }
  cout << sum << endl;
}
