Pagini recente » Cod sursa (job #607581) | Cod sursa (job #2616909) | Cod sursa (job #1644216) | Cod sursa (job #1423678) | Cod sursa (job #3194600)
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin ("timbre.in");
ofstream fout("timbre.out");
int n,m,k,x,stare,y,c,sol,i,j,l,nr,cost,T[100003],S[100003];
priority_queue <pair<int,int>> Q;
int get_root(int x)
{
int c=x;
while(T[x]!=x)
x=T[x];
int nod=c;
while(nod!=x)
{
c=T[nod];
T[nod]=x;
nod=c;
}
return x;
}
void join(int x,int y)
{
int root_a=get_root(x);
int root_b=get_root(y);
if(root_a<root_b)
swap(root_a,root_b);
T[root_b]=root_a;
}
int main()
{
fin>>n>>m>>k;
for(i=0;i<=n+1;i++)
T[i]=i;
for(i=1;i<=n;i++)
{
fin>>y>>cost;
Q.push({cost,y});
}
j=k;
l=m/k+1;
for(i=m;i>=0;i--,j--)
{
if(!j)
{
j=k;
l--;
}
S[i]=l;
}
while(nr<m/k+1)
{
x=Q.top().first;
stare=S[Q.top().second];
Q.pop();
j=get_root(stare);
if(j<=m/k+1)
{
sol+=x;
nr++;
//fout<<stare<<" "<<j<<"\n";
join(j,j+1);
}
}
fout<<sol<<"\n";
return 0;
}