#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  vector<int> B(N, 0);
  for (int i = 0; i < (int)(N); i++) cin >> A[i];
  B = A;
  int ans = 0;
  for (int i = 0; i < A.size() - 1; i++) {
    if (B[i] < B[i + 1]) {
      ans += B[i + 1] - B[i];
      B[i + 1] += B[i + 1] - B[i];
    }
  }
  cout << ans;
}
