題目大意:
分析:
代碼:
#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--)
#define N 1000005
using namespace std;
typedef long long ll;
typedef double db;
struct Node {
int w, v;
}a[N];
ll f[2][N], ans, tot, m;
int val[105], maxv, n, usev;
db cmax = -1;
void read(int &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); }
x = x * f;
}
int main() {
freopen("backpack.in", "r", stdin);
freopen("backpack.out", "w", stdout);
read(n); scanf("%lld", &m);
rep(i, 1, n) read(a[i].v), read(a[i].w), maxv = max(maxv, a[i].v);
rep(i, 1, n) val[a[i].v] = max(val[a[i].v], a[i].w);
rep(i, 1, maxv) {
db now = (db)val[i] / i;
if (now > cmax) cmax = now, usev = i;
}
tot = (ll)(m / usev) - 100;
if (tot > 0)
m -= 1ll * tot * usev, ans += 1ll * tot * val[usev];
int id = 0;
rep(i, 1, maxv) {
id ^= 1;
rep(j, 0, i - 1) f[id][j] = f[id ^ 1][j];
rep(j, i, m) f[id][j] = max(f[id][j - i] + val[i], f[id ^ 1][j]);
}
printf("%lld\n", f[id][m] + ans);
return 0;
}