#include <bits/stdc++.h>
int main() {
  std::ios::sync_with_stdio(false);
  int n, height, sum_base_height;
  std::cin >> n;
  std::cin.ignore();
  sum_base_height = 0;
  for (int i = 0; i < n; i++) {
    int j;
    std::cin >> j;
    if (i == 0) {
      height = j;
    } else {
      int diff = height - j;
      if (diff > 0) {
        sum_base_height += diff;
      } else if (diff < 0) {
        height = j;
      }
    }
  }
  std::cout << sum_base_height << std::endl;
  return 0;
}
