1 条题解

  • 1
    @ 2026-9-16 17:32:42
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int n, L;
        cin >> n >> L;
        vector<int> dp(L + 1, 0);
    
        for(int i = 0; i < n; ++i)
        {
            int a, b, c;
            cin >> a >> b >> c;
    
            //二进制拆分多重背包
            int cnt = c;
            int k = 1;
            while(cnt > 0)
            {
                int take = min(k, cnt);
                int w = take * a;
                int val = take * b;
                //01背包逆序
                for(int j = L; j >= w; j--)
                {
                    dp[j] = max(dp[j], dp[j - w] + val);
                }
                cnt -= take;
                k *= 2;
            }
        }
        cout << dp[L] << endl;
        return 0;
    }
    
    
    
    • 1

    信息

    ID
    2319
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    154
    已通过
    24
    上传者