#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ans = 0;
  int min = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i != 0) {
      if (min > a) {
        int step = min - a;
        ans += step;
        min = step + a;
      } else {
        min = a;
      }
    } else {
      min = a;
    }
  }
  cout << ans << endl;
}
