#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  long long n, a, sum = 0;
  cin >> n;
  vector<long long> height;
  for (long long i = (0); i < (n); i++) {
    cin >> a;
    height.push_back(a);
  }
  for (long long i = (0); i < (n - 1); i++) {
    if (height[i] < height[i + 1]) {
      sum += (height[i + 1] - height[i]);
    }
  }
  cout << sum;
  return 0;
}
