#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
  int n, m, i, j, t;
  while (cin >> n) {
    for (i = 1; i <= n; i++) {
      cin >> a[i];
    }
    int ans = 0;
    for (i = 1; i < n; i++) {
      if (a[i] > a[i + 1]) {
        ans += a[i] - a[i + 1];
        a[i + 1] = a[i];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
