ural1018

maksyuki 发表于 oj 分类,标签:
0

1018. Binary Apple Tree

Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by integers the root of binary apple tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to N, where N is the total number of all enumerated points. For instance in the picture below N is equal to 5. Here is an example of an enumerated tree with four branches:

InputAs you may know it's not convenient to pick an apples from a tree when there are too much of branches. That's why some of them should be removed from a tree. But you are interested in removing branches in the way of minimal loss of apples. So your are given amounts of apples on a branches and amount of branches that should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.

First line of input contains two numbers: N and Q (2 ≤ N ≤ 100; 1 ≤ Q ≤ N − 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. NextN − 1 lines contains descriptions of branches. Each description consists of a three integer numbers divided by spaces. The first two of them define branch by it's ending points. The third number defines the number of apples on this branch. You may assume that no branch contains more than 30000 apples.

Output

Output should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root 😉

Sample

input output
5 21 3 11 4 10

2 3 20

3 5 20

21

 

题目类型:树形DP

算法分析:dp[i][j]表示以i为根的子树恰好保留j个分支所具有的最多苹果数量,初始化dp全为0。最后直接输出dp[1][m]即可,其中m为保留的分支的个数。状态转移方程为:dp[u][i] = max (dp[u][i], dp[u][i-k] + dp[v][k-1] + w),其中v为u的子节点,w为u-v这条边上的苹果数