Heard that eom is a fishing MASTER, you want to acknowledge him as your mentor. As everybody knows, if you want to be a MASTER’s apprentice, you should pass the trial. So when you find fishing MASTER eom, the trial is as follow:
There are n fish in the pool. For the i - th fish, it takes at least ti minutes to stew(overcook is acceptable). To simplify this problem, the time spent catching a fish is k minutes. You can catch fish one at a time and because there is only one pot, only one fish can be stewed in the pot at a time. While you are catching a fish, you can not put a raw fish you have caught into the pot, that means if you begin to catch a fish, you can’t stop until after k minutes; when you are not catching fish, you can take a cooked fish (stewed for no less than ti) out of the pot or put a raw fish into the pot, these two operations take no time. Note that if the fish stewed in the pot is not stewed for enough time, you cannot take it out, but you can go to catch another fish or just wait for a while doing nothing until it is sufficiently stewed.
Now eom wants you to catch and stew all the fish as soon as possible (you definitely know that a fish can be eaten only after sufficiently stewed), so that he can have a satisfying meal. If you can complete that in the shortest possible time, eom will accept you as his apprentice and say “I am done! I am full!”. If you can’t, eom will not accept you and say “You are done! You are fool!”.
So what’s the shortest time to pass the trial if you arrange the time optimally?
Input
The first line of input consists of a single integer T(1≤T≤20), denoting the number of test cases.
For each test case, the first line contains two integers n(1≤n≤105),k(1≤k≤109), denoting the number of fish in the pool and the time needed to catch a fish.
the second line contains n integers, t1,t2,…,tn(1≤ti≤109) ,denoting the least time needed to cook the i - th fish.
Output
For each test case, print a single integer in one line, denoting the shortest time to pass the trial.
Sample Input
2
3 5
5 5 8
2 4
3 3
Sample Output
23
11
Hint
Case 1: Catch the 3rd fish (5 mins), put the 3rd fish in, catch the 1st fish (5 mins), wait (3 mins),
take the 3rd fish out, put the 1st fish in, catch the 2nd fish(5 mins),
take the 1st fish out, put the 2nd fish in, wait (5 mins), take the 2nd fish out.
Case 2: Catch the 1st fish (4 mins), put the 1st fish in, catch the 2nd fish (4 mins),
take the 1st fish out, put the 2nd fish in, wait (3 mins), take the 2nd fish out.
有n个鱼
钓鱼的时间都是k,但是炖鱼的时间不一样,每个鱼都有一个炖鱼时间ti
钓鱼的时候啥也不能干,只能钓鱼(不能再钓鱼的期间去炖鱼)。
但是炖鱼的时候可以去钓鱼。
问 把n个鱼全炖了吃了最少需要多少时间。
最简单的两种
1 k>=最大的炖鱼时间。这时候 我们只需要考虑每条鱼掉的时间就好了 然后最后加上一个最少的炖鱼时间
2 k<=最小的炖鱼时间,那么每条鱼都得炖吧,在炖鱼的时候去钓鱼。那么花费的时间就是把所有炖鱼的时间加起来,然后加上钓第一条鱼的时间
3 其他情况呢?
我们求 每个ti/k 然后加和 等于q
这个q的意思就是说,在炖鱼的时间,最多能钓多少条鱼
3 -1 如果q>=n-1(因为第一条必须是要先钓)那么就转化成了第2种情况
3 - 2 如果q
那么我们需要多浪费的时间就是n-1-q个这样的x 这个x咋算呢?
就是 k-ti%k 然后把所有的这个x排序 取前n-1-q个 在 加上 2的情况 就是最终结果
#include
#define ll long long
using namespace std;
const int maxn = 1e5+10;
ll a[maxn];
ll b[maxn];
ll n ,k;
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
ll max_time = a[n];
ll min_time = a[1];
ll ans;
if(k>=max_time){
ans = k*n+min_time;
printf("%lld\n",ans);
continue;
}
if(k<=min_time){
ans = k;
for(int i=1;i<=n;i++){
ans+=a[i];
}
printf("%lld\n",ans);
continue;
}
ll q=0;
for(int i=1;i<=n;i++){
q+=(a[i]/k);
}
if(q>=n-1){
ans = k;
for(int i=1;i<=n;i++){
ans += a[i];
}
printf("%lld\n",ans);
continue;
}
int last = n-1-q;
for(int i=1;i<=n;i++){
b[i] = k-a[i]%k;
}
sort(b+1,b+1+n);
ans = k;
for(int i=1;i<=n;i++){
ans += a[i];
}
for(int i=1;i<=last;i++){
ans += b[i];
}
printf("%lld\n",ans);
}
return 0;
}