Codeforces 862 E. Fire (01 Backpack) __ Knapsack problem

Source: Internet
Author: User
Description

Polycarp is in really serious trouble-his house are 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, his estimated the value of di-the moment after which the item I'll be completely burned and 'll no longer is valuable for him in all. In particular, if ti≥di, then i-th item cannot to be saved.

Given The values pi for each of the items and find a set of items that Polycarp can save such that's total value of this it EMS is maximum possible. Polycarp saves the items one after another. For example, if he takes item A, and then item B, then the item a would be saved in ta seconds, and the item b-in t A + TB seconds after fire started.

Input

The 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 need Ed to save the item I, the time after which the item I'll burn completely and the value of item I.

Output

In the the ' 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. The 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

examples Output

2
2 3

the

There are n n files, the first I files save it needs Ti t_i time, the file will be automatically destroyed in Di d_i time, its value is pi p_i, ask ultimately can save the maximum value, and output path.

train of Thought

Simple 01 knapsack problem

DP[I][J] Dp[i][j] represents the maximum value that can be saved for the first I I items in J J sec

When Ti>j t_i>j or J>=di j>=d_i, the current item cannot be selected, so Dp[i][j]=dp[i−1][j] dp[i][j]=dp[i-1][j]

For other times: Dp[i][j]=max (DP[I−1][J],DP[I−1][J−TI]+PI) Dp[i][j]=\max (dp[i-1][j],dp[i-1][j-t_i]+p_i)

Just a way to record the path.

AC Code

#include <bits/stdc++.h> using 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<x.d;

}} A[MAXN];
    void Solve () {int Maxx = 0,MAXJ = 0;
    for (int i=a[0].t; i<min (2000,A[0].D); i++) dp[0][i] = A[0].p,path[0][i] = 1,maxx = A[0].P,MAXJ = i; for (int i=1; i<n; i++) {for (int j=0; j<=2000; J + +) {if (j<a[i].t| |
            J&GT;=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<int> ans;
            for (int i=n-1,j=maxj; i>=0; i--) {if (path[i][j)) {ans.push_back (i);
        j-=a[i].t;
    }} cout<<maxx<<endl;
    Cout<<ans.size () <<endl;
    int len = Ans.size (); for (int i=len-1; i>=0; i--) cout<<a[ans[i]].id<< (i!=0? ")

": \ n");}
    int main () {cin>>n;
    for (int i=0; i<n; i++) cin>>a[i].t>>a[i].d>>a[i].p,a[i].id = i+1;
    Sort (a,a+n);
    Solve ();
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.