Editorial for 第2回卬高杯 E問題 - Kougakubu M@ster


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: admin

Python

Python3

n,h,r=map(int,input().split())
c=h+n*r+1
d=[[-10000000000]*(c)for i in range(n+1)]
d[0][h]=0
ans=0
for k in range(n):
    p0,a0,p1,a1,p2,a2=map(int,input().split())
    for i in range(c):
        if d[k][i]<0:continue
        if i-a0>-1:d[k+1][i-a0]=max(d[k+1][i-a0],d[k][i]+p0)
        else:ans=max(ans,d[k][i]+p0)
        if i-a1>-1:d[k+1][i-a1]=max(d[k+1][i-a1],d[k][i]+p1)
        else:ans=max(ans,d[k][i]+p1)
        if i-a2>-1:d[k+1][i-a2]=max(d[k+1][i-a2],d[k][i]+p2)
        else:ans=max(ans,d[k][i]+p2)
        if i+r<c:d[k+1][i+r]=d[k][i]
print(max(ans,max(d[n])))

C++

C++20

#include <ext/alloc_traits.h>
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    int n, h, r;
    std::cin >> n >> h >> r;
    int p[n][3], a[n][3];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 3; j++)
            std::cin >> p[i][j] >> a[i][j];
    int m = h + n * r;
    std::vector<std::vector<long>> dp(n + 1, std::vector<long>(m + 1, -2e18));
    dp[0][h] = 0;
    long ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                if (j >= a[i][k])
                    dp[i + 1][j - a[i][k]] = std::max(dp[i + 1][j - a[i][k]], dp[i][j] + p[i][k]);
                else
                    ans = std::max(ans, dp[i][j] + p[i][k]);
            }
            if (j + r <= m)
                dp[i + 1][j + r] = std::max(dp[i + 1][j + r], dp[i][j]);
        }
    }
    for (int i = 0; i <= m; i++)
        ans = std::max(ans, dp[n][i]);
    std::cout << ans << std::endl;
}

Comments

There are no comments at the moment.