C. Ilya and Matrix

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve.

He's got a square 2n × 2n-sized matrix and 4n integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum.

The beauty of a 2n × 2n-sized matrix is an integer, obtained by the following algorithm:

  1. Find the maximum element in the matrix. Let's denote it as m.
  2. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2n - 1 × 2n - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices.

As you can see, the algorithm is recursive.

Help Ilya, solve the problem and print the resulting maximum beauty of the matrix.

Input

The first line contains integer 4n (1 ≤ 4n ≤ 2·106). The next line contains 4n integers ai (1 ≤ ai ≤ 109) — the numbers you need to arrange in the 2n × 2n-sized matrix.

Output

On a single line print the maximum value of the beauty of the described matrix.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Sample test(s)
input
1
13
output
13
input
4
1 2 3 4
output
14
Note

Consider the second sample. You need to arrange the numbers in the matrix as follows:

1 2
3 4

Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.


解题说明:题目的意思是如何排列这4^n个元素得到一个最大的beauty number,可以通过贪心来实现。首先对这4^n个数字按照从大到小的顺利排列,然后每次不断取原来数目的1/4,只需要确保每次剩下的都是排在最前面的数字即可。


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;

long long i,j,k,a[2000002],p;
int main()
{
	 scanf("%I64d",&k);
	 for(i=0;i<k;i++)
	 { 
		 scanf("%I64d",&a[i]); 
		 a[i]=-a[i]; 
	 }
	 sort(a,a+k);
	 for(i=0;i<k;i++)
	 {	
		 a[i]=-a[i];
	 }
	 for(;k;k=k/4)
	{	 
		for(i=0;i<k;i++) 
		{
			j+=a[i];
		}
	 }
	 printf("%I64d\n",j);
	 return 0;
}


你可能感兴趣的:(C. Ilya and Matrix)