#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N;
  cin >> N;
  int stoolHeight = 0;
  int maxHeight = 0;
  for (int i = 0; i < N; i++) {
    int currentHeight;
    cin >> currentHeight;
    if (maxHeight > currentHeight) stoolHeight += maxHeight - currentHeight;
    maxHeight = max(maxHeight, currentHeight);
  }
  std::cout << stoolHeight << "\n";
  return 0;
}
