#include <bits/stdc++.h>
using namespace std;
const int MAX = 200000;
int main() {
  int N;
  int A[MAX];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  long long ans = 0;
  int t = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < t) {
      ans += t - A[i];
      A[i] = t;
    }
    cout << A[i] << endl;
    t = A[i];
  }
  cout << ans << endl;
  return 0;
}
