#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> arr(n);
  int maxSeen = 0;
  int total = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    maxSeen = max(maxSeen, arr[i]);
    if (arr[i] != maxSeen) {
      total += (maxSeen - arr[i]);
    }
  }
  cout << total << "\n";
  return 0;
}
