Cod sursa(job #911601)
#include<fstream>
#include<set>
using namespace std;
int n,m,k,i,j,pas,poz;
struct oaie
{
unsigned long long dist,l;
};
oaie a[100001];
bool uz[100001],ok;
unsigned long long max1,sol,d;
int main()
{
ifstream f("lupu.in");
ofstream g("lupu.out");
f>>n>>m>>k;
for(i=1;i<=n;++i)
f>>a[i].dist>>a[i].l;
ok=1;
pas=sol=0;
while(ok)
{
ok=max1=0;
for(i=1;i<=n;++i)
{
d=pas*k+a[i].dist;
if(!uz[i]&&d>m-k&&d<=m&&a[i].l>max1)
max1=a[i].l,poz=i;
}
if(max1)
{
sol+=max1;
uz[poz]=1;
ok=1;
//g<<poz<<" "<<sol<<"\n";
}
++pas;
}
g<<sol;
}