#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, count, countmae;
  cin >> N;
  if (1 <= N <= 2 * 10 ^ 5) {
    count = 0;
    vector<int> vec(N);
    for (int i = 0; i < N; ++i) cin >> vec[i];
    for (int j = 0; j < N - 1; ++j) {
      if (vec.at(j) > vec.at(j + 1)) {
        countmae = count;
        count = (vec.at(j) - vec.at(j + 1)) + countmae;
        vec.at(j + 1) = vec.at(j);
      }
    }
    cout << count << endl;
  }
}
