Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)

E. Fire
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 tiseconds 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 ≤ 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 2seconds. 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 3seconds, but this item will already be completely burned by this time.

【题意】有n件物品,起火了,救每件物品需要花t[i]时间,且必须在d[i]时刻之前完成,救下之后价值为p[i],求最大价值。

【分析】背包吧,dp很好写,dp[i][j]表示在j时刻救下i所得最大价值,关键是记录路径,更新时,将pos[i][j]更新为1,否则更新为0.

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e2+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,s,ans,now=;
int dp[];
int pos[][];
struct man{
int t,d,p,id;
}a[];
bool cmp(const man&s,const man&t){
if(s.d==t.d)return s.t<t.t;
return s.d<t.d;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d%d",&a[i].t,&a[i].d,&a[i].p);
a[i].id=i;
}
sort(a+,a++n,cmp);
for(int i=;i<=n;i++){
if(a[i].t>a[i].d)continue;
for(int j=a[i].d-;j>=a[i].t;j--){
if(dp[j]<dp[j-a[i].t]+a[i].p){
dp[j]=dp[j-a[i].t]+a[i].p;
pos[a[i].id][j]=a[i].id;
}
else{
pos[a[i].id][j]=;
}
}
}
int maxn=,now=;
for(int i=;i<=;i++){
if(dp[i]>maxn){
maxn=dp[i];
now=i;
}
}
stack<int>s;
printf("%d\n",maxn);
for(int i=n;i>=;i--){
int id=a[i].id;
if(pos[id][now]){
s.push(id);
now-=a[i].t;
}
}
printf("%d\n",s.size());
while(!s.empty()){
printf("%d ",s.top());
s.pop();
}
return ;
}
上一篇:Android Volley源码分析


下一篇:codeforces 1272F dp+记录路径