#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long sum = 0;
  for (int i = 0; i < n;) {
    for (int j = 1; j + i < n; j++) {
      if (a.at(i + j) <= a.at(i)) {
        sum += a.at(i) - a.at(i + j);
        if (i + j == n - 1) {
          i += j + 1;
          break;
        }
      } else {
        i += j;
        break;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
