#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) cin >> A.at(i);
  long long sum = 0;
  cout << endl;
  cout << A.at(0) << endl;
  for (long long i = 1; i < N; i++) {
    if (A.at(i) < A.at(i - 1)) {
      sum += A.at(i - 1) - A.at(i);
      A.at(i) = A.at(i - 1);
    }
    cout << A.at(i) << endl;
  }
  cout << sum << endl;
  return 0;
}
