Fire(codefroces 864 E)

                                                  http://codeforces.com/problemset/problem/864/E

olycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tbseconds after fire started.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.

Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.

Examples

Input

3
3 7 4
2 6 5
3 7 6

Output

11
2
2 3 

Input

2
5 6 1
3 3 5

Output

1
1
1 

Note

In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.

In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.

 

题意:房子起火了,房子里面有n物品,每件物品编号为i。救出每件物品需要花费ci的时间,如果这件物品从开始起经过ti的时间还没有被救出的话,这件物品就会被彻底烧坏,每件物品的价值为vi。问,你最多可以救出价值为多少的物品,有几样?编号分别是多少?

思路:一个背包问题,先按物品被烧坏的时间升序排序。价值是重量,时间是容量。每个物品两种情况,即救或者不救,如果选择救的话,必须还要满足当前时间小于该物品被烧坏的时间。 输出物品的个数即编号的话,在每次选择抢救该物品时,记一个标记,vis[i][j],表示在花费时间为j的情况下,可以救出i物品。最后逆推时,只需从时间最大的开始,如果vis[i][j].表示在这个时间下,i可以被抢救出,那么最大时间b减去该个物品所花费的时间,继续逆推。

 

#include "iostream"
#include "cstring"
#include "algorithm"
#include "vector"
#include "cstdio"
using namespace std;
int dp[105][2005],vis[105][2005];
int n;
struct node
{
    int c,t,v,index;
}a[1500];
vector<int >ans;
int cmp(node a,node b)
{
    return a.t<b.t;
}
int main()
{
    int b;
    cin>>n;
    for(int i=1;i<=n;i++) {cin>>a[i].c>>a[i].t>>a[i].v;a[i].index=i;}
    sort(a+1,a+1+n,cmp);
    memset(dp,0,sizeof(dp));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        b=0;
        for(int j=1;j<=2000;j++){
            if(j>=a[i].c&&j<a[i].t&&dp[i-1][j]<dp[i-1][j-a[i].c]+a[i].v){
                    dp[i][j]=dp[i-1][j-a[i].c]+a[i].v;
                    vis[i][j]=1;//点i满足条件,故标记,方便逆推。
            }
            else  dp[i][j]=dp[i-1][j];
            if(dp[i][j]>dp[i][b]) b=j;//求出价值最时所花费的时间
        }
    }
    cout<<dp[n][b]<<endl;
    vector<int> ans;
    for(int i=n;i;i--){
        if(vis[i][b]){
            ans.push_back(a[i].index);
            b-=a[i].c;
        }
    }
    cout<<ans.size()<<endl;
    for(int i=ans.size()-1;i>=0;i--) cout<<ans[i]<<" ";
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值