#include <iostream>
#include <string>
using namespace std;

int main() {
  long int N, a1 = 0, a2, count = 0;
  cin >> N;
  for (int i = 0; i<N; i++){
    cin >> a2;
    if(a1 > a2) count += a1-a2;
    else a1 = a2;
  }
  cout << count << endl;
  return 0;
}