跳到正文

个人记录

666

dl_001约 4 分钟
原文

这是 2026年7月11日 存档的第 6 版,不是当前版本。查看当前版本

文章信息与版本

#include <iostream> #include <algorithm> #include <cmath> #include <set> #include <vector> #include <stack> using namespace std; long long n, s, dp[600][600], ans; struct node { long long w, x; }a[600]; bool cmp(node s1, node s2) { if (s1.w == s2.w) { return s1.x < s2.x; } return s1.w < s2.w; } int path[600][600]; vector<int> sel; stack<int> st; int main() { cin >> n >> s; for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j] = 0; } } for (int i = 1; i <= n; i++) { cin >> a[i].w; } for (int i = 1; i <= n; i++) { cin >> a[i].x; } sort(a + 1, a + n + 1, cmp); for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { path[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { if (a[i].x > j) { dp[i][j] = dp[i - 1][j]; path[i][j] = 0; } else { if (dp[i - 1][j] >= dp[i - 1][j - a[i].x] + a[i].w) { dp[i][j] = dp[i - 1][j]; path[i][j] = 0; } else { dp[i][j] = dp[i - 1][j - a[i].x] + a[i].w; path[i][j] = 1; } } } } int i = n, j = s; while (i > 0 && j >= 0) { if (path[i][j] == 1) { sel.push_back(i); j -= a[i].x; } i—; } reverse(sel.begin(), sel.end()); long long last_pos = -1; for (int idx : sel) { long long cur = a[idx].w; if (last_pos >= cur) { cur = last_pos + 1; } ans += cur; last_pos = cur; } cout << ans; return 0; }