#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int main() {
  int n;
  vector<long long> v(n + 10);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  long long ans = 0;
  long long maxx = v[1];
  for (int i = 2; i <= n; i++) {
    if (v[i] > maxx) {
      maxx = v[i];
    } else {
      ans += (maxx - v[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
