Gym 100694 A. Did he drop any good loot? 背包 dp

A. Did he drop any good loot?

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Slava plays a very cool RPG. He has just cleaned up the dungeon, and n cool artifacts have been dropped down. He wants to sell them to gain as much profit as he can. Each artifact has its own price pi and weight wi. Slava cannot carry artifacts with the total weight greater than m, but it's possible to activate some of them. Being activated, the i-th artifact increases Slava's strength and adds additional weight dito Slava's weight limit. Activated artifacts don't disappear, their price and weight don't change and they still can be sold. Slava can activate no more than 2 artifacts at the same time.

More formally, Slava wants to choose a set of artifacts with the greatest total price among all correct sets. The set of artifacts i1, i2, ..., ik(k ≤ n) is called correct if wi1 + wi2 + ... + wik ≤ m + s, where s is the additional weight limit gained from the actifacts activation:s = dj1 + dj2, if Slava activated artifacts j1 and j2 (j1 ≠ j2), s = dj — if he activated only artifact j, and s = 0, if he didn't activate any artifacts (j1, j2 and j must be contained in the set of chosen artifacts i1, i2, ..., ik).

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 104, 1 ≤ m ≤ 500) — the number of artifacts and the Slava's weight limit.

Each of the next n lines contains three space-separated integers piwi and di (1 ≤ pi ≤ 105, 1 ≤ wi ≤ 100, 0 ≤ di ≤ 100) — the price of the i-th artifact, its weight and the additional weight limit it gains at its activation.

Output

Write one integer — the maximum total price of the artifacts Slava can take with him.

Examples

input

5 10
1 5 3
2 4 0
3 2 2
4 1 4
5 3 1

output

15

input

3 10
100 100 20
200 80 30
300 60 40

output

0

Note

In the first sample Slava cannot just take all artifacts — his weight limit is 10, and the total weight of all artifacts is 15. That's why he activates artifacts 1 and 4, which leads to increasing his weight limit to 10 + 3 + 4 = 17 (in fact, he can activate any pair of artifacts which increases his weight limit by 5 or more).

In the second sample Slava cannot take any artifacts, either with the activation or without it.


题意:

       给你n个物品,背包容量m。每个物品有价值,重量,和取得这个物品获得的额外的背包容量,最多可以选择两个物品获得其额外的背包容量,问最后可以获得的最大价值。(如果想获得这个额外容量,一定要先可以取这个物品)

 

做法:

       反正自己是做不出来,机房里的大大给我们讲的dp。dp[i][j][k],i是滚动数组,表示处理到第i个物品,j表示的是当前的重量,k表示已经得到了k个物品的额外容量,dp表示此时最大值,转移过程在代码中解释。


代码如下:

 

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
using namespace std;
int dp[2][1150][3];   //  因为可能会出现负值(先占用了之后会用额外背包填回来的容量)
int n,m,add,wei,val;  //  所以以300作为分界线,dp[i][300][0]其实是此时重量为0的情况。
int main(){
    cin>>n>>m;
    int now=0;
    mem(dp[now^1],-inf);  //  初始化,都是最小值
    dp[now^1][300][0]=0;   //重量为0时价值为0
    int ans=0;
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&val,&wei,&add);
        mem(dp[now],-inf);
        for(int j=0;j<=1100;j++){
            if(j-wei>=0){  //假设我们不用这个背包的额外容量,那么状态只在同样的k间转移
                dp[now][j][0]=max(dp[now^1][j][0],dp[now^1][j-wei][0]+val);
                dp[now][j][1]=max(dp[now^1][j][1],dp[now^1][j-wei][1]+val);
                dp[now][j][2]=max(dp[now^1][j][2],dp[now^1][j-wei][2]+val);
            }              //假设我们用了额外的容量,那么k要加1,和原先不用add的dp取最大值。
            if(j-wei+add>=0&&j-wei+add<=1100){
                dp[now][j][1]=max(dp[now][j][1],dp[now^1][j-wei+add][0]+val);
                dp[now][j][2]=max(dp[now][j][2],dp[now^1][j-wei+add][1]+val);
            }
            if(j>0){       //可能会出现一些特殊情况,为什么呢我也不知道。
                dp[now][j][0]=max(dp[now][j][0],dp[now][j-1][0]);
                dp[now][j][1]=max(dp[now][j][1],dp[now][j-1][1]);
                dp[now][j][2]=max(dp[now][j][2],dp[now][j-1][2]);
            }              //答案找最大值。
            if(j<=m+300) ans=max(ans,max(dp[now][j][0],max(dp[now][j][1],dp[now][j][2])));
        }
        now^=1;
    }
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自2020年以来,自动驾驶汽车(Autonomous Vehicle)在这一领域取得了显著进展。 自动驾驶汽车通过使用先进的传感技术,如雷达、激光雷达和摄像头,以及深度学习和人工智能算法,实现了车辆的自主驾驶。它们能够感知周围环境,了解道路状况,并做出相应的决策和行驶动作,从而实现无需人类操控的车辆行驶。 自动驾驶汽车在2020年迅速崭露头角。它们的技术日益成熟,不断的实验和测试表明其在安全性和性能方面已经取得了显著的突破。虽然仍然存在一些挑战,比如在复杂城市环境中导航和处理紧急情况,但这些问题正经过不断的研究和改进来得以解决。 在未来,自动驾驶汽车有望在各个领域发挥重要作用。首先,它们将可以提高道路交通的安全性。由于自动驾驶车辆不受人类司机的疲劳、分心和驾驶误差的限制,它们的驾驶能力更为稳定和准确。其次,自动驾驶汽车还能够提高交通效率。通过与其他车辆实时通信和协同,它们可以避免交通堵塞和减少事故发生,从而减少交通拥堵和行车时间成本。 此外,自动驾驶汽车也将为交通出行带来便利和舒适性。乘客可以更轻松地进行其他活动,如工作、休息或娱乐,而不必担心驾驶问题。老年人和残障人士也将能够自由独立地出行,提高他们的生活质量。 综上所述,作为2020年的重要趋势,自动驾驶汽车具有广阔的应用前景。通过不断的创新和发展,它们将在道路交通安全、交通效率和出行体验方面取得进一步的提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值