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