1 solutions

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

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long 
    void read(int &p)
    {
        p = 0;
        int k = 1;
        char c = getchar();
        while(c < '0' || c > '9')
        {
            if(c == '-')
            {
                k = -1;
            }
            c = getchar();
        }
        while(c >= '0' && c <= '9')
        {
            p = p * 10 + c - '0';
            c = getchar();
        }
        p *= k;
        return ;
    }
    void write_(int x)
    {
        if(x < 0)
        {
            putchar('-');
            x = -x;
        }
        if(x > 9)
        {
            write_(x / 10);
        }
        putchar(x % 10 + '0');
    }
    void writesp(int x)
    {
        write_(x);
        putchar(' ');
    }
    void writeln(int x)
    {
        write_(x);
        puts("");
    }
    int n,m,a[1111],b[55555];
    int dp[51111];
    signed main()
    {
        read(n),read(m);
        for(int i = 1;i <= m;i++)
        {
            read(a[i]);
        }
        for(int i = 0; i < n;i++)
        {
            read(b[i]);
        }
        memset(dp,0xcf,sizeof(dp));
        dp[0] = b[0];
        for(int i = 0;i < n;i++)
        {
            for(int j = 1;j <= m;j++)
            {
                dp[min(n,i + a[j])] = max(dp[min(n,i + a[j])],dp[i] + b[min(n,i + a[j])]);
            }
        }
        writeln(dp[n]);
        return 0;
    }
    
    • 1

    Information

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