#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n = 0;
  cin >> n;
  vector<int> a(n);
  vector<int> saidai(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      saidai[i] = a[i];
    } else {
      if (saidai[i - 1] < a[i]) {
        saidai[i] = a[i];
      } else {
        saidai[i] = saidai[i - 1];
      }
    }
    cout << saidai[i] << " ";
  }
  cout << endl;
  int sum = accumulate(saidai.begin(), saidai.end(), 0) -
            accumulate(a.begin(), a.end(), 0);
  cout << sum << endl;
}
