#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long ans = 0;
  long long k = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      k += (arr[i - 1] - arr[i]);
      ans += k;
    } else
      k = 0;
  }
  cout << ans << endl;
}
int main() { solve(); }
