transport

Title description

Now the goods have been installed. Mr. B., the boss of the moving company, came over. He told us that his birthday is today, and he was very happy. Originally decided to free shipping, but because of some factors he changed his mind. N products are now known, and the cost of handling each of them. Now Mr.B decided to let us choose 2 items at random. Combining these two products into a new product, the handling cost is the result of dividing the sum of the costs of the two selected products by K (K is read from the file) This is repeated until only one item of money is received. This is what the store has to pay. The shopkeeper wants to pay as little as possible in order to donate more money to the Hope Project. So please help him to calculate at least how much it will cost.

Input

n,k

w1, w2,…, wn (carrying cost of each product)

Output

Output a number indicating the minimum payment.

Sample input

5 2
1 2 3 4 5

Sample output

1

prompt

n<=10000,k<=10000

Code

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=(j);i<=(k);++i)
using namespace std;
priority_queue < int > s;
int n, k;
template<class T> inline void read(T &x) {
    x=0;
    register char c=getchar();
    register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    if(f)x=-x;
}
int main() {
    read(n), read(k);
    rep(i, 1, n) {
        register int x;
        read(x);
        s.push(x);
    }
    for (register int i = 1; i < n; i ++) {
        register int x;
        x = s.top();
        s.pop();
        x += s.top();
        s.pop();
        x = x / k;
        s.push(x);
    }
    cout << s.top() << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/12678588.html