#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long V[N];
  for (int i = 0; i < N; i++) {
    cin >> V[i];
  }
  long long a = 0;
  long long t = 0;
  for (int i = 0; i < N; i++) {
    if (a <= V[i])
      a = V[i];
    else {
      int b = a - V[i];
      t = t + a;
      V[i] = V[i] + a;
    }
  }
  cout << t << endl;
}
