PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000218. Gộp các phần tử bằng nhau (Merge Equals)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Gộp các phần tử bằng nhau (Merge Equals)

Merge Equals

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn positive integers. While any value appears at least twice, perform this operation: choose the smallest repeated value xx, locate its first two occurrences from the left in the current array, remove the earlier occurrence, and replace the later occurrence by 2x2x. Other elements retain their relative order. Repeat until all remaining values are pairwise distinct. Output the final array, including its length.

Input

  • Line 1 contains nn, the initial array length.
  • Line 2 contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their initial order.

Output

  • Line 1 prints kk, the number of remaining elements.
  • Line 2 prints exactly kk integers: the final array from left to right.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 2≤n≤1500002\le n\le 150000; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.

Examples

Input

7
3 4 1 2 2 1 1

Output

4
3 8 2 1

Explanation

The smallest repeated value is initially 11, giving [3,4,2,2,2,1][3,4,2,2,2,1] after merging its first two occurrences. Merging the first two 22 values gives [3,4,4,2,1][3,4,4,2,1]. Next the two 44 values merge into 88, producing [3,8,2,1][3,8,2,1]. All values are now distinct, so the output length is 44 followed by this array.