#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int row[n];
  for (int i = 0; i < n; i++) cin >> row[i];
  int s = 0;
  for (int i = 1; i < n; i++) {
    if (row[i] < row[i - 1]) {
      s += row[i - 1] - row[i];
      row[i] += row[i - 1] - row[i];
    }
  }
  cout << s;
}
