#P2014. [CTSC1997] 选课

    ID: 958 远端评测题 1000ms 125MiB 尝试: 2 已通过: 1 难度: 6 上传者: 标签>动态规划,dp搜索1997WC/CTSC/集训队树形 dp

[CTSC1997] 选课

Description

At a university, to reach a certain number of credits, each student must choose some courses from many options. Among the courses, some must be taken before others; for example, Advanced Mathematics is always taken before other courses. Now there are NN courses, each with some credits, denoted as s1,s2,,sNs_1,s_2,\cdots,s_N. Each course has one direct prerequisite or none (if course aa is a prerequisite of course bb, then one can study course bb only after finishing course aa). A student needs to choose MM courses from these courses. What is the maximum total number of credits he can obtain?

The problem guarantees that the course arrangement has no conflicts. (That is, there is no situation where aa is a prerequisite of bb while bb is also a prerequisite of aa.)

Input Format

The first line contains two integers NN, MM separated by a space (1N300(1 \leq N \leq 300 , 1M300)1 \leq M \leq 300).

The next NN lines: the (i+1)(i+1)-th line contains two integers kik_i and sis_i, where kik_i is the direct prerequisite of course ii, and sis_i is the credits of course ii. If ki=0k_i=0, it means there is no direct prerequisite (0kiN(0 \leq {k_i} \leq N,1si20)1 \leq {s_i} \leq 20).

The testdata guarantees that there exists at least one ki=0k_i=0, i.e., there is at least one course without a prerequisite.

Output Format

Output a single line containing the maximum total credits when choosing MM courses.

7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
13

Hint

Translated by ChatGPT 5