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])

注意初始化和边界调节:

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

虽然是n^3但能过,就没必要打多叉树转二叉树

转载自YZH神犇%%%%OTTTTTZ

http://www.cnblogs.com/Yuzao/p/7074373.html

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lol;
struct Node
{
int next,to;
}edge[];
int head[],num,n,size[];
lol b,w[],d[],v[],f[][][];
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void dfs(int x)
{int i,j,k;
f[x][][]=;
f[x][][]=v[x];
f[x][][]=w[x];
size[x]=;
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
dfs(v);
for (j=size[x];j>=;j--)
{
for (k=;k<=size[v];k++)
{
f[x][j+k][]=min(f[x][j+k][],f[x][j][]+f[v][k][]);
f[x][j+k][]=min(f[x][j+k][],f[v][k][]+f[x][j][]);
f[x][j+k][]=min(f[x][j+k][],f[v][k][]+f[x][j][]);
}
}
size[x]+=size[v];
}
}
int main()
{int i,fa;
cin>>n>>b;
scanf("%I64d%I64d",&w[],&d[]);
v[]=w[]-d[];
for (i=;i<=n;i++)
{
scanf("%I64d%I64d%d",&w[i],&d[i],&fa);
v[i]=w[i]-d[i];
add(fa,i);
}
memset(f,/,sizeof(f));
dfs();
for (i=n;i>=;i--)
if (f[][i][]<=b||f[][i][]<=b)
{
cout<<i<<endl;
return ;
}
}

codeforces 815C Karen and Supermarket的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  2. Codeforces 815C. Karen and Supermarket【树形DP】

    LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...

  3. CodeForces 816E Karen and Supermarket ——(树形DP)

    题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...

  4. 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 ...

  5. CodeForces 816B Karen and Coffee(前缀和,大量查询)

    CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...

  6. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

  7. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

  8. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

  9. 【Codeforces 815C】Karen and Supermarket

    Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...

随机推荐

  1. C语言函数嵌套调用作业总结

    成绩管理系统作业小结 devc中出现"ID Return"问题,我帮很多同学看完后发现,大部分还是自身程序问题,如全局变量大小写错误.没有添加c文件进工程等等.所以对于" ...

  2. mongodb 高级操作

    聚合 aggregate 聚合(aggregate)主要用于计算数据,类似sql中的sum().avg() 语法 db.集合名称.aggregate([{管道:{表达式}}]) 管道 管道在Unix和 ...

  3. SDVN

    Software Defined Vehicular Networks VANET 车载自组网(VANET)是指在交通环境中车辆之间.车辆与固定接入点之间及车辆与行人之间相互通信组成的开放式移动Ad ...

  4. openfalcon

    一.环境准备 操作系统:centos7(minimal,www.centos.org下载的包是CentOS-7-x86_64-Minimal-1611.iso) 1.1 更换阿里yum(个人习惯) 步 ...

  5. JS判断不同操作系统显示不同样式css

    <script type="text/javascript"> var system ={}; var p = navigator.platform; //判断是否为P ...

  6. MySQL 主从复制那些事(一)

    本部分主要以理论为主,具体的主从搭建环境,大家可以参考博客其他部分.下面我就给大家数说主从复制那些理论的东西.说的不一定都是正确的,有不同出入的地方,欢迎大家一起交流沟通,以下我把我自己整理出来的主从 ...

  7. mysql(3)—— 内连接、外连接的区别

    先来看一下,内连接的语法: SELECT  XXX FROM XXX INNER JOIN XXX ON XXX; 这里 INNER 可以省略,在上一篇博客中我们对于笛卡尔积现象的研究中(http:/ ...

  8. mysql Access denied for user root@localhost错误解决方法

    select * from user \G use mysql select * from user limit 1 \G update user set Host='%' where `User`= ...

  9. 关于css的层叠上下文和层叠顺序问题

    关于css的层叠上下文和层叠样式问题 最近在项目中遇到了一个让我欲仙欲死的问题,我给项目中的图片设置了一个淡入效果,几opacity变化,但当我在它的上面有一个定位元素时,动画结束后,定位元素居然被遮 ...

  10. linux下git常用命令

    1 安装: sudo apt-get install git 2 创建一个版本库: 新建一个文件夹,进入这个目录之后 git init 3 创建一个版本: git add 文件名或目录 #表示将当前文 ...