#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int ans = 0;
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int num[n];
  for (int i = 0; i < (n); ++i) num[i] = 0;
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] > a[i + 1]) {
      num[i + 1] = a[i] - a[i + 1];
      a[i + 1] += num[i + 1];
      sum += num[i + 1];
    }
  }
  cout << sum << endl;
  for (int i = 0; i < (n); ++i) cout << num[i] << endl;
}
