游客 Signup | Login
中文 | En

4046 - 运输问题

现在已知N件商品,和搬运它们其中每一件的费用。现在搬家公司老板Mr.sb决定让我们每次任意选取2件商品。然后这2件商品只算一件商品的费用。但是这个商品的搬运费用是将选出的2个商品的费用之和除以k的运算结果。如此反复。直到只收一件商品的钱。这个就是商店要付的费用。掌柜的想尽可能的少付钱,以便将更多的钱捐给希望工程。所以请你帮他计算一下最少只用付多少钱。

Input

n,k(n和k<=10000)

w1,w2.....wn(每一件物品搬运费)

Output

输出一个数字,表示最少付多少钱

Examples

Input

5 2
1 2 3 4 5

Output

1

Solution C++

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10005;
int a[N];

int main(){
	int n,k;
	cin>>n>>k;
	for (int i=1; i<=n; i++) cin>>a[i];
	sort(a+1,a+n+1);
	for (int i=n,x,j; i>=2; i--){
		x=(a[i]+a[i-1])/k;
		j=i-2;
		while (x<a[j]){
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=x;
	}
	cout<<a[1]<<endl;
	return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题