#include <iostream>
using namespace std;
 
int main() {
  int n, h, max_h = 0;
  long long answer = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h;
    if (max_h < h) max_h = h;
    else answer += max_h - h;
  }
  cout << answer;
  return 0; 
}