#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> x;
  int A1, total = 0;
  cin >> A1;
  x.push_back(A1);
  for (int i = 0; i < N - 1; i++) {
    int A;
    cin >> A;
    x.push_back(A);
    if (x[i + 1] < x[i]) {
      int y;
      y = x[i] - x[i + 1];
      x[i + 1] = x[i + 1] + y;
      total = total + y;
    }
  }
  cout << total << endl;
}
