# 962D

You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value xx that occurs in the array 22 or more times. Take the first two occurrences of xx in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, 2x2⋅x).

Determine how the array will look after described operations are performed.

For example, consider the given array looks like [3,4,1,2,2,1,1][3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1]  [3,4,2,2,2,1]  [3,4,4,2,1]  [3,8,2,1][3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].

If the given array is look like [1,1,3,1,1][1,1,3,1,1] it will be changed in the following way: [1,1,3,1,1]  [2,3,1,1]  [2,3,2]  [3,4].

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
//使用优先队列，其中元素类型为pa
//优先队列的排序根据是value
//这样的话value小的一定会排在前面
//每次比较第一个和第二个是否相等
//如果不相等直接出队
//如果相等在数组中标记已被删除，然后队列中下一个元素value翻倍
//只要队列不空，循环此操作
struct pa
{
long long value;
int index;

bool operator <(const pa& u) const
{
if(value==u.value)
return index > u.index;             //万一两者value相等，要让索引小的删除
return value > u.value;
}
};
//初始化工作
const int maxn=150000+5;
//如果元素被删除就改成-1
long long array[maxn]={0};
int ans=0;

priority_queue<struct pa> b;
void delete_same()
{
struct pa u=b.top();
b.pop();
if(b.empty())
return;
if(b.top().value==u.value)
{
//下一个值和他一样，说明有重复
struct pa v;
v.value=u.value*2;
v.index=b.top().index;
b.pop();
b.push(v);
//还得把这个index的值改成-1
array[u.index]=-1;
array[v.index]*=2;
//cout<<u.value<<" has been emerged with "<<u.index<<"and "<<v.index<<endl;
ans++;
}
//如果不相等那就直接出队
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
pa newm;
newm.value=(long long)array[i];
newm.index=i;
//cout<<array[i]<<" "<<i<<" is being pushed in"<<endl;
b.push(newm);
}
while(!b.empty())
{
delete_same();
}
cout<<n-ans<<endl;
for(int i=0;i<n;i++)
{
if(array[i]>0)
cout<<array[i]<<" ";
}
printf("\n");
}
return 0;
}