CodeForces 313C 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:
- Find the maximum element in the matrix. Let's denote it as m.
- 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
- 1
13
- 13
- 4
1 2 3 4
- 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的更多相关文章
- codeforces C. Ilya and Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/313/C 题目意思:给定 4n 个整数(可以组成 2n × 2n 大小的矩阵),问通过对这些整数进行排列, ...
- codeforces Ilya and Matrix
http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...
- 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 ...
- codeforces 486B.OR in Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...
- Codeforces 518D Ilya and Escalator
http://codeforces.com/problemset/problem/518/D 题意:n个人,每秒有p的概率进电梯,求t秒后电梯里人数的期望 考虑dp:f[i][j]代表第i秒有j个人的 ...
- 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 ...
- ●CodeForces 518D Ilya and Escalator
题链: http://codeforces.com/problemset/problem/518/D题解: 期望dp. 定义dp[t][i]表示在第t秒开始之前,已经有了i个人在电梯上,之后期望能有多 ...
- Codeforces 903F Clear The Matrix(状态压缩DP)
题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...
- Codeforces 903F Clear the Matrix
题目大意 考虑一个 $4$ 行 $n$ ($4\le n\le 1000$)列的矩阵 $f$,$f$ 中的元素为 * 或 . . 对 $f$ 进行若干次如下变换: 将一个 $k\times k$($1 ...
随机推荐
- 夺命雷公狗—angularjs—2—模拟表单验证
这里我们就来借助妹子ui来搭建下模版,废话不多说,代码如下图所示: <!doctype html> <html lang="en"> <head> ...
- pic计数
#include <pic.h> //用的是PICC编译器 __CONFIG (HS & PROTECT & PWRTEN & BOREN & WDTDIS ...
- MessageDigest
转: 我们知道,编程中数据的传输,保存,为了考虑安全性的问题,需要将数据进行加密.我们拿数据库做例子.如果一个用户注册系统的数据库,没有对用户的信息进 行保存,如,我去页面注册,输入"Vic ...
- 在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法
在开发Asp.Net系统的时候,我们很多时候希望系统发生错误后能够跳转到一个自定义的错误页面,于是我们经常会在Global.asax中的Application_Error方法中使用Response.R ...
- 我的AutoCAD二次开发之路 (一)
原帖地址 http://379910987.blog.163.com/blog/static/33523797201011184552167/ 今天在改代码的时候,遇到了AddVertexAt方法的用 ...
- android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)
语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...
- css分离思想
CSS命名就应该最简单.最直接,直捣黄龙.没有HTML标签,没有层级,这些通通滚蛋,不要.为什么不要,有三大原因: 1. 限制重用 我们会使用层级(#test .test),会使用标签(ul.test ...
- linux设备驱动归纳总结(五):1.在内核空间分配内存【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-79134.html linux设备驱动归纳总结(五):1.在内核空间分配内存 xxxxxxxxxxxx ...
- dojo/dom dojo/domConstruct dojo/query
dom.byId require(["dojo/dom", "dojo/domReady!"], function(dom) { var one = dom.b ...
- linux的mtd架构分析【转】
转自:http://blog.csdn.net/column/details/xgbing-linux-mtd.html linux mtd 嵌入式系统的存储有很多不可靠之处.随着使用容量的增大,现在 ...