Ilya and Matrix

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 cin, cout streams or the %I64dspecifier.

Sample Input

Input
1
13
Output
13
Input
4
1 2 3 4
Output
14

Hint

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.

 #include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int a[]; bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n;
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+,cmp);
long long s=;
for(i=;pow(,i)<=n+;i++)
{
for(j=;j<=pow(,i);j++)
s=s+a[j];
}
printf("%I64d\n",s);
}
return ;
}

CodeForces 313C Ilya and Matrix的更多相关文章

  1. codeforces C. Ilya and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/313/C 题目意思:给定 4n 个整数(可以组成 2n × 2n 大小的矩阵),问通过对这些整数进行排列, ...

  2. codeforces Ilya and Matrix

    http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...

  3. Educational Codeforces Round 40 C. Matrix Walk( 思维)

    Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...

  4. codeforces 486B.OR in Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...

  5. Codeforces 518D Ilya and Escalator

    http://codeforces.com/problemset/problem/518/D 题意:n个人,每秒有p的概率进电梯,求t秒后电梯里人数的期望 考虑dp:f[i][j]代表第i秒有j个人的 ...

  6. codeforces 842C Ilya And The Tree

    Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...

  7. ●CodeForces 518D Ilya and Escalator

    题链: http://codeforces.com/problemset/problem/518/D题解: 期望dp. 定义dp[t][i]表示在第t秒开始之前,已经有了i个人在电梯上,之后期望能有多 ...

  8. Codeforces 903F Clear The Matrix(状态压缩DP)

    题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...

  9. Codeforces 903F Clear the Matrix

    题目大意 考虑一个 $4$ 行 $n$ ($4\le n\le 1000$)列的矩阵 $f$,$f$ 中的元素为 * 或 . . 对 $f$ 进行若干次如下变换: 将一个 $k\times k$($1 ...

随机推荐

  1. linux时区的设置

    到目前为止,个人的理解就是linux中设置时区就是修改配置文件 /etc/localtime 而通常的做法就是让这个文件作为符号链接,链接到 /usr/share/zoneinfo/ 中的某个特定的时 ...

  2. 使用Graham扫描法求二维凸包的一个程序

    #include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...

  3. Jsoup的demao

    package com.ch.jsoupdemo; import java.io.IOException; import org.jsoup.Jsoup;import org.jsoup.nodes. ...

  4. sql 循环语句几种方式

    --第一   declare @orderNum varchar(255) create table #ttableName(id int identity(1,1),Orders varchar(2 ...

  5. php用jquery-ajax上传多张图片限制图片大小

    php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...

  6. Linux, Mac下Shell 数组 Array 的修理工

    我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...

  7. windows cmd command line 命令

    Reference: 1. http://msdn.microsoft.com/en-us/library/ms977170.aspx(前面有一段VB看不懂没关系) 2. http://csserve ...

  8. Linux 进程状态【转】

    转自:http://www.cnblogs.com/itech/p/3208261.html 来自: http://blog.csdn.net/tianlesoftware/article/detai ...

  9. [转]Rotate a table in reporting services

    转自:http://stackoverflow.com/questions/9002796/rotate-a-table-in-reporting-services 12down voteaccept ...

  10. Docker第三方项目小结

    软件项目的成功常常根据其催生的生态系统来衡量.围绕或基于核心技术构建的项目增添了功能和易用性,它们常常日臻完善.Docker就是一个典例,有众多第三方项目扩展.补充或改进Docker. •Docker ...