Codeforces 862 E. Fire (01背包)

网友投稿 305 2022-08-31

Codeforces 862 E. Fire (01背包)

Description

Polycarp 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 + tb seconds 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 ≤ 2000, 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

33 7 42 6 53 7 6

Examples output

1122 3

题意

有 n 个文件,第 i 个文件拯救它需要 ti 时间,该文件会在 di 时间自动销毁,它的价值是 pi

思路

简单 01 背包问题

dp[i][j] 代表对于前 i 件物品在第 j

则当 ti>j 或 j>=di 时,当前物品无法被选择,因此 dp[i][j]=dp[i−1][j]

对于其他时间: dp[i][j]=max(dp[i−1][j],dp[i−1][j−ti]+pi)

顺便记录一下路径就好了

AC 代码

#includeusing namespace std;typedef __int64 LL;const int maxn = 110;int n;int dp[maxn][maxn*25];bool path[maxn][maxn*25];struct node{ int t,d,p,id; bool operator<(const node &x) { return d=a[i].d) dp[i][j] = dp[i-1][j]; else { dp[i][j] = dp[i-1][j]; if(dp[i-1][j-a[i].t]+a[i].p>dp[i][j]) { path[i][j] = true; dp[i][j] = dp[i-1][j-a[i].t]+a[i].p; } } if(dp[i][j]>=maxx) { maxx = dp[i][j]; maxj = j; } } } vector ans; for(int i=n-1,j=maxj; i>=0; i--) { if(path[i][j]) { ans.push_back(i); j-=a[i].t; } } cout<=0; i--) cout<>n; for(int i=0; i>a[i].t>>a[i].d>>a[i].p,a[i].id = i+1; sort(a,a+n); solve(); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:王者荣耀女子公开赛成都站RE-Girls夺冠!(成都女子电竞大赛)
下一篇:codevs 1036 商务旅行 (LCA)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~