#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
vector<int> ans;
int st[4 * Nmax];
int a[Nmax];
int r[Nmax];
int l[Nmax];
int n;
void update(int id, int l, int r, int i, int val) {
  if (l > i || r < i) return;
  if (l == r) {
    st[id] = val;
    return;
  }
  int mid = (l + r) >> 1;
  update(id * 2, l, mid, i, val);
  update(id * 2 + 1, mid + 1, r, i, val);
  st[id] = min(st[id * 2], st[id * 2 + 1]);
}
int go_segtree(int id, int l, int r) {
  int mid = (l + r) >> 1;
  if (l == r) return l;
  if (!st[id * 2]) return go_segtree(id * 2, l, mid);
  return go_segtree(id * 2 + 1, mid + 1, r);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    r[i] = i + 1;
    l[i] = i - 1;
  }
  for (int i = 1; i <= n; i++) {
    if (r[i] > n) {
      update(1, 1, n, i, 0);
    } else {
      update(1, 1, n, i, abs(a[i] - a[r[i]]));
    }
  }
  while (true) {
    int pos = go_segtree(1, 1, n);
    if (r[pos] > n) break;
    a[pos]++;
    update(1, 1, n, r[pos], 1e9);
    r[pos] = r[r[pos]];
    l[r[pos]] = pos;
    if (r[pos] > n)
      update(1, 1, n, pos, 0);
    else
      update(1, 1, n, pos, abs(a[pos] - a[r[pos]]));
    update(1, 1, n, l[pos], abs(a[pos] - a[l[pos]]));
  }
  int i = 1;
  while (i <= n) {
    ans.push_back(a[i]);
    i = r[i];
  }
  cout << ans.size() << "\n";
  for (int x : ans) cout << x << " ";
  return 0;
}
