-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
63 lines (60 loc) · 1.36 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0);cin.tie(0)
long long ara[300005];
int main()
{
fastio;
long long t,n,m,k,i,ans,x,cnt,cur;
cin>>t;
while(t--)
{
cin>>n>>m>>k;
vector<pair<long long,long long> > vect;
for(i=0;i<n;i++)
{
cin>>ara[i];
vect.push_back({ara[i],i});
}
sort(vect.begin(),vect.end());
if(k%m==0)
x=k/m;
else
x=k/m+1;
vector<long long> idx;
for(i=0;i<x;i++)
{
idx.push_back(vect[i].second);
}
sort(idx.begin(),idx.end());
ans=0;
cnt=0;
for(i=0;i<x;i++)
{
if(i==x-1)
{
ans+=(k-cnt)*ara[idx[i]]+cnt*(k-cnt);
}
else
{
ans+=ara[idx[i]]*m+cnt*m;
cnt+=m;
}
//cout<<ans<<"\n";
}
if(k%m!=0)
{
cur=ans;
for(i=x-2;i>=0;i--)
{
cur-=ara[idx[i+1]]*(k%m)+(i+1)*m*(k%m);
cur+=ara[idx[i+1]]*m+(i*m+k%m)*m;
cur-=ara[idx[i]]*m+(i*m)*m;
cur+=ara[idx[i]]*(k%m)+i*m*(k%m);
ans=min(ans,cur);
}
}
cout<<ans<<"\n";
}
return 0;
}