Pagini recente » Cod sursa (job #984971) | Cod sursa (job #3230831) | Cod sursa (job #411289) | Cod sursa (job #572669) | Cod sursa (job #184837)
Cod sursa(job #184837)
#include<stdio.h>
#define dim [50001]
struct auxiliar{
long long p,nr;
} aux[1001];
long long n,ttotal,k,i,j,l,p dim,m,best dim,u[1001],t dim,h dim,tmax;
FILE *in,*out;
void heap(int i){
int s,d,max;
while(i<<1<=m){
s=i<<1;d=s+1;max=i;
if(p[h[max]]<p[h[s]])
max=s;
if(d<=m&&p[h[max]]<p[h[d]])
max=d;
if(max-i){
h[i]=h[i]^h[max];
h[max]=h[max]^h[i];
h[i]=h[i]^h[max];
}
else
return;
}
}
int main(){
//citire
in=fopen("peste.in","rt");
fscanf(in,"%lld%lld%lld",&n,&k,&ttotal);
for(i=1;i<=n;i++){
fscanf(in,"%lld%lld",&p[i],&t[i]),h[i]=i;
u[t[i]]=1;
if(tmax<t[i])
tmax=t[i];
}
fclose(in);
//end
//calculez aux
m=n;
aux[tmax+1].nr=k;
for(i=n/2;i;i--)
heap(i);
for(;m;m--){
for(i=t[h[1]];aux[i].nr-k;i++)
aux[i].p+=p[h[1]],aux[i].nr++;
h[1]=h[m];
heap(1);
}
//end
//calculez best
for(i=1,l=0;i-ttotal-1;i++,l=0){
for(j=1;j-tmax-1&&j-i-1;j++)
if(l<aux[j].p+best[i-j]&&u[j]==1)
l=aux[j].p+best[i-j];
best[i]=l;
}
//end
//afisare
out=fopen("peste.out","wt");
fprintf(out,"%lld\n",best[ttotal]);
fclose(out);
//end
return 0;
}