#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i = 0, a[200000], sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 1 || a[i] > 1000000000) break;
  }
  if (i == n) {
    for (i = 1; i < n; i++) {
      if (a[i] < a[i - 1]) {
        sum = sum + (a[i - 1] - a[i]);
        a[i] = a[i - 1];
        cout << a[i] << " ";
      }
    }
    cout << sum;
  }
  return 0;
}
