#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> res, ans;
  long long maxi = INT_MIN;
  for (int i = 0; i < n; i++) {
    maxi = max(maxi, a[i]);
    res.push_back(maxi);
  }
  int s(0);
  for (int i = 0; i < n; i++) {
    s += res[i] - a[i];
  }
  cout << s << "\n";
}
