#include <bits/stdc++.h>
const long long mod = 1000000007;
using namespace std;
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) cin >> A[i];
  int total = 0;
  int h = -1;
  for (int i = 0; i < n; i++) {
    h = max(h, A[i]);
    total += (h - A[i]);
  }
  cout << total;
}
