#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ans = 0;
  int x;
  int max = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x < max)
      ans += max - x;
    else
      max = x;
  }
  cout << ans;
  return 0;
}
