#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
set<int> s;
int freq[N], val[N];
bool hasNext(set<int>::iterator it) {
  it++;
  return it != s.end();
}
bool hasPrev(set<int>::iterator it) { return it != s.begin(); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (s.empty() or val[*s.rbegin()] != x) {
      s.insert(i);
      freq[i] = 1;
      val[i] = x;
    } else
      freq[*s.rbegin()]++;
  }
  auto it = s.begin();
  while (1) {
    while (it != s.end() and freq[*it] == 1) it++;
    if (it == s.end()) break;
    int i = *it;
    if (freq[i] > 2) {
      s.insert(i + 1);
      freq[i + 1] = freq[i] - 2;
      val[i + 1] = val[i];
    }
    freq[i] = 1;
    val[i]++;
    if (hasNext(it)) {
      auto nit = it;
      nit++;
      int j = *nit;
      if (val[j] == val[i]) {
        freq[i] += freq[j];
        s.erase(nit);
      }
    }
    if (hasPrev(it)) {
      auto pit = it;
      pit--;
      int h = *pit;
      if (val[h] == val[i]) {
        freq[h] += freq[i];
        s.erase(it);
        it = pit;
      }
    }
  }
  vector<int> ans;
  for (int i : s) {
    for (int j = 0; j < freq[i]; j++) ans.push_back(val[i]);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d%c", ans[i], " \n"[i + 1 == ans.size()]);
  return 0;
}
