#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[2 * 100000], b[2 * 100000 - 1];
  char c;
  string str;
  vector<int> v;
  vector<string> s;
  map<string, int> m;
  pair<int, int> p;
  int n;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] <= a[i])
      b[i] = 0;
    else {
      b[i] = a[i - 1] - a[i];
      a[i] += b[i];
    }
    ans += b[i];
  }
  for (int i = 0; i < (int)(n); i++) cout << a[i] << " ";
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << ans << endl;
  return 0;
}
