#P2515. [HAOI2010] 软件安装

    ID: 1530 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp2010河南各省省选强连通分量,缩点Tarjan

[HAOI2010] 软件安装

Description

We have NN software packages. For a software ii, it occupies WiW_i units of disk space, and its value is ViV_i. We want to choose some software to install on a computer with disk capacity MM, so that the total value of the installed software is as large as possible (i.e., the sum of ViV_i is maximized).

However, there is a problem: there are dependency relationships between software. Software ii can work properly only if software jj (including software jj’s direct or indirect dependencies) is installed (software ii depends on software jj). Fortunately, a software depends on at most one other software. If a software cannot work properly, then its contribution is 00.

We already know the dependencies between software: software ii depends on DiD_i. Please design a plan to install software so that the total value is as large as possible. Each software can be installed at most once. If a software has no dependency, then Di=0D_i=0; in this case, as long as this software is installed, it will work properly.

Input Format

Line 1: N,M(0N100,0M500)N,M(0\leq N\leq 100, 0\leq M\leq 500)

Line 2: W1,W2,...Wi,...,Wn(0WiM)W_1,W_2, ... W_i, ..., W_n (0\leq W_i\leq M)

Line 3: V1,V2,...,Vi,...,Vn(0Vi1000)V_1, V_2, ..., V_i, ..., V_n (0\leq V_i\leq 1000)

Line 4: $D_1, D_2, ..., D_i, ..., D_n (0\leq D_i\leq N, D_i≠i)$

Output Format

An integer representing the maximum total value.

3 10
5 5 6
2 3 4
0 1 1
5

Hint

Translated by ChatGPT 5