C. Karen and Supermarket
 
 

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

 题意:

  给你n件商品,

  给出每件商品的 原价和 优惠券,用优惠券前必须使用第x件的优惠券才可以使用当前优惠券

  求b元下,最多买多少件商品

题解:

  暴力转移

  dp[now][x][0/1]表示当前节点now,买x件商品,当前now节点的商品是否使用优惠券的最小花费

  最后check一下就行

  先转移后更新是个优化

#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const int N = 5e3+, mod = , inf = 2e9; int n,b,c[N],d[N],siz[N],x[N];
LL dp[N][N][];
vector<int > G[N];
void dfs(int u,int f) {
siz[u] = ;
dp[u][][] = ;
dp[u][][] = c[u];
dp[u][][] = c[u] - d[u];
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) continue;
dfs(to,u);
for(int j = siz[u]; j >= ; --j) {
for(int k = siz[to]; k >= ; --k) {
dp[u][j+k][] = min(dp[u][j+k][], dp[u][j][] + dp[to][k][]);
dp[u][j+k][] = min(dp[u][j+k][], dp[u][j][] + min(dp[to][k][],dp[to][k][]));
}
}
siz[u] += siz[to];
}
}
int main() {
scanf("%d%d",&n,&b);
scanf("%d%d",&c[],&d[]);
for(int i = ; i <= n; ++i) {
scanf("%d%d%d",&c[i],&d[i],&x[i]);
G[x[i]].push_back(i);
}
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j) {
dp[i][j][] = INF;
dp[i][j][] = INF;
}
dfs(,);
int ans = ;
for(int i = ; i <= n; ++i)
if(min(dp[][i][],dp[][i][]) <= b) ans = i;
printf("%d\n",ans);
return ;
}

Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP的更多相关文章

  1. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  2. Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)

    http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...

  3. Codeforces Round #419 (Div. 2) C. Karen and Game

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

  4. Codeforces Round #419 (Div. 2) B. Karen and Coffee

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

  5. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  6. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  7. 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game

    容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...

  8. Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp

    题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...

  9. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

随机推荐

  1. 搭建Mysql主从复制

    mysql 主从复制流程图 Server version: 10.0.24-MariaDB-7 Ubuntu 16.04 Master 记录二进制文件 导出数据并记录二进制位置 导入数据,设置二进制位 ...

  2. maven添加本地jar包到本地仓库

    1 进入jar包所在文件夹,进入cmd命令 2 执行命令 mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -Dartif ...

  3. STM32F407 串口通信实验 视频第27节 个人笔记

    前言 第26节也是串口,笔记链接在此:https://www.cnblogs.com/YuQiao0303/p/10019362.html github地址:https://github.com/Yu ...

  4. 动手实操:如何用 Python 实现人脸识别,证明这个杨幂是那个杨幂?

    当前,人脸识别应用于许多领域,如支付宝的用户认证,许多的能识别人心情的 AI,也就是人的面部表情,还有能分析人的年龄等等,而这里面有着许多的难度,在这里我想要分享的是一个利用七牛 SDK 简单的实现人 ...

  5. 【数学】codeforces C. Maximal GCD

    http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...

  6. x1 carbon 扩展屏 模糊

    x1 carbon 扩展屏 模糊,扩展屏是dell的屏,分辨率最大是1920*1080, x1最大是2560*1440. 不论是通过DP mini转VGA,还是HDMI,输出都是模糊,只有复制屏幕的时 ...

  7. RedisDesktopManager 踩坑之旅

    虚拟机上装了redis, 本地Windows的RedisDesktopManager  connect failed. 解决方法: 1.修改 redis.conf 文件 bind 127.0.0.1 ...

  8. 2016 ACM/ICPC 区域赛(北京) E 题 bfs

    https://vjudge.net/problem/UVALive-7672 题意    输入一个五位数n 问由12345变到n的操作最少次数 不可达输出-1 有三种操作 1.交换相邻的位置 次数不 ...

  9. 2017年icpc西安网络赛 Maximum Flow (找规律+数位dp)

    题目 https://nanti.jisuanke.com/t/17118 题意 有n个点0,1,2...n-1,对于一个点对(i,j)满足i<j,那么连一条边,边权为i xor j,求0到n- ...

  10. Log4j的日志级别分析(转)

    说明:Log4j的日志是有级别的,从低到高顺序为:ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF,当定义了日志级别为 ...