#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int ans[n];
  for (int i = 0; i < n; ++i) {
    cin >> ans[i];
  }
  int count = 0;
  for (int i = 1; i < n; ++i) {
    if (ans[i] < ans[i - 1]) {
      int temp = abs(ans[i - 1] - ans[i]);
      ans[i] += temp;
      count += temp;
    }
  }
  cout << count << endl;
}
