#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int m = INT_MIN;
  int tool = 0;
  while (n--) {
    int val;
    cin >> val;
    m = max(m, val);
    if (m > val) tool += m - val;
  }
  cout << tool;
}
