P4377 USACO18OPEN Talent Show G

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
#include<bits/stdc++.h>
using namespace std;
const int MAXn = 250;
const int MAXW = 1e3;
const double MAXfrac = 1e6;
const double EPS = 1e-8;

template <typename T>
inline void read(T &a) {
register char c;while (c = getchar(), c < '0' || c > '9');register T x(c - '0');while (c = getchar(), c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);}a = x;
}
template <typename T, typename ...Argv>
inline void read(T &n, Argv &...argv) {
read(n), read(argv...);
}

int n, W, w[MAXn + 10], t[MAXn + 10]; double v[MAXn + 10];
double d[MAXW + 10];
double calc(double x) {
for (int i = 1; i <= n; ++i) {
v[i] = (double)t[i] - (double)w[i] * x;
}
memset(d, 0xc2, sizeof(d));
d[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = W; ~j; --j) {
int k = min(j + w[i], W);
d[k] = max(d[k], d[j] + v[i]);
}
}
return d[W];
}

signed main() {
read(n, W);
for (int i = 1; i <= n; ++i) {
read(w[i], t[i]);
}
double L = 0, R = MAXfrac;
while (R - L > EPS) {
double mid = (L + R) / 2;
if (calc(mid) > 0) {
L = mid;
} else {
R = mid;
}
}
printf("%d\n", int(L * 1000));
}