#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  int highest = 0;
  ll result = 0;
  ll d;
  cin >> n;
  int a[n];
  int ans = 0;
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
    if (highest < a[i]) {
      highest = a[i];
    }
    if (a[i] < highest) {
      ans += highest - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
