Problem:
Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 各種変数の読み込み
            var nm = Readints();
            var n = nm[0];
            var m = nm[1];
            var a = Readints();
            var ad = new double[a.Length];
            double area = 0;
            for (int i = 0; i < a.Length; i++)
            {
                ad[i] = Math.Log((double)a[i])/Math.Log(2);
                area += ad[i];
            }
            Array.Sort(ad);

            double tailsum = 0;
            double threshold = 0;
            int thresholdindex = 0;
            for (int i = 0; i < n; i++)
            {
                var temp = area - tailsum - (n - i) * ad[i];
                if (temp < m)
                {
                    threshold = ad[i] - (m - temp) / (n - i);
                    thresholdindex = i;
                    // 半額でもthreshold以上なら必ずOK
                    break;
                }
                tailsum += ad[i];
            }
            var kezurinum = new int[n];
            var kezurisum = 0;
            for (int i = n-1; i >= thresholdindex; i--)
            {
                kezurinum[i] = (int)Math.Floor(ad[i] - threshold);
                kezurisum += kezurinum[i];
            }
            var shousuu = new double[n - thresholdindex];
            for (int i = n - 1; i >= thresholdindex; i--)
            {
                shousuu[n - 1 - i] = ad[i] - kezurinum[i];
            }
            var sortedindexes = SortwithIndex(shousuu);
            for (int i = 0; i < m - kezurisum; i++)
            {
                kezurinum[thresholdindex + sortedindexes[i]]++;
            }
            long kingaku = 0;
            Array.Sort(a);
            for (int i = 0; i < n; i++)
            {
                kingaku += a[i] >> Math.Min(31, kezurinum[i]);
            }
            Console.WriteLine(kingaku);



#if DEBUG
            Console.ReadKey();
#endif
        }


        private static int Readint()
        {
            // 1行に1つのintが入力されたのを読む
            return int.Parse(Console.ReadLine());
        }

        private static int[] Readints()
        {
            // 1行に複数のintが入力されたのを読む
            var s = Console.ReadLine().Split(' ');
            var output = new int[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                output[i] = int.Parse(s[i]);
            }
            return output;
        }

        private static int[][] Readints(int n, bool transpose = false)
        {
            // n行それぞれにintが入力されたのを読む
            // output[行番号][列番号]で返す。
            // transposeがtrueのときは、output[列番号][行番号]で返す。
            var s = Console.ReadLine().Split(' ');
            int rownum = s.Length;
            if (transpose)
            {
                var outputs = new int[rownum][];
                for (int j = 0; j < rownum; j++)
                {
                    outputs[j] = new int[n];
                }
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[i][0] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[i][l] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
            else
            {
                var outputs = new int[n][];
                outputs[0] = new int[rownum];
                for (int i = 0; i < s.Length; i++)
                {
                    outputs[0][i] = int.Parse(s[i]);
                }
                for (int l = 1; l < n; l++)
                {
                    outputs[l] = new int[rownum];
                    var temps = Console.ReadLine().Split(' ');
                    for (int i = 0; i < rownum; i++)
                    {
                        outputs[l][i] = int.Parse(temps[i]);
                    }
                }
                return outputs;
            }
        }



        // 元の配列をソートして、ソート後の各要素の、元のインデックスを返す
        private static int[] SortwithIndex(double[] data)
        {
            var index = new int[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                index[i] = i;
            }
            QSort(data, index, 0, data.Length - 1);
            return index;
        }

        private static void QSort(double[] data, int[] index, int low, int high)
        {
            // 要素数が1以下の領域ができた場合、その領域は確定とする。
            if (high - low < 1)
                return;

            // 1. ピボットとして一つ選びそれをPとする。
            var P = data[(low + high + 1) / 2];

            // 2. 左から順に値を調べ、P以上のものを見つけたらその位置をiとする。
            // 3. 右から順に値を調べ、P以下のものを見つけたらその位置をjとする。
            // 4. iがjより左にあるのならばその二つの位置を入れ替え、2に戻る。ただし、次の2での探索はiの一つ右、次の3での探索はjの一つ左から行う。
            int i = low - 1;
            int j = high + 1;
            while (Scan(data, P, ref i, ref j))
            {
                Swap(data, index, i, j);
            }

            // 5. 2に戻らなかった場合、iの左側を境界に分割を行って2つの領域に分け、そのそれぞれに対して再帰的に1からの手順を行う。
            QSort(data, index, low, i - 1);
            QSort(data, index, i, high);
        }

        private static bool Scan(double[] data, double P, ref int i, ref int j)
        {
            while (data[++i] < P) ;
            while (data[--j] > P) ;
            return i < j;
        }


        private static void Swap(double[] data, int[] index, int i, int j)
        {
            var temp = data[i];
            data[i] = data[j];
            data[j] = temp;
            var temp2 = index[i];
            index[i] = index[j];
            index[j] = temp2;
        }
    }
}