#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> height(n);
  for (int i = 0; i < n; i++) {
    cin >> height[i];
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int offset = max(0, height[i - 1] - height[i]);
    ans += offset;
    height[i] += offset;
  }
  cout << ans << endl;
  return 0;
}
