On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.

The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.

Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.

There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).

Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?

Input

The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.

The next n lines describe the items. Specifically:

  • The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
  • If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
Output

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples
input
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output
4
input
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output
5
Note

In the first test case, Karen can purchase the following 4 items:

  • Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
  • Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
  • Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
  • Buy the sixth item for 2 dollars.

The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.

In the second test case, Karen has enough money to use all the coupons and purchase everything.

题解:

树上背包哈,F[x][j][0/1]表示x子节点和本身中,选j个,当前节点是否打折(0/1)

方程式:

F[x][j+k][0]=min(F[x][j+k][0],F[u][k][0]+F[x][j][0])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][1]+F[x][j][1])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][0]+F[x][j][1])

复杂度O(n^2).

注意初始化和边界调节:

F[x][0][0]是要赋为0的,因为当前节点不打折时是可以不选的,而F[x][0][1]不能.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
int n,m;
int head[N],num=,w[N],h[N],now[N];ll F[N][N][];
struct Lin
{
int next,to;
} a[N<<];
void init(int x,int y)
{
a[++num].next=head[x];
a[num].to=y;
head[x]=num;
}
void dfs(int x)
{
int u;
now[x]=;
F[x][][]=;
F[x][][]=w[x];F[x][][]=h[x];
for(int i=head[x]; i; i=a[i].next)
{
u=a[i].to;
dfs(u);
for(int j=now[x];j>=;j--)
{
for(int k=; k<=now[u]; k++)
{
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
}
}
now[x]+=now[u];//now为当前节点的子节点个数.
}
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,fa;
scanf("%d%d",&w[],&h[]);
h[]=w[]-h[];
memset(F,/,sizeof(F));
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&x,&y,&fa);
w[i]=x;
h[i]=x-y;
init(fa,i);
}
dfs();
for(int i=n; i>=; i--)
if(F[][i][]<=m || F[][i][]<=m)
{
printf("%d",i);
break;
}
return ;
}

codeforces round #419 E. Karen and Supermarket的更多相关文章

  1. Codeforces Round #419 D. Karen and Test

    Karen has just arrived at school, and she has a math test today! The test is about basic addition an ...

  2. codeforces round #419 C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  3. codeforces round #419 B. Karen and Coffee

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  4. codeforces round #419 A. Karen and Morning

    Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...

  5. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  6. Codeforces Round #419 (Div. 2) A-E

    上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...

  7. Codeforces round 419 div2 补题 CF 816 A-E

    A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...

  8. Codeforces Round #419

    A Karen and Morning 找最近的回文时间 模拟  往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...

  9. Codeforces Round #419 (Div. 1) (ABCD)

    1. 815A Karen and Game 大意: 给定$nm$矩阵, 每次选择一行或一列全部减$1$, 求最少次数使得矩阵全$0$ 贪心, $n>m$时每次取一列, 否则取一行 #inclu ...

随机推荐

  1. 解决python中flask_sqlalchemy包安装失败的问题

    在进行flask_sqlalchemy包的下载安装时出现以下问题: 由图片可看出是编码转换出了问题,找到pip\compat_init_.py文件,打开它并查看第73行,将代码做如下更改并保存: 问题 ...

  2. 2017-2018-1 1623 bug终结者 冲刺002

    bug终结者 冲刺002 by 20162329 张旭升 今日冲刺任务: 能够显示主菜单和功能 游戏需要提供主菜单让玩家进行游戏设置,同时能能够把地图文件中的信息转换成为图像显示到游戏界面上 能够实现 ...

  3. 201621123057 《Java程序设计》第1周学习总结

    1.本周学习总结 .java - - 源程序 .class - - 字节码文件 JVM - - 虚拟机 JRE - - 执行环境 JDK - - 开发工具包 其中,运行的是.class,而非.java ...

  4. verilog学习笔记(3)_task/case小例子及其tb

    module ex_case `timescale lns/1ns module ex_case( input wire rst_n, input wire sclk, output reg [7:0 ...

  5. Flask Markup 上下文,request

    在模板渲染中,使用Markup转换变量中的特殊字符 from flask import Markup Markup函数对字符串进行转移处理再传递给render_template()函数 在浏览器中显示 ...

  6. CentOS7安装配置iptables防火墙

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761 CentOS7默认的防火墙不是iptables,而是firewall ...

  7. 一个页面多个HTTP请求 页面卡顿!

    用promise解决 前两天面试的时候 一个面试官问到这样一个问题 这里先说出解决的路径 这几天会更新具体的做法 或者直接参考廖雪峰大神 地址如下: https://www.liaoxuefeng.c ...

  8. 让linux远程主机在后台运行脚本

    后台挂起:python xxx.py & 在脚本命令后面加入"&"符号就可以后台运行.结束进程:kill -9 sidps -ef | grep ... 查询sid

  9. java程序员最不愿意看到的十件事

     0.遍历结果集并构造对象如果你是个时髦的开发者而不是专业人员,显然你从某篇博客中读过有开发者遇到Hibernate的“性能问题”,因而认为ORM都不好,觉得手动编码“明显更好”.喜欢的话你当然可以用 ...

  10. kubernetes入门(10)kubernetes单机安装后 - helloworld

    前言 查看端口是否被监听了 ::netstat -tlp |grep 31002 我是用的yum install etcd kubernetes docker vim, 这样装的是1.5.2,不是最新 ...