#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
int A[MAXN], D[MAXN];
int N;
long long cost;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
  for (int i = 2; i <= N; ++i) D[i] = A[i] - A[i - 1];
  for (int i = 2; i <= N; ++i)
    if (D[i] < 0) cost = cost - D[i];
  cout << cost << endl;
  return 0;
}
