#include <bits/stdc++.h>
using namespace std;
int const ic_max = 100001;
long long int iGetMinMoveNum(int iN, int iArr[]);
int main() {
  int iNum;
  long long int iAns;
  int iArr[ic_max];
  while (cin >> iNum) {
    for (int i = 1; i <= iNum; i++) {
      cin >> iArr[i];
    }
    iAns = iGetMinMoveNum(iNum, iArr);
    cout << iAns << endl;
  }
  return 0;
}
long long int iGetMinMoveNum(int iN, int iArr[]) {
  long long int iC;
  iC = 0;
  for (int i = 1; i < iN; i++) {
    if (iArr[i] > iArr[i + 1]) {
      iC += iArr[i] - iArr[i + 1];
    }
  }
  return iC;
}
