#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  scanf("%d", &n);
  vector<int> heights(n, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &heights[i]);
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += heights[i] - heights[i - 1] < 0 ? heights[i - 1] - heights[i] : 0;
    heights[i] = heights[i] - heights[i - 1] < 0 ? heights[i - 1] : heights[i];
  }
  cout << ans << "\n";
}
