#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
using pii = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> max(n, 0);
  max[0] = a[0];
  for (int i = 1; i < n; i++) {
    if (max[i - 1] <= a[i])
      max[i] = a[i];
    else
      max[i] = max[i - 1];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += max[i] - a[i] >= 0 ? max[i] - a[i] : 0;
  }
  cout << ans << endl;
}
