#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    int prev = v[0];
    int sum = 0;
    for (int i = 1; i < n; i++) {
      if (v[i] < prev) {
        sum += abs(v[i] - prev);
      } else
        prev = v[i];
    }
    cout << sum;
  }
}
