#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, res = 0;
  cin >> n;
  vector<int> height(n);
  for (int i = 0; i < n; i++) cin >> height[i];
  for (int i = 1; i < n; i++)
    if (height[i - 1] > height[i]) {
      res += height[i - 1] - height[i];
      height[i] = height[i - 1];
    }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
