#include <bits/stdc++.h>
int n = 0, max = 0, row_o = 0;
int main() {
  std::cin >> n;
  int row_i[n];
  for (int i = 0; i < n; i++) {
    std::cin >> row_i[i];
  }
  for (int j = 0; j < n; j++) {
    if (max > row_i[j]) {
      row_o = row_o + max - row_i[j];
    } else if (max < row_i[j]) {
      max = row_i[j];
    }
  }
  std::cout << row_o;
}
