#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ans = 0, max_val = INT_MIN;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < max_val) {
      ans += (abs(max_val - a));
    }
    if (max_val < a) max_val = a;
  }
  cout << ans;
  return 0;
}
