#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int main() {
  int n;
  cin >> n;
  int list[N];
  for (int i = 0; i < n; i++) {
    cin >> list[i];
  }
  int answer = 0;
  for (int i = 1; i < n; i++) {
    if (list[i] < list[i - 1]) {
      answer += abs(list[i] - list[i - 1]);
      list[i] += abs(list[i] - list[i - 1]);
    }
  }
  cout << answer << endl;
}
