#include <bits/stdc++.h>
using namespace std;
const int mxN = 2 * (1e5);
int heights[mxN + 1];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> heights[i];
  }
  int sum = 0;
  int prev = heights[1];
  for (int i = 2; i <= n; i++) {
    if (prev <= heights[i]) {
      prev = heights[i];
    } else if (prev > heights[i]) {
      sum += (prev - heights[i]);
      heights[i] = prev;
    }
  }
  cout << sum;
}
