#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int mani = 0;
  int suma = 0;
  int sumb = 0;
  for (int i = 0; i < n - 1; i++) {
    mani = max(mani, v[i]);
    if (v[i + 1] < mani) {
      suma = suma + v[i + 1];
      v[i + 1] = v[i + 1] + (mani - v[i + 1]);
      sumb = sumb + v[i + 1];
    }
  }
  cout << abs(suma - sumb);
}
