1 solutions

  • 0
    @ 2025-11-27 11:59:02

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, L, c[509], l[509];
    int f[2009]; //f[j]:买总容量不低于L的饮料的最小花费
    
    int main()
    {
        cin >> n >> L; // n种饮料   总容量不低于L
        for (int i = 1; i <= n; i++)
            cin >> c[i] >> l[i]; // 每一种饮料的售价和容量
        
        memset(f , 0x3f , sizeof(f));
        f[0] = 0;
    
        for(int i = 1; i <= n; i++)
        {
            for(int j = L; j >= 0; j--)
                f[j] = min(f[j] , f[max(0 , j - l[i])] + c[i]);
        }
        
        if(f[L] != 0x3f3f3f3f)
            cout << f[L];
        else
            cout << "no solution";
        return 0;
    }
    
    
    • 1

    Information

    ID
    120
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By