#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int x = 0;
  int last_a;
  cin >> N;
  cin >> last_a;
  for (int i = 0; i < (N - 1); i++) {
    int Ai;
    cin >> Ai;
    if (last_a <= Ai) {
      last_a = Ai;
    } else {
      x += last_a - Ai;
    }
  }
  cout << x << endl;
  return 0;
}
