#include <bits/stdc++.h>
using namespace std;
int main() {
  int num, c = 0;
  cin >> num;
  int a[num];
  for (int i = 0; i < num; i++) {
    cin >> a[i];
  }
  int x = 0;
  while (x < num - 1) {
    if (a[x] < a[x + 1]) c += a[x + 1] - a[x];
    x++;
  }
  cout << c;
  return 0;
}
