#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans = 0;
  int x = a[0];
  for (int i = 1; i < n; ++i) {
    ans += max(0, x - a[i]);
    x = max(x, a[i]);
  }
  cout << ans << endl;
}
