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