#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc = 1;
  while (tc--) {
    int n;
    cin >> n;
    int h[n];
    for (int i = 0; i < n; i++) cin >> h[i];
    int cnt = 0;
    for (int i = 1; i < n; i++) {
      if (h[i] < h[i - 1]) {
        cnt += h[i - 1] - h[i];
        h[i] = h[i - 1];
      }
    }
    cout << cnt;
  }
  return 0;
}
