#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  priority_queue<int> que;
  que.push(0);
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (que.top() > a) {
      cnt += que.top() - a;
    }
    que.push(a);
  }
  cout << cnt << endl;
}
