#include <bits/stdc++.h>
using namespace std;
void solve() {}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  int m = 0, sum = 0;
  for (int i = 0; i < n; ++i) {
    m = max(m, a[i]);
    sum += m - a[i];
  }
  cout << sum;
}
