#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int sum1 = 0;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum1 += a[i];
  }
  int m = a[0];
  int sum2 = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a.at(i) > a.at(i + 1)) {
      a.at(i + 1) = a.at(i);
    } else {
      m = a[i + 1];
    }
  }
  for (int i = 0; i < n; i++) {
    sum2 += a[i];
  }
  cout << sum2 - sum1 << endl;
}
