【题意】

给n个数,每次选择最小的两个,且在最左边,且两个是相等的,把第一个消除掉,第二个变成两倍,位置不变。

问几番操作后 最后的数组是怎样的。

【题解】

优先队列处理即可,处理元素val值和下标,如果两个数相等,那么就优先输出最开始输入的那两个,如果两个数不相等,那么就把最小的那个先记录下来,然后把另一个压回到优先队列中。因为有可能组成另一个稍微大点的值。然后就是两个数合并过程和输出记录过程。

【代码】

/*简化版*/
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
//#define pb push_back
using namespace std;
typedef long long ll;
const int N=150000+10;
struct node
{
    ll val;
    int id;
    bool operator <(const node &o)const {
        if(val==o.val) return o.id<id;
        return o.val<val;
    }
}a[N];
struct node1
{
    ll val;
    int id;
    bool operator <(const node1 &o)const {
        return o.id<id;
    }
};
int main()
{
	int n;
	cin>>n;
	priority_queue<node>que;
	priority_queue<node1>ans;
	rep(i,1,n)
	{
	    cin>>a[i].val;
	    a[i].id=i;
	    que.push(a[i]);
	}
	while(que.size()>1)
    {
        node u=que.top();que.pop();
        node v=que.top();que.pop();
        if(u.val==v.val) {
            v.val+=u.val;
            que.push(v);
        }
        else{
            ans.push({u.val,u.id});
            que.push(v);
        }
    }
    while(que.size())
    {
        ans.push({que.top().val,que.top().id});que.pop();
    }
    printf("%d\n",ans.size());
    while(ans.size()){
        printf("%lld ",ans.top().val);ans.pop();
    }
}

【题目】

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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, 2⋅x2⋅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][1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].

Input

The first line contains a single integer nn (2≤n≤1500002≤n≤150000) — the number of elements in the array.

The second line contains a sequence from nn elements a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output

In the first line print an integer kk — the number of elements in the array after all the performed operations. In the second line print kkintegers — the elements of the array after all the performed operations.

Examples

input

Copy

7
3 4 1 2 2 1 1

output

Copy

4
3 8 2 1 

input

Copy

5
1 1 3 1 1

output

Copy

2
3 4 

input

Copy

5
10 40 20 50 30

output

Copy

5
10 40 20 50 30 

Note

The first two examples were considered in the statement.

In the third example all integers in the given array are distinct, so it will not change.