#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int temp = 0, ans = 0, max = 0;
  for (int i : a) {
    if (temp == 0) {
      temp = i;
      max = i;
    } else {
      if (i < temp) {
        if (max < temp) {
          max = temp;
        }
        ans += max - i;
      }
      temp = i;
    }
  }
  cout << ans << endl;
}
