#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e+14;
int main() {
  int n;
  cin >> n;
  vector<long long> a;
  for (int i = 0; i < n; i++) {
    long long p;
    cin >> p;
    a.push_back(p);
  }
  long long ans = 0;
  long long memory = a[0];
  for (int i = 0; i < n - 1; i++) {
    if (a[i] <= a[i + 1])
      memory = a[i + 1];
    else {
      ans += memory - a[i + 1];
    }
  }
  cout << ans << endl;
}
