#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, s, r;
  cin >> N;
  r = 0;
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    cin >> s;
    L[i] = s;
  }
  for (int i = 0; i < N - 1; i++) {
    cout << L[i] << L[i + 1] << endl;
    if (L[i] - L[i + 1] > 0) {
      r += L[i] - L[i + 1];
      L[i + 1] = L[i];
    }
  }
  cout << r << endl;
}
