#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long c = 0;
  for (int i = n - 1; i > 0; i--) {
    int mx = a[i];
    for (int j = i - 1; j >= 0; j--) {
      mx = max(a[j], mx);
    }
    c += mx - a[i];
  }
  cout << c;
}
