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. 夺命雷公狗—angularjs—15—内置封装好的计时器$interval和$timeout

    这里其实和js源生的效果是一样的,但是源生的在angularjs里面不能直接正常执行代码如下所示: <!DOCTYPE html> <html lang="en" ...

  2. STM

    STM(System Trace macrocell) STM是coresight system中的一个trace source,可以提供high-bandwidth的trace data. STM优 ...

  3. 词频统计-------------web版本

    要求:把程序迁移到web平台,通过用户上传TXT的方式接收文件.建议(但不强制要求)保留并维护Console版本,有利于测试. 在页面上设置上传的控件,然后在servlet中接受,得到的是一个字节流, ...

  4. linux中的一个看图的软件

    毕竟才是初入linux,很多工具,先要要求有个用着,之后再要求小巧强大好用. 看图软件,不喜欢kde或者是gnome这种庞大的桌面环境,也就不喜欢一些跟特定桌面环境沾边的软件(总觉得用软件必须要用特定 ...

  5. ubuntu遇到的命令

    sudo passwd root这个命令是给root用户设定密码.su root切换到root用户.sudo cp 文件 /var/www移动文件到一个目录unzip xxx.zip解压zip文件mk ...

  6. HorizontalScrollView的配置

    package com.exmple.jinritoutiao; import java.util.ArrayList;import java.util.Calendar;import java.ut ...

  7. http-使用get和post方式提交数据

    注意点: 1.Get和post这两种提交方式有何不同? 很明显post方式提交多了content-length和content-type这两项,所以post提交是要为这两项设置setRequestPr ...

  8. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )

    1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...

  9. django migrate10060 Duplicate column name错误

    这个错误意思是有重复的列名,其实大部分原因是因为某些列被执行了多次,可以打开migration里面的django生成的文件去排查错误,然后自己手动修改数据库还原,实在不行可以把除了0001和init文 ...

  10. combotree

    1,直接获取:     单选:$("#id").combotree("getValue")     多选:$("#id").combotre ...