#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, totalHeight = 0, diff, temp;
  int *a;
  cin >> n;
  a = new int[n];
  for (i = 0; i < n; i++) {
    cin >> temp;
    *(a + i) = temp;
    if (i > 0) {
      if (*(a + i - 1) > *(a + i)) {
        diff = *(a + i - 1) - *(a + i);
        totalHeight += diff;
        *(a + i) += diff;
      }
    }
  }
  cout << totalHeight;
}
