zoukankan      html  css  js  c++  java
  • 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:
    2   5
      / 
      3   4
        /
        1
    
    As 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.

    Input

    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

    inputoutput
    5 2
    1 3 1
    1 4 10
    2 3 20
    3 5 20
    
    21
    
    Problem Source: Ural State University Internal Contest '99 #2 
  • 相关阅读:
    【第3版emWin教程】第41章 emWin6.x窗口管理器基础知识(重要)
    【第3版emWin教程】第40章 emWin6.x支持的颜色格式
    【第3版emWin教程】第39章 emWin6.x指针输入设备(摇杆)
    H7-TOOL迎来新版固件V2.08,Modbus助手,RTT波形展示和时间戳上线,新增美仁半导体,NXP MKE系列,华大F460系列等脱机烧录支持
    【第3版emWin教程】第38章 emWin6.x多任务设计
    嵌入式新闻早班车-第27期
    【第3版emWin教程】第37章 emWin6.x抗锯齿
    【STM32H7的DSP教程】第49章 STM32H7的自适应滤波器实现,无需Matlab生成系数(支持实时滤波)
    【STM32F429的DSP教程】第49章 STM32F429的自适应滤波器实现,无需Matlab生成系数(支持实时滤波)
    【STM32F407的DSP教程】第49章 STM32F407的自适应滤波器实现,无需Matlab生成系数(支持实时滤波)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3229841.html
Copyright © 2011-2022 走看看