ARC223A
aaa_Pigeon · · 题解
考虑枚举第一个可以选但不选的
那么
#include <bits/stdc++.h>
using namespace std;
const int N = 60;
int n;
long long w[N + 10], v[N + 10], W;
void work() {
cin >> n >> W;
for (int i = 1; i <= n; ++i) cin >> w[i] >> v[i];
long long ans = 0, pre = 0, sum = 0, tot = 0;
for (int i = 1; i <= n; ++i) pre += v[i];
for (int i = n; i >= 1; --i) {
pre -= v[i];
if (sum + w[i] > W) continue;
ans = max(ans, tot + pre);
sum += w[i], tot += v[i];
}
cout << max(ans, tot) << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T; cin >> T;
while (T--) work();
return 0;
}