f[i][j]的含义是在第i个月,在幸福度为j的情况下,能有的最多的钱。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int f[52][N];
void solve() {
int n, x, c, h;
cin >> n >> x >> c >> h;
memset(f + 1, -0x3f, sizeof(f[0]) * n);
f[1][0] = x;
int total = 0;
unordered_set<int> candidate[2];
candidate[1].insert(0);
if (c == 0) {
f[1][total = h] = x;
candidate[1].insert(h);
}
for (int i = 2; i <= n; ++i) {
cin >> c >> h;
total += h;
bool first_enter = true;
candidate[i & 1] = candidate[i - 1 & 1];
for (int j : candidate[i - 1 & 1]) {
f[i][j] = f[i - 1][j] + x;
if (f[i - 1][j] >= c) {
f[i][j + h] = max(f[i][j + h], f[i - 1][j] - c + x);
candidate[i & 1].insert(j + h);
total = max(total, j + h);
}
}
}
for (int j = total; j + 1; j--)
if (f[n][j] >= 0) {
cout << j << "\n";
return;
}
cout << "0\n";
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
#else
cin.tie(0)->sync_with_stdio(0);
#endif
int t;
cin >> t;
while (t--)
solve();
return 0;
}