Pagini recente » Cod sursa (job #2587831) | Cod sursa (job #758900) | Cod sursa (job #2756789) | Cod sursa (job #1723150) | Cod sursa (job #163558)
Cod sursa(job #163558)
#include <stdio.h>
#include <algorithm>
using namespace std;
long t, i, j, n, k, a, b, nx;
struct lol
{
long a, b;
double c;
};
lol x[50010];
int cmpf(lol a, lol b)
{
return a.c > b.c;
}
long abss(long a)
{
return a < 0 ? -a : a;
}
long valid(long rez)
{
long i, j, sum = 0, kc, tc = t;
while (tc && sum < rez)
{
i = 1;
while (tc < x[i].b && i <= n + 1)
++i;
if (i == n + 1)
return sum >= rez;
kc = 1;
sum += x[i].a;
for (j = i + 1; j <= n && kc < k; j ++)
if (x[j].b <= x[i].b)
{
kc ++;
sum += x[j].a;
}
tc -= x[i].b;
}
return sum >= rez;
}
long cautbin(long st, long dr)
{
long m = (st + dr) / 2;
if (abss(st - dr) <= 1)
return valid(dr) ? dr : st;
if (valid(m))
return cautbin(m, dr);
return cautbin(st, m - 1);
}
int main()
{
freopen ("peste.in", "rt", stdin);
freopen ("peste.out", "wt", stdout);
scanf("%ld %ld %ld", &n, &k, &t);
for (i = 1; i <= n; ++i)
{
scanf("%ld %ld", &a, &b);
if (b <= t)
{
x[++nx].a = a;
x[nx].b = b;
x[nx].c = (double) x[nx].a / x[nx].b;
}
}
n = nx;
sort(x + 1, x + n + 1, cmpf);
printf("%ld\n", cautbin(0, 500000000));
return 0;
}