313C.md (1908B)
1 Source: https://codeforces.com/problemset/problem/313/C 2 3 C. Ilya and Matrix 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 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. 22 23 He's got a square 2*n* × 2*n*-sized matrix and 4*n* 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. 24 25 The beauty of a 2*n* × 2*n*-sized matrix is an integer, obtained by the following algorithm: 26 27 1. Find the maximum element in the matrix. Let's denote it as *m*. 28 2. If *n* = 0, then the beauty of the matrix equals *m*. Otherwise, a matrix can be split into 4 non-intersecting 2*n* - 1 × 2*n* - 1-sized submatrices, then the beauty of the matrix equals the sum of number *m* and other four beauties of the described submatrices. 29 30 As you can see, the algorithm is recursive. 31 32 Help Ilya, solve the problem and print the resulting maximum beauty of the matrix. 33 34 Input 35 36 The first line contains integer 4*n* (1 ≤ 4*n* ≤ 2·106). The next line contains 4*n* integers *a**i* (1 ≤ *a**i* ≤ 109) — the numbers you need to arrange in the 2*n* × 2*n*-sized matrix. 37 38 Output 39 40 On a single line print the maximum value of the beauty of the described matrix. 41 42 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 %I64d specifier. 43 44 Examples 45 46 Input 47 48 ``` 49 1 50 13 51 ``` 52 53 Output 54 55 ``` 56 13 57 ``` 58 59 Input 60 61 ``` 62 4 63 1 2 3 4 64 ``` 65 66 Output 67 68 ``` 69 14 70 ``` 71 72 Note 73 74 Consider the second sample. You need to arrange the numbers in the matrix as follows: 75 76 ``` 77 1 2 78 3 4 79 ``` 80 81 Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.