#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int result = 0;
  int maxTall = 0;
  int A;
  cin >> maxTall;
  for (int i = 0; i < N - 1; i++) {
    cin >> A;
    if (maxTall > A) {
      result += (maxTall - A);
    }
    if (maxTall < A) {
      maxTall = A;
    }
  }
  cout << result;
  return 0;
}
