#include<bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) (v).begin(),(v).end()
template<typename T>inline bool chkmin(T &a,const T &b){return a>b?a=b,1:0;}
template<typename T>inline bool chkmax(T &a,const T &b){return a<b?a=b,1:0;}
typedef long long ll;
typedef unsigned uint;
const int INF=0x3f3f3f3f;
const double EPS=1e-10;
typedef pair<int,int> pii;
int inline in(){int x=0,c;for(;(uint)((c=getchar())-'0')>=10;)
{if(c=='-')return -in();if(!~c)throw ~0;}do{x=(x<<3)+(x<<1)+(c-'0');}
while((uint)((c=getchar())-'0')<10);return x;}
int n;
const int Maxn=105;
const int Maxt=2005;
struct node{
	int tim,d,p,id;
	node(){};
	node(int _tim,int _d,int _p,int _id):tim(_tim),d(_d),p(_p),id(_id){};
}a[Maxn];
inline bool cmp(const node &x,const node &y){
	return x.d<y.d;
}
int dp[Maxn][Maxt],pre[Maxn][Maxt];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		a[i].id=i;
		scanf("%d%d%d",&a[i].tim,&a[i].d,&a[i].p);
	}
	sort(a+1,a+n+1,cmp);
//	printf("a:\n");
//	for(int i=1;i<=n;i++)printf("%d %d %d %d\n",a[i].tim,a[i].d,a[i].p,a[i].id);

	memset(pre,-1,sizeof(pre));
	rep(i,Maxn)rep(j,Maxt)dp[i][j]=-INF;

	dp[0][0]=0;
	for(int i=1;i<=n;i++){
		for(int w=0;w<Maxt;w++){
			if(dp[i-1][w]!=INF){
				if(chkmax(dp[i][w],dp[i-1][w])){
					pre[i][w]=w;
				}
			}
			if(w+a[i].tim<a[i].d&&w+a[i].tim<Maxt&&dp[i-1][w]!=-INF){
				if(chkmax(dp[i][w+a[i].tim],dp[i-1][w]+a[i].p)){
					pre[i][w+a[i].tim]=w;
				}
			}
		}
	}
	int ans=-1,ans_tim=-1;
	for(int w=0;w<Maxt;w++){
		if(chkmax(ans,dp[n][w])){
			ans_tim=w;
		}
	}
	printf("%d\n",ans);
	int x=n,y=ans_tim;
	vector<int>seq;
	while(pre[x][y]!=-1){
		if(pre[x][y]!=y)seq.push_back(x);
		y=pre[x][y];
		x--;
	}

//	printf("seq:");rep(i,SZ(seq))printf("%d ",seq[i]);printf("\n");
	vector<int>op;
	rep(i,SZ(seq)){
		op.push_back(a[seq[i]].id);
	}
	sort(all(op));
	printf("%d\n",SZ(op));
	rep(i,SZ(op))printf("%d ",op[i]);printf("\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v,0,sizeof(v))
#define rep(i,n) for(int i=0;i<(n);i++)
#define foreach(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
#define all(v) (v).begin(),(v).end()
template<typename T>inline bool chkmin(T &a,const T &b){return a>b?a=b,1:0;}
template<typename T>inline bool chkmax(T &a,const T &b){return a<b?a=b,1:0;}
typedef long long ll;
typedef unsigned uint;
const int INF=0x3f3f3f3f;
const double EPS=1e-10;
typedef pair<int,int>P;
int inline in(){int x=0,c;for(;(uint)((c=getchar())-'0')>=10;)
{if(c=='-')return -in();if(!~c)throw ~0;}do{x=(x<<3)+(x<<1)+(c-'0');}
while((uint)((c=getchar())-'0')<10);return x;}
int n;
const int Maxn=105;
const int Maxt=2005;
int ti[Maxn],d[Maxn],v[Maxn],c[Maxn];
int dp[Maxt];
P pre[Maxt];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&ti[i],&d[i],&v[i]);
		c[i]=d[i]-ti[i];
	}
	memset(pre,-1,sizeof(pre));
	rep(i,Maxt)dp[i]=-INF;
	dp[0]=0;
	for(int i=1;i<=n;i++){
		for(int w=Maxt-1;w>=0;w--){
			if(w<c[i]&&w+ti[i]<Maxt&&dp[w]!=-INF){
				if(chkmax(dp[w+ti[i]],dp[w]+v[i])){
					pre[w+ti[i]]=P(i,w);
				}
			}
		}
	}
	int ans=-INF,T=-1;
	rep(i,Maxt){
		if(chkmax(ans,dp[i])){
			T=i;
		}
	}
	printf("%d\n",ans);
//	cout<<"T:"<<T<<endl;
	vector<int>seq;
	while(pre[T].first!=-1){
		seq.push_back(pre[T].first);
		T=pre[T].second;
	}
	reverse(all(seq));
	printf("%d\n",SZ(seq));
	rep(i,SZ(seq))printf("%d%c",seq[i],((i==SZ(seq)-1)?'\n':' '));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v,0,sizeof(v))
#define rep(i,n) for(int i=0;i<(n);i++)
#define foreach(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
#define all(v) (v).begin(),(v).end()
template<typename T>inline bool chkmin(T &a,const T &b){return a>b?a=b,1:0;}
template<typename T>inline bool chkmax(T &a,const T &b){return a<b?a=b,1:0;}
typedef long long ll;
typedef unsigned uint;
const int INF=0x3f3f3f3f;
const double EPS=1e-10;
typedef pair<int,int>P;
int inline in(){int x=0,c;for(;(uint)((c=getchar())-'0')>=10;)
{if(c=='-')return -in();if(!~c)throw ~0;}do{x=(x<<3)+(x<<1)+(c-'0');}
while((uint)((c=getchar())-'0')<10);return x;}
int n;
const int Maxn=105;
const int Maxt=2005;
struct Item{
	int need,lim,val,id;
}a[Maxn];
bool cmp(Item x,Item y){
	return x.lim<y.lim;
}
int dp[Maxn][Maxt];
int pre[Maxn][Maxt];
int main(){
	scanf("%d",&n);
	rep(i,n){
		scanf("%d%d%d",&a[i].need,&a[i].lim,&a[i].val);
		a[i].id=i;
	}
	sort(a,a+n,cmp);
//	rep(i,n){
//		printf("%d %d %d %d\n",a[i].need,a[i].lim,a[i].val,a[i].id);
//	}
	memset(pre,-1,sizeof(pre));
	rep(i,n){
		rep(j,Maxt){
			dp[i+1][j]=dp[i][j];
			pre[i+1][j]=pre[i][j];
		}
		for(int j=a[i].lim-1;j>=a[i].need;j--){
			int nval=dp[i][j-a[i].need]+a[i].val;
			if(chkmax(dp[i+1][j],nval)){
				pre[i+1][j]=i;
			}
		}
	}
	int ans=0,mx=0;
	rep(j,Maxt){
		if(chkmax(ans,dp[n][j])){
			mx=j;
		}
	}
	printf("%d\n",ans);
	vector<int>seq;
	int q=n;
	while(pre[q][mx]!=-1){
		seq.push_back(a[pre[q][mx]].id+1);
		int tmp=mx;
		mx-=a[pre[q][mx]].need;
		q=pre[q][tmp];
	}
	reverse(all(seq));
	printf("%d\n",SZ(seq));
	rep(i,SZ(seq))printf("%d%c",seq[i],(i==SZ(seq)-1?'\n':' '));
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005][2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int used[2005][2005];
vector<int> take;
void solve(int x, int lvl)
{
    if(lvl==1) return;
    if(used[lvl][x])
    {
        take.push_back(nn[lvl-1].id);
        x=x-nn[lvl-1].t;
    }
    solve(x,lvl-1);
}
int main()
{
	int i,j,sum,ans=-1;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=1;i<=n;i++)
    {
        for(j=0;j<=2000;j++)
        {
            if(j+nn[i].t<nn[i].d)
            {
                if(dp[i][j]+nn[i].p>dp[i+1][j+nn[i].t])
                {
                    dp[i+1][j+nn[i].t]=dp[i][j]+nn[i].p;
                    used[i+1][j+nn[i].t]=1;
                }
            }
            if(dp[i][j]>dp[i+1][j])
            {
                used[i+1][j]=0;
                dp[i+1][j]=dp[i][j];
            }
            //cout<<dp[i+1][j]<<" ";
        }
        //cout<<endl;
    }
    for(i=0;i<=2002;i++)
    {
        if(dp[n+1][i]>ans)
        {
            ans=dp[n+1][i];
            idx=i;
        }
    }
    solve(idx,n+1);
    cout<<ans<<endl;
    sz=take.size();
    cout<<sz<<endl;
    for(i=sz-1;i>=0;i--)
        cout<<take[i]<<" ";
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005][2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int used[2005][2005];
vector<int> take;
void solve(int x, int lvl)
{
    if(lvl==1) return;
    if(used[lvl][x])
    {
        take.push_back(nn[lvl-1].id);
        x=x-nn[lvl-1].t;
    }
    solve(x,lvl-1);
}
int main()
{
	int i,j,sum,ans=-1;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=1;i<=n;i++)
    {
        for(j=0;j<=20;j++)
        {
            if(j+nn[i].t<nn[i].d)
            {
                if(dp[i][j]+nn[i].p>dp[i+1][j+nn[i].t])
                {
                    dp[i+1][j+nn[i].t]=dp[i][j]+nn[i].p;
                    used[i+1][j+nn[i].t]=1;
                }
            }
            if(dp[i][j]>dp[i+1][j])
            {
                used[i+1][j]=0;
                dp[i+1][j]=dp[i][j];
            }
            //cout<<dp[i+1][j]<<" ";
        }
        //cout<<endl;
    }
    for(i=0;i<=2002;i++)
    {
        if(dp[n+1][i]>ans)
        {
            ans=dp[n+1][i];
            idx=i;
        }
    }
    solve(idx,n+1);
    cout<<ans<<endl;
    sz=take.size();
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
        cout<<take[i]<<" ";
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005][2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int used[2005][2005];
vector<int> take;
void solve(int x, int lvl)
{
    if(lvl==1) return;
    if(used[lvl][x])
    {
        take.push_back(nn[lvl-1].id);
        x=x-nn[lvl-1].t;
    }
    solve(x,lvl-1);
}
int main()
{
	int i,j,sum,ans=-1;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=1;i<=n;i++)
    {
        for(j=0;j<=20;j++)
        {
            if(j+nn[i].t<nn[i].d)
            {
                if(dp[i][j]+nn[i].p>dp[i+1][j+nn[i].t])
                {
                    dp[i+1][j+nn[i].t]=dp[i][j]+nn[i].p;
                    used[i+1][j+nn[i].t]=1;
                }
            }
            dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
            cout<<dp[i+1][j]<<" ";
        }
        cout<<endl;
    }
    for(i=0;i<=2002;i++)
    {
        if(dp[n+1][i]>ans)
        {
            ans=dp[n+1][i];
            idx=i;
        }
    }
    solve(idx,n+1);
    cout<<ans<<endl;
    sz=take.size();
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
        cout<<take[i]<<" ";
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i+nn[j].t>=nn[j].d) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==nn[j].id) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+nn[j].p>dp[i+nn[j].t])
            {
                dp[i+nn[j].t]=dp[i]+nn[j].p;
                v[i+nn[j].t].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+nn[j].t].push_back(v[i][k]);
                }
                v[i+nn[j].t].push_back(nn[j].id);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
struct nz
{
    int t,d,p;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i+nn[j].t>=nn[j].d) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==j) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+nn[j].p>dp[i+nn[j].t])
            {
                dp[i+nn[j].t]=dp[i]+nn[j].p;
                v[i+nn[j].t].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+nn[j].t].push_back(v[i][k]);
                }
                v[i+nn[j].t].push_back(j);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        if(i>0)
        {
            if(dp[i-1]>dp[i])
            {
                v[i].clear();
                sz=v[i-1].size();
                for(j=0;j<sz;j++)
                {
                    v[i].push_back(v[i-1][j]);
                }
                dp[i]=dp[i-1];
            }
        }
        for(j=1;j<=n;j++)
        {
            if(i+nn[j].t>=nn[j].d) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==nn[j].id) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+nn[j].p>dp[i+nn[j].t])
            {
                dp[i+nn[j].t]=dp[i]+nn[j].p;
                v[i+nn[j].t].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+nn[j].t].push_back(v[i][k]);
                }
                v[i+nn[j].t].push_back(nn[j].id);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        if(i>0) dp[i]=max(dp[i],dp[i-1]);
        for(j=1;j<=n;j++)
        {
            if(i+nn[j].t>=nn[j].d) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==nn[j].id) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+nn[j].p>dp[i+nn[j].t])
            {
                dp[i+nn[j].t]=dp[i]+nn[j].p;
                v[i+nn[j].t].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+nn[j].t].push_back(v[i][k]);
                }
                v[i+nn[j].t].push_back(nn[j].id);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        if(i>0)
        {
            if(dp[i-1]>dp[i])
            {
                v[i].clear();
                sz=v[i-1].size();
                for(j=0;j<sz;j++)
                {
                    v[i].push_back(v[i-1][j]);
                }
                dp[i-1]=dp[i];
            }
        }
        for(j=1;j<=n;j++)
        {
            if(i+nn[j].t>=nn[j].d) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==nn[j].id) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+nn[j].p>dp[i+nn[j].t])
            {
                dp[i+nn[j].t]=dp[i]+nn[j].p;
                v[i+nn[j].t].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+nn[j].t].push_back(v[i][k]);
                }
                v[i+nn[j].t].push_back(nn[j].id);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005];
int n;
int t[128],d[128],p[128];
vector<int> v[2005];
int main()
{
	int i,j,sum,ans=0;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>t[i]>>d[i]>>p[i];
    }
    int idx;
    int sz,l,k;
    for(i=0;i<=2002;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i+t[j]>=d[j]) continue;
            sz=v[i].size();
            l=1;
            for(k=0;k<sz;k++)
            {
                if(v[i][k]==j) {l=0; continue;}
            }
            if(!l) continue;
            if(dp[i]+p[j]>dp[i+t[j]])
            {
                dp[i+t[j]]=dp[i]+p[j];
                v[i+t[j]].clear();
                for(k=0;k<sz;k++)
                {
                    v[i+t[j]].push_back(v[i][k]);
                }
                v[i+t[j]].push_back(j);
            }
        }
    }
    ans=-1;
    idx=-1;
    for(i=1;i<=2002;i++)
    {
        if(dp[i]>ans)
        {
            ans=dp[i];
            idx=i;
        }
    }
    sz=v[idx].size();
    cout<<ans<<endl;
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
    {
        cout<<v[idx][i]<<" ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int dp[2005][2005];
int n;
struct nz
{
    int t,d,p;
    int id;
};
int cmp(nz n1 , nz n2)
{
    if(n1.d==n2.d)
    {
        return n1.p>n2.p;
    }
    return n1.d<n2.d;
}
nz nn[128];
vector<int> v[2005];
int used[2005][2005];
vector<int> take;
void solve(int x, int lvl)
{
    if(lvl==1) return;
    if(used[lvl][x])
    {
        take.push_back(nn[lvl-1].id);
        x=x-nn[lvl-1].t;
    }
    solve(x,lvl-1);
}
int main()
{
	int i,j,sum,ans=-1;
	cin>>n;
	for(i=1;i<=n;i++)
    {
        cin>>nn[i].t>>nn[i].d>>nn[i].p;
        nn[i].id=i;
    }
    sort(nn+1,nn+n+1,cmp);
    int idx;
    int sz,l,k;
    for(i=1;i<=n;i++)
    {
        for(j=0;j<=2000;j++)
        {
            if(j+nn[i].t<nn[i].d)
            {
                if(dp[i][j]+nn[i].p>dp[i+1][j+nn[i].t])
                {
                    dp[i+1][j+nn[i].t]=dp[i][j]+nn[i].p;
                    used[i+1][j+nn[i].t]=1;
                }
            }
            if(dp[i][j]>dp[i+1][j])
            {
                used[i+1][j]=0;
                dp[i+1][j]=dp[i][j];
            }
            //cout<<dp[i+1][j]<<" ";
        }
        //cout<<endl;
    }
    for(i=0;i<=2002;i++)
    {
        if(dp[n+1][i]>ans)
        {
            ans=dp[n+1][i];
            idx=i;
        }
    }
    solve(idx,n+1);
    cout<<ans<<endl;
    sz=take.size();
    cout<<sz<<endl;
    for(i=0;i<sz;i++)
        cout<<take[i]<<" ";
    cout<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

int N, cost[MAXN], limit[MAXN], val[MAXN];
int dp[MAXN][MAXN];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXN];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(i);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
        scanf("%d%d%d", &cost[i], &limit[i], &val[i]);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=MAXT;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<cost[i+1]<<" limit: "<<limit[i+1]<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+cost[i+1]<limit[i+1])
                {
                    if(dp[i][j]+val[i+1]>dp[i+1][j+cost[i+1]])
                    {
                        dp[i+1][j+cost[i+1]]=dp[i][j]+val[i+1];
                        last[i+1][j+cost[i+1]]=make_pair(i, j);
                    }
                }
                if(dp[i+1][j+cost[i+1]]>ans)
                {
                    ans=dp[i+1][j+cost[i+1]];
                    ans_i=i+1;
                    ans_j=j+cost[i+1];
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXN];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXN];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=MAXT;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXT];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXT];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=2000;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXT];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXT];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=2000;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit && j+thing[i+1].cost<=2000)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(j+thing[i+1].cost<=2000 && dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXT];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXT];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=2000;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(j+thing[i+1].cost<=2000 && dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXT];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXT];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=0, ans_j=0;
    dp[0][0]=0;
    last[0][0]=make_pair(0, 0);
    for(int i=0;i<N;++i)
        for(int j=2000;j>=0;--j)
            if(~dp[i][j])
            {
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit && j+thing[i+1].cost<=2000)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                    if(dp[i+1][j+thing[i+1].cost]>ans)
                    {
                        ans=dp[i+1][j+thing[i+1].cost];
                        ans_i=i+1;
                        ans_j=j+thing[i+1].cost;
                    }
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXN];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXN];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(i);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=MAXT;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define sqr(x) ((x)*(x))

const int MAXN=100+3;
const int MAXT=2000+3;

struct Thing
{
    int cost, limit, val, id;
    bool operator < (const Thing &other)const
    {
        return limit<other.limit;
    }
}thing[MAXN];

int N;
int dp[MAXN][MAXT];//前i个物品，前j秒的最大价值
pair<int, int> last[MAXN][MAXT];
vector<int> save_ans;

void init()
{
    mem(dp, -1);
}

void out(int i, int j)
{
//    cout<<"i: "<<i<<" j: "<<j<<endl;
    if(last[i][j].fi)
        out(last[i][j].fi, last[i][j].se);
    if(last[i][j].se!=j)
        save_ans.push_back(thing[i].id);
}

int main()
{
    scanf("%d", &N);
    init();
    for(int i=1;i<=N;++i)
    {
        scanf("%d%d%d", &thing[i].cost, &thing[i].limit, &thing[i].val);
        thing[i].id=i;
    }
    sort(thing+1, thing+1+N);
    int ans=0, ans_i=-1, ans_j=-1;
    dp[0][0]=0;
    last[0][0]=make_pair(-1, -1);
    for(int i=0;i<N;++i)
        for(int j=MAXT;j>=0;--j)
            if(~dp[i][j])
            {
//                cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<" cost: "<<thing[i+1].cost<<" limit: "<<thing[i+1].limit<<endl;
                if(dp[i][j]>dp[i+1][j])
                {
                    dp[i+1][j]=dp[i][j];
                    last[i+1][j]=make_pair(i, j);
                }
                if(j+thing[i+1].cost<thing[i+1].limit)
                {
                    if(dp[i][j]+thing[i+1].val>dp[i+1][j+thing[i+1].cost])
                    {
                        dp[i+1][j+thing[i+1].cost]=dp[i][j]+thing[i+1].val;
                        last[i+1][j+thing[i+1].cost]=make_pair(i, j);
                    }
                }
                if(dp[i+1][j+thing[i+1].cost]>ans)
                {
                    ans=dp[i+1][j+thing[i+1].cost];
                    ans_i=i+1;
                    ans_j=j+thing[i+1].cost;
                }
            }
    printf("%d\n", ans);
    out(ans_i, ans_j);
    printf("%d\n", (int)save_ans.size());
    for(int i=0;i<save_ans.size();++i)
        printf("%d%c", save_ans[i], i==save_ans.size()-1?'\n':' ');

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn = 10000000 + 5;
int n, k;
int a[maxn];
long long sum[maxn];
long long dlt[maxn];
int ret;

bool cmp(int x, int y){
    return x > y;
}

int main(){
    cin>>n>>k;
    for(int i = 1;i <= n;i++){
        cin>>a[i];
    }
    sort(a+1, a+n+1);
    for(int i = 1;i <= n;i++){
        sum[i] = sum[i-1] + a[i];
    }
    int p = 1;
    long long cut = 0;
    for(int h = a[n];h >= a[1];h--){
        while(p <= n && a[p] > h){
            p++;
        }
        dlt[h] = sum[p-1] - h * (p-1) - cut;
        if (dlt[h] > k){
            ret++;
            cut += dlt[h+1];
            dlt[h] = sum[p-1] - h * (p-1) - cut;
        }
    }
    if(dlt[a[n]] > 0){
        ret++;
    }
    cout<<ret<<endl;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x7fffffff;
const int maxn=2000+10;
struct node{
	int id,t,d,p;
	bool operator < (const node &a) const{
		return d<a.d;
	}
}nd[maxn];
int dp[maxn];
int main(){
	int n;  scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&nd[i].t,&nd[i].d,&nd[i].p);
		nd[i].id=i;
	}
	sort(nd+1,nd+1+n);
	vector<int> v[maxn];
	int maxans=0,ansid;
	for(int i=1;i<=n;i++){
		node it=nd[i];
		for(int j=it.d-1;j>=it.t;j--){
			if(dp[j]<dp[j-it.t]+it.p){
				dp[j]=dp[j-it.t]+it.p;
				v[j]=v[j-it.t];
				v[j].push_back(nd[i].id);
			}
			if(dp[j]>maxans){
				maxans=dp[j];
				ansid=j;
			}
		}
	}
	printf("%d\n",maxans);
	printf("%d\n",v[ansid].size());
	for(int i=0;i<v[ansid].size();i++)
		printf("%d ",v[ansid][i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=100+25;
struct fire{
    int t,d,p;
    int num;
}s[maxn];
int n,mx,ans;
int dp[2000+5];//表示前i个时间能得到的最大值
string p[2000+5],q;
bool cmp(fire x,fire y){
    if(x.d!=y.d)return x.d<y.d;
    return x.t<y.t;
}
int main(){
    //freopen("fire.in","r",stdin);
    //freopen("fire.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d%d%d",&s[i].t,&s[i].d,&s[i].p),mx=max(mx,s[i].d),s[i].num=i;
    sort(s+1,s+n+1,cmp);
    for(int i=1;i<=n;++i){
        for(int j=mx;j>=s[i].t;--j){
            if(j<s[i].d){
            	if(dp[j]<dp[j-s[i].t]+s[i].p){
            		dp[j]=max(dp[j],dp[j-s[i].t]+s[i].p);
            		char c=s[i].num+'0';
            		p[j]=p[j-s[i].t]+c;
				}
            }
        }
    }
    for(int i=0;i<=mx;++i){
    	if(ans<dp[i]){
    		ans=dp[i];
    		q=p[i];
		}
	}
    printf("%d\n",ans);
    cout<<q.length()<<endl;
    for(int i=0;i<q.length();++i)printf("%c ",q[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}


#include<bits/stdc++.h>
#define MAXN 10001
using namespace std;
struct Node
{
    int t,d,p,id;
    bool operator<(const Node&x) const
	{
		return d<x.d;
	}
}a[MAXN];
int n;
bool vis[MAXN][MAXN];
int ans,max1;
int dp[MAXN];
int cnt,out[MAXN];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
	{
        cin>>a[i].t>>a[i].d>>a[i].p;
        a[i].id=i;
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=a[i].d-1;j>=a[i].t;j--)
        {
          	int tmp=dp[j];
            dp[j]=max(dp[j],dp[j-a[i].t]+a[i].p);
            if(dp[j]!=tmp)
            vis[i][j]=1;
        }
    }
    for(int i=1;i<=a[n].d;i++)
    {
        if(dp[i]>max1)
		{
			ans=i;
			max1=dp[i];
		}
    }
    cout<<max1<<endl;
    for(int i=n;i>=1;i--)
    {
        if(vis[i][ans])
		{
			out[++cnt]=a[i].id;
			ans-=a[i].t;
		}
	}
    cout<<cnt<<endl;
    for(int i=1;i<=cnt;i++)
    {
    	cout<<out[i]<<" ";
	}
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
const int maxn = 110;
struct node{
	int id;
	int t, st, w;
}e[maxn];
int n, tot, ans;
int f[2010];
il bool cmp(node x, node y) {
	if(x.st == y.st) return x.id < y.id;
	return x.st < y.st;
}
int v[2010][2010];
int main() {
	scanf("%d", &n);
	int a, b, c;
	memset(f, 0, sizeof(f));
	tot = 0; ans = 0;
	for(re int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &a, &b, &c);
		e[i].id = i;
		e[i].t = a; e[i].st = b; e[i].w = c;
	}
	sort(e+1, e+n+1, cmp);
	int r = e[n].st - 1;
	for(re int i = 1; i <= n; ++i)
		for(re int j = r; j >= e[i].t; --j)
			if(f[j] < f[j-e[i].t] + e[i].w) {
				f[j] = f[j-e[i].t] + e[i].w;
				//cout << e[j].w << endl;
				v[i][j] = 1;
				//cout << j << " " << e[i].id << " " << e[i].w << endl;
			}
	int ans = 0;
	int to[maxn], cnt = 0;
	for(re int i = 1; i <= r; ++i)
		if(f[i] > f[ans]) ans = i;
	printf("%d\n", f[ans]);
	for(re int i = n; i >= 0; i--)
		if(v[i][ans]) to[++cnt]=e[i].id, ans -= e[i].t;
	printf("%d\n", cnt);
	for(re int i = cnt; i > 0; i--)
		printf("%d ", to[i]);
	return 0;
}
//how to 记录?


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> v[3010];
int dp[3010];
struct team
{
	int t,d,p,xb;
}a[110];
inline bool cmp(team x,team y){return x.d<y.d;}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)a[i].xb=i;
	for(int i=0;i<n;i++)cin>>a[i].t>>a[i].d>>a[i].p;
	sort(a,a+n,cmp);
	for(int i=0;i<n;i++)
	{
		for(int j=2000;j>=0;j--)
		{
			if(a[i].t>=j)continue;
			if(j>a[i].d)continue;
			if(dp[j-a[i].t]+a[i].p>dp[j])
			{
				dp[j]=dp[j-a[i].t]+a[i].p;
				v[j].clear();
				for(int k=0;k<v[j-a[i].t].size();k++)v[j].push_back(v[j-a[i].t][k]);
				v[j].push_back(a[i].xb);
			}
		}
	}
	int cnt=0;
	int xbb;
	for(int i=0;i<=2000;i++)
	{
		if(dp[i]>cnt)
		{
			cnt=dp[i];
			xbb=i;
		}
	}
	cout<<cnt<<endl<<v[xbb].size()<<endl;
	for(int i=0;i<v[xbb].size();i++)
	{
		if(i!=0)cout<<" ";
		cout<<v[xbb][i]+1;
	}
	cout<<endl;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N=1e4+5;
int n,f[N];
struct node
{
	int t,d,p,id;
	bool operator < (const node&other) const
	{
		return d<other.d;
	}
}a[N];
vector <int> g[N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d %d %d",&a[i].t,&a[i].d,&a[i].p),a[i].id=i;
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
	{
		for(int j=a[i].d-1;j>=a[i].t;j--)
		{
			if(f[j-a[i].t]+a[i].p>f[j])
			{
				f[j]=f[j-a[i].t]+a[i].p;
				g[j].clear();
				g[j]=g[j-a[i].t];
				g[j].push_back(a[i].id);
			}
		}
	}
	int ans=0;
	for(int i=1;i<=2000;i++)
		if(f[i]>f[ans])
			ans=i;
	printf("%d\n",f[ans]);
	int len=g[ans].size();
	printf("%d\n",len);
	for(int i=0;i<len;i++)
		printf("%d ",g[ans][i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=101;
int n;
struct object{
	int need,start,val,id;
}e[maxn];
bool cmp(object a,object b){
	return a.start<b.start;
}
bool rcmp(int a,int b){
	return e[a].id<e[b].id;
}
int dp[102][2011],f[102][2011];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>e[i].need>>e[i].start>>e[i].val;
		e[i].id=i;
	}
	sort(e+1,e+1+n,cmp);
	for(int i=1;i<=n;i++){
		for(int j=e[i].start-1;j>=e[i].need;j--){
			int tmp=dp[i][j];
			dp[i][j]=max(dp[i][j],dp[i-1][j-e[i].need]+e[i].val);
			if(dp[i][j]!=tmp){
				f[i][j]=true;
			}
		}
	}
	int ans=0,pb=0;
	for(int i=1;i<maxn;i++){
		if(dp[n][i]>ans){
			ans=dp[n][i];
			pb=i;
		}
	}
	cout<<ans<<endl;
	vector<int> out;
	out.clear();
	for(int i=n;i>=1;i--){
		if(f[i][pb]==1){
			out.push_back(e[i].id);
			pb-=e[i].need;
		}
	}
	reverse(out.begin(),out.end());
	cout<<out.size()<<endl;
	for(int i=0;i<out.size();i++){
		cout<<out[i]<<" ";
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 105
using namespace std;
int f[maxn],vis[maxn][maxn],ans,cnt,can[maxn];
struct node
{
	int id,t,d,p;
}wp[maxn];
bool cmp(node a,node b)
{
	return a.d<b.d;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&wp[i].t,&wp[i].d,&wp[i].p);
		wp[i].id=i;
	}
	sort(wp+1,wp+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		for(int j=wp[i].d-1;j>=wp[i].t;j--)
		{
			if(f[j]<f[j-wp[i].t]+wp[i].p)
			{
				f[j]=f[j-wp[i].t]+wp[i].p;
				vis[i][j]=1;
			}
		}
	}
	for(int i=1;i<=wp[n].d;i++)
	{
		if(f[i]>f[ans])
		ans=i;
	}
	printf("%d\n",f[ans]);
	for(int i=n;i>=1;i--)
	if(vis[i][ans])
	can[++cnt]=wp[i].id,ans-=wp[i].t;
	printf("%d\n",cnt);
	for(int i=cnt;i>=1;i--)
	printf("%d ",can[i]);
}


//team:p,t,d,id
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> v[2020];
int dp[2020];
struct team
{
	int t,d,p,xb;
}a[110];
inline bool cmp(team x,team y){return x.d<y.d;}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)a[i].xb=i;
	for(int i=0;i<n;i++)cin>>a[i].t>>a[i].d>>a[i].p;
	sort(a,a+n,cmp);
	for(int i=0;i<n;i++)
	{
		for(int j=2000;j>=0;j--)
		{
			if(a[i].t>=j)continue;
			if(j>a[i].d)continue;
			if(dp[j-a[i].t]+a[i].p>dp[j])
			{
				dp[j]=dp[j-a[i].t]+a[i].p;
				v[j].clear();
				for(int k=0;k<v[j-a[i].t].size();k++)v[j].push_back(v[j-a[i].t][k]);
				v[j].push_back(a[i].xb);
			}
		}
	}
	int cnt=0;
	int xbb;
	for(int i=0;i<=2000;i++)
	{
		if(dp[i]>cnt)
		{
			cnt=dp[i];
			xbb=i;
		}
	}
	cout<<cnt<<endl<<v[xbb].size()<<endl;
	for(int i=0;i<v[xbb].size();i++)
	{
		if(i!=0)cout<<" ";
		cout<<v[xbb][i]+1;
	}
	cout<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct D{int t,d,v,id;}e[101];
int f[2005],ans,ti;
vector<int> q[2005];
int n;
bool cmp(const D&a,const D&b) {return a.d<b.d;}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%d%d%d",&e[i].t,&e[i].d,&e[i].v),e[i].id=i;
	sort(e+1,e+n+1,cmp);
	for(int i=1;i<=n;i++)
	for(int j=e[i].d-1;j>=e[i].t;j--)
	{
		if(f[ j-e[i].t ]+e[i].v>f[j])
		{
			f[j]=f[ j-e[i].t ]+e[i].v;
			q[j]=q[ j-e[i].t ];
			q[j].push_back(e[i].id);
		}
	}
	for(int i=1;i<=2000;i++)
	if(f[i]>ans) ans=f[i],ti=i;
	printf("%d\n%d\n",ans,q[ti].size());
	for(int i=0;i<q[ti].size();i++)
	printf("%d ",q[ti][i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define maxn 2005
using namespace std;
int f[maxn],vis[maxn][maxn],ans,cnt,can[maxn];
struct node
{
	int id,t,d,p;
}wp[maxn];
bool cmp(node a,node b)
{
	return a.d<b.d;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&wp[i].t,&wp[i].d,&wp[i].p);
		wp[i].id=i;
	}
	sort(wp+1,wp+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		for(int j=wp[i].d-1;j>=wp[i].t;j--)
		{
			if(f[j]<f[j-wp[i].t]+wp[i].p)
			{
				f[j]=f[j-wp[i].t]+wp[i].p;
				vis[i][j]=1;
			}
		}
	}
	for(int i=1;i<=wp[n].d;i++)
	{
		if(f[i]>f[ans])
		ans=i;
	}
	printf("%d\n",f[ans]);
	for(int i=n;i>=1;i--)
	if(vis[i][ans])
	can[++cnt]=wp[i].id,ans-=wp[i].t;
	printf("%d\n",cnt);
	for(int i=cnt;i>=1;i--)
	printf("%d ",can[i]);
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=101;
int n;
struct object{
	int need,start,val,id;
}e[maxn];
bool cmp(object a,object b){
	return a.start<b.start;
}
bool rcmp(int a,int b){
	return e[a].id<e[b].id;
}
int dp[102][2011],f[102][2011];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>e[i].need>>e[i].start>>e[i].val;
		e[i].id=i;
	}
	sort(e+1,e+1+n,cmp);
	for(int i=1;i<=n;i++){
		for(int j=0;j<2001;j++){
			dp[i][j]=dp[i-1][j];
			f[i][j]=-1;
		}
		for(int j=e[i].need;j<e[i].start;j++){
			int tmp=dp[i][j];
			dp[i][j]=max(dp[i][j],dp[i-1][j-e[i].need]+e[i].val);
			if(dp[i][j]!=tmp){
				f[i][j]=e[i].id;
			}
		}
	}
	int ans=0,pb=0;
	for(int i=1;i<maxn;i++){
		if(dp[n][i]>ans){
			ans=dp[n][i];
			pb=i;
		}
	}
	cout<<ans<<endl;
	vector<int> out;
	out.clear();
	int i=n,j=pb;
	while(i>0&&j>0){
		//cout<<i<<" "<<j<<" "<<out.size()<<endl;
		if(f[i][j]>0){
			out.push_back(f[i][j]);
			i--;
			j-=e[i].need;
		}else{
			i--;
		}
	}
	sort(out.begin(),out.end());
	cout<<out.size()<<endl;
	for(int i=0;i<out.size();i++){
		cout<<out[i]<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct node{
	int t,d,p,id;
}a[110];
int n;
bool cmp(node x,node y){
	return x.d < y.d;
}
bool CMP(int x,int y){
	return a[x].id < a[y].id;
}
int dp[4010],judge[210][4010];
vector<int> ans;
int main(){
	memset(dp,0,sizeof(dp));
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d%d%d",&a[i].t,&a[i].d,&a[i].p),a[i].id = i;
	sort(a+1,a+1+n,cmp);
	for(int i = 1;i <= n;i++){
		for(int j = a[i].d-1;j >= a[i].t;j--){
			if(dp[j-a[i].t]+a[i].p >= dp[j]){
				judge[i][j] = 1;
				dp[j] = dp[j-a[i].t]+a[i].p;
			}
		}
	}
	int maxid = 0;
	for(int i = 1;i <= a[n].d;i++){
		if(dp[i] > dp[maxid]) maxid = i;
	}
	int aim = maxid;
	for(int i = n;i;i--){
		if(judge[i][aim]){
			ans.push_back(i);
			aim -= a[i].t;
		}
	}
	printf("%d\n%d\n",dp[maxid],ans.size());
	sort(ans.begin(),ans.end());
	for(int i = 0;i < ans.size();i++){
		printf("%d ",a[ans[i]].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn = 10000000 + 5;
int n, k;
int a[maxn];
long long sum[maxn];
long long dlt[maxn];
int ret;

bool cmp(int x, int y){
    return x > y;
}

int main(){
    cin>>n>>k;
    for(int i = 1;i <= n;i++){
        cin>>a[i];
    }
    sort(a+1, a+n+1, cmp);
    for(int i = 1;i <= n;i++){
        sum[i] = sum[i-1] + a[i];
    }
    int p = 1;
    ret = 0;
    long long cut = 0;
    for(int h = a[1];h >= a[n];h--){
        while(p <= n && a[p] > h){
            p++;
        }
        dlt[h] = sum[p-1] - h * (p-1) - cut;
        if (dlt[h] > k){
            ret++;
            cut += dlt[h+1];
            dlt[h] = sum[p-1] - h * (p-1) - cut;
        }
    }
    if(dlt[a[n]] > 0){
        ret++;
    }
    cout<<ret<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct Item{//结构体，p,t,d作用于题面里相同，num储存编号
	int p,t,d,num;
} a[110];
int n,f[2010],ans,ansnum;//ans储存最大价值，ansnum储存最大价值对应的f[i]的编号
vector<int> v[2010];
bool cmp(Item a,Item b){
	return a.d<b.d;//按d排序
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].t>>a[i].d>>a[i].p;
		a[i].num=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++)
		for(int j=a[i].d-1;j>=a[i].t;j--)//只能取一次，倒序循环（从a[i].d-1开始！）
			if(f[j-a[i].t]+a[i].p>f[j]){
				f[j]=f[j-a[i].t]+a[i].p;//转移方程
				v[j]=v[j-a[i].t];
				v[j].push_back(a[i].num);//更新v[j]
			}
	for(int i=0;i<=2000;i++){
		if(f[i]>ans){
			ans=f[i];
			ansnum=i;
		}
	}
	cout<<ans<<endl<<v[ansnum].size()<<endl;
	for(int i=0;i<v[ansnum].size();i++)
		cout<<v[ansnum][i]<<" ";//输出
	return 0;
}


#include<bits/stdc++.h>
#define MAXN 2010
#define N 110
using namespace std;

inline int read()
{
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^48);
	return x*f;
}

struct node{
	int t,d,p,id;
}A[N];

int n,f[MAXN];
priority_queue<int,vector<int>,greater<int> > Q[MAXN];

inline bool cmp(const node x,const node y)
{
	return x.d<y.d;
}

int main()
{
	n=read();int pos=0;
	for(int i=1;i<=n;++i){
		A[i].t=read(),A[i].d=read(),A[i].p=read(),A[i].id=i;
	}
	sort(A+1,A+1+n,cmp);
	for(int i=1;i<=n;++i){
		for(int j=A[i].d-1;j>=A[i].t;--j){
			if(f[j]<f[j-A[i].t]+A[i].p){
				f[j]=f[j-A[i].t]+A[i].p;
				while(!Q[j].empty()) Q[j].pop();
				Q[j]=Q[j-A[i].t];
                Q[j].push(A[i].id);
			}
		}
	}
	for(int i=1;i<=2000;++i)
        if(f[i]>f[pos]) pos=i;
	printf("%d\n%d\n",f[pos],Q[pos].size());
	while(!Q[pos].empty()){
		printf("%d ",Q[pos].top());Q[pos].pop();
	}
	return 0;
}


#include<bits/stdc++.h>
#define MAXN 2010
#define N 110
using namespace std;

inline int read()
{
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^48);
	return x*f;
}

struct node{
	int t,d,p,id;
}A[N];

int n,f[MAXN];
priority_queue<int,vector<int>,greater<int> > Q[MAXN];

inline bool cmp(const node x,const node y)
{
	return x.d<y.d;
}

int main()
{
	n=read();int ans=0,pos;
	for(int i=1;i<=n;++i){
		A[i].t=read(),A[i].d=read(),A[i].p=read(),A[i].id=i;
	}
	sort(A+1,A+1+n,cmp);
	for(int i=1;i<=n;++i){
		for(int j=A[i].d-1;j>=A[i].t;--j){
			if(f[j]<f[j-A[i].t]+A[i].p){
				f[j]=f[j-A[i].t]+A[i].p;
				while(!Q[j].empty()) Q[j].pop();
				Q[j]=Q[j-A[i].t];
                Q[j].push(A[i].id);
				if(f[j]>ans) ans=f[j],pos=j;
			}
		}
	}
	printf("%d\n%d\n",f[pos],Q[pos].size());
	while(!Q[pos].empty()){
		printf("%d ",Q[pos].top());Q[pos].pop();
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#define maxn 105
using namespace std;
int ti[maxn],di[maxn],pi[maxn];
int f[100000];
string ans[100000];
int main()
{
    int n,zs=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>ti[i]>>di[i]>>pi[i];
    	zs+=ti[i];
    }

    for(int i=1;i<=n;i++)
    {
    	for(int j=zs;j>ti[i] && zs-j<di[i] && zs-j+ti[i]<di[i];j--)
    	{
    	   if(f[j-ti[i]]+pi[i]>f[j])
    	   {
	    	   f[j]=f[j-ti[i]]+pi[i];
	           ans[j]=ans[j-ti[i]]+char(i);
    	   }
    	}
    }
    cout<<f[zs]<<endl;
    cout<<ans[zs].size()<<endl;
    for(int i=0;i<ans[zs].size();i++)
    {
    	int cnt=ans[zs][i];
    	cout<<cnt<<" ";
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t[110],d[110],p[110];
struct node
{
	int ans,s[110],len;
	node()
	{
		ans=0;len=0;
	}
}f[2100];
int main()
{
	int n,maxx=0;scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d%d",&t[i],&d[i],&p[i]),maxx=max(maxx,d[i]);
	node ans;ans.ans=0;
	for(int i=1;i<=n;i++)
		for(int j=maxx;j>=t[i];j--)
		{
			if(f[j].ans<f[j-t[i]].ans+p[i]&&j<d[i])
			{
				f[j]=f[j-t[i]];
				f[j].ans+=p[i];
				f[j].len++;
				f[j].s[f[j].len]=i;
			}
			if(ans.ans<f[j].ans)ans=f[j];
		}
	printf("%d\n%d\n",ans.ans,ans.len);
	for(int i=1;i<ans.len;i++)printf("%d ",ans.s[i]);
	printf("%d\n",ans.s[ans.len]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[2100],len,s[110];
bool road[110][2100];
struct wuti
{
	int t,d,p,num;
}wt[110];
bool cmp(wuti n1,wuti n2)
{
	return n1.d<n2.d;
	return n1.t<n2.t;
}
int main()
{
	int n,ans,anss=0;scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d%d",&wt[i].t,&wt[i].d,&wt[i].p),wt[i].num=i;
	sort(wt+1,wt+n+1,cmp);
	memset(f,0,sizeof(f));memset(road,false,sizeof(road));
	for(int i=1;i<=n;i++)
	{
		int t=wt[i].t,d=wt[i].d,p=wt[i].p,num=wt[i].num;
		for(int j=d-1;j>=t;j--)
		{
			if(f[j]<f[j-t]+p)
			{
				f[j]=f[j-t]+p;
				road[num][j]=true;
			}
			if(anss<f[j])
			{
				ans=j;anss=f[j];
			}
		}
	}
	printf("%d\n",anss);
	len=0;
	for(int i=n;i>=1;i--)if(road[i][ans]&&ans){ans-=wt[i].t;s[++len]=i;}
	printf("%d\n",len);
	for(int i=len;i>1;i--)printf("%d ",s[i]);
	printf("%d\n",s[1]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N=1e2+5;
const int T=2e3+5;
int pre[N][T+1];
vector <int> v;
int dp[N][T+1];//±íÊ¾ÔÚTÊ±¿ÌÊ±£¬Ç°n¸öÎïÌåÀï£¬Ä¿Ç°ËùÄÜÈ¡³öµÄ×î´ó¼ÛÖµ
struct node
{
	int id;
	int t,d,p;
	bool operator <(const node &b)const
	{
		return d<b.d;
	}
}obj[N];
int n;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++)cin>>obj[i].t>>obj[i].d>>obj[i].p,obj[i].id=i;
	for (int i=0;i<=n;i++)for (int j=0;j<=T;j++)dp[i][j]=0;
	sort(obj+1,obj+1+n);
	for (int i=1;i<=n;i++)
		for (int j=0;j<=T;j++)
			if (j<obj[i].d&&j>=obj[i].t&&(dp[i-1][j]<dp[i-1][j-obj[i].t]+obj[i].p))
			{
				dp[i][j]=dp[i-1][j-obj[i].t]+obj[i].p;
				pre[i][j]=j-obj[i].t;
			}
			else
			{
				pre[i][j]=j;
				dp[i][j]=dp[i-1][j];
			}
	int nowt=0;
	int ans=0;
	for (int i=1;i<=T;i++)
	{
		if (dp[n][i]>ans)
		{
			ans=dp[n][i];
			nowt=i;
		}
	}
	cout<<ans<<"\n";
	for (int i=n;i>=1;i--)
	{
		if (pre[i][nowt]!=nowt)
		{
			v.push_back(obj[i].id);
		}
		nowt=pre[i][nowt];
	}
	cout<<v.size()<<"\n";
	for (int i=v.size()-1;i>=0;i--)
		cout<<v[i]<<" ";
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int Maxn=110,Maxm=2010,inf=0x3f3f3f3f;
int f[Maxm],n,ans;
struct node{
	int d,t,v,id;
}a[Maxn];
vector <int> bin[Maxm];
inline bool cmp(node x,node y)
{
	return x.d<y.d;
}
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
	//freopen("in.txt","r",stdin);
	n=read();
	for(int i=1;i<=n;++i)
	{
		a[i].t=read(),a[i].d=read(),a[i].v=read();
		a[i].id=i;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;++i)
	for(int j=a[i].d-1;j>=a[i].t;--j)
	if(f[j]<f[j-a[i].t]+a[i].v)
	{
		f[j]=f[j-a[i].t]+a[i].v;
		bin[j].clear();
		bin[j]=bin[j-a[i].t];
		bin[j].push_back(a[i].id);
	}
	for(int i=1;i<=2000;++i)
	if(f[i]>f[ans])ans=i;
	printf("%d\n%d\n",f[ans],bin[ans].size());
	for(int i=0;i<bin[ans].size();++i)
	printf("%d ",bin[ans][i]);
	printf("\n");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define M 105
#define INF 0x3f3f3f3f

struct node {
	int t, d, p;
}a[M];
struct fuck{
	int hav[105], val, cnt;
}dp[2005];
int n, maxn, ans;

int main (){
	scanf ("%d", &n);
	for (int i = 1; i <= n; i ++){
		scanf ("%d %d %d", &a[i].t, &a[i].d, &a[i].p);
		maxn = max (maxn, a[i].d);
	}
	for (int i = 1; i <= maxn; i ++)
		dp[i].val = -INF;
	for (int i = 1; i <= n; i ++){
		for (int j = a[i].d - 1; j >= a[i].t; j --){
			if (dp[j].val < dp[j - a[i].t].val + a[i].p){
				dp[j].val = dp[j - a[i].t].val + a[i].p;
				for (int k = 1; k <= dp[j - a[i].t].cnt; k ++)
					dp[j].hav[k] = dp[j - a[i].t].hav[k];
				dp[j].cnt = dp[j - a[i].t].cnt + 1;
				dp[j].hav[dp[j].cnt] = i;
				if (dp[j].val > dp[ans].val)
					ans = j;
			}
		}
	}
	printf ("%d\n%d\n", dp[ans].val, dp[ans].cnt);
	sort (dp[ans].hav + 1, dp[ans].hav + 1 + dp[ans].cnt);
	for (int i = 1; i <= dp[ans].cnt; i ++)
		printf ("%d ", dp[ans].hav[i]);
	printf ("\n");
	return 0;
}
/*
3
3 7 4
3 7 6
2 6 5
*/


//#pragma GCC optimize(3)

#include <random>

#include <stdio.h>

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>

using namespace std;

typedef long long ll;

typedef unsigned int uint;

typedef unsigned long long ull;

typedef pair<ll, ll> pll;

typedef pair<ll, int> pli;

typedef pair<int, ll> pil;

typedef pair<int, int> pii;

#define Continue(x) { x; continue; }

#define Break(x) { x; break; }

#define Return(x) { x; return; }

#define P2(x) ((x) * (x))

#define lowbit(x) ((x) & -(x))

#define SZ(x) ((int)(x).size())

#define ALL(x) (x).begin(), (x).end()

#define MEM(x, y) memset((x), (y), sizeof(x))

#define MEN(x, y, z) memset((x), (y), sizeof((x)[0]) * (z))

#define Diz(x) (lower_bound(dz.begin(), dz.end(), (x)) - dz.begin())

#define DISCRETE(x) (x).push_back(INT_MIN), sort((x).begin(), (x).end()), (x).erase(unique((x).begin(), (x).end()), (x).end())

#ifdef LOCAL

#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;

#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;

void PRT() {} template <typename S, typename... T> void PRT(S x, T ... y){ cout << " " << x; PRT(y...); }

void PRTS() {} template <typename S> void PRTS(S x) { for (auto v : x) cout << " " << v; }

#define TIME cout << "RunTime: " << clock() << "ms\n", 0

#define __int128_t long long

#else

#define DEBUG(...) ;

#define DEBUGS(...) ;

#define TIME 0

#endif

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3f;

const int MOD = 1e9 + 7;

template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }

template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }

template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % MOD; if (a < 0) a += MOD; }

template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % MOD; }

template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }

template<typename T> inline T Gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; } return a; }

template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }

template<typename T> inline T MDis(T x1, T y1, T x2, T y2){ return abs(x1 - x2) + abs(y1 - y2); }

template<typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2){ return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); }

inline double EDis(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); }

inline ll Pow(ll a, ll n) { ll t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; }

#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;

inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }

//emplace unordered 能ll就开 函数内判非法

const int N = 2e3 + 100;

struct node

{

	int t, d, p, id;

	bool operator < (const node &o) const

	{

		return d > o.d;

	}

}a[N];

int f[N][N]; //完成的任务 从当前时间开始 最大收益

int main()

{

#ifdef LOCAL

	freopen("C:/input.txt", "r", stdin);

	//freopen("C:/output.txt", "w", stdout);

#endif

	//不能一维直接记录转移!!

	int n;

	cin >> n;

	for (int i = 1; i <= n; ++i)

		scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p), a[i].id = i;

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; ++i)

		for (int j = 0; j <= 2000; ++j)

		{

			Max(f[i][j], f[i - 1][j]);

			if (j < a[i].d - a[i].t)

				Max(f[i][j], f[i - 1][j + a[i].t] + a[i].p);

		}

	cout << f[n][0] << endl;

	vector<int> v;

	int i = n, j = 0;

	while (i >= 1 && j <= 2000 && f[i][j])

	{

		if (j < a[i].d - a[i].t && f[i][j] == f[i - 1][j + a[i].t] + a[i].p) //当前是从i转移 需要判断d-t

			v.push_back(a[i].id), j += a[i].t;

		--i;

	}

	cout << SZ(v) << endl;

	for (int x : v)

		printf("%d ", x);

	cout << endl;

	;;;;

	return TIME;

}


//#pragma GCC optimize(3)

#include <random>

#include <stdio.h>

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>

using namespace std;

typedef long long ll;

typedef unsigned int uint;

typedef unsigned long long ull;

typedef pair<ll, ll> pll;

typedef pair<ll, int> pli;

typedef pair<int, ll> pil;

typedef pair<int, int> pii;

#define Continue(x) { x; continue; }

#define Break(x) { x; break; }

#define Return(x) { x; return; }

#define P2(x) ((x) * (x))

#define lowbit(x) ((x) & -(x))

#define SZ(x) ((int)(x).size())

#define ALL(x) (x).begin(), (x).end()

#define MEM(x, y) memset((x), (y), sizeof(x))

#define MEN(x, y, z) memset((x), (y), sizeof((x)[0]) * (z))

#define Diz(x) (lower_bound(dz.begin(), dz.end(), (x)) - dz.begin())

#define DISCRETE(x) (x).push_back(INT_MIN), sort((x).begin(), (x).end()), (x).erase(unique((x).begin(), (x).end()), (x).end())

#ifdef LOCAL

#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;

#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;

void PRT() {} template <typename S, typename... T> void PRT(S x, T ... y){ cout << " " << x; PRT(y...); }

void PRTS() {} template <typename S> void PRTS(S x) { for (auto v : x) cout << " " << v; }

#define TIME cout << "RunTime: " << clock() << "ms\n", 0

#define __int128_t long long

#else

#define DEBUG(...) ;

#define DEBUGS(...) ;

#define TIME 0

#endif

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3f;

const int MOD = 1e9 + 7;

template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }

template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }

template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % MOD; if (a < 0) a += MOD; }

template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % MOD; }

template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }

template<typename T> inline T Gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; } return a; }

template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }

template<typename T> inline T MDis(T x1, T y1, T x2, T y2){ return abs(x1 - x2) + abs(y1 - y2); }

template<typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2){ return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); }

inline double EDis(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); }

inline ll Pow(ll a, ll n) { ll t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; }

#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;

inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }

//emplace unordered 能ll就开 函数内判非法

const int N = 2e3 + 100;

struct node

{

	int t, d, p, id;

	bool operator < (const node &o) const

	{

		return d > o.d;

	}

}a[N];

int f[N][N]; //完成的任务 从当前时间开始 最大收益

int main()

{

#ifdef LOCAL

	freopen("C:/input.txt", "r", stdin);

	//freopen("C:/output.txt", "w", stdout);

#endif

	//不能一维直接记录转移!!

	int n;

	cin >> n;

	for (int i = 1; i <= n; ++i)

		scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p), a[i].id = i;

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; ++i)

		for (int j = 0; j <= 2000; ++j)

		{

			Max(f[i][j], f[i - 1][j]);

			if (j < a[i].d - a[i].t)

				Max(f[i][j], f[i - 1][j + a[i].t] + a[i].p);

		}

	cout << f[n][0] << endl;

	vector<int> v;

	int i = n, j = 0;

	while (i >= 1 && j <= 2000 && f[i][j])

	{

		if (j < a[i].d - a[i].t && f[i][j] == f[i - 1][j + a[i].t] + a[i].p) //当前是从i转移 需要判断d-t

			v.push_back(a[i].id), j += a[i].t;

		--i;

	}

	cout << SZ(v) << endl;

	for (int x : v)

		printf("%d ", x);

	cout << endl;

	;;

	return TIME;

}


//#pragma GCC optimize(3)

#include <random>

#include <stdio.h>

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>

using namespace std;

typedef long long ll;

typedef unsigned int uint;

typedef unsigned long long ull;

typedef pair<ll, ll> pll;

typedef pair<ll, int> pli;

typedef pair<int, ll> pil;

typedef pair<int, int> pii;

#define Continue(x) { x; continue; }

#define Break(x) { x; break; }

#define Return(x) { x; return; }

#define P2(x) ((x) * (x))

#define lowbit(x) ((x) & -(x))

#define SZ(x) ((int)(x).size())

#define ALL(x) (x).begin(), (x).end()

#define MEM(x, y) memset((x), (y), sizeof(x))

#define MEN(x, y, z) memset((x), (y), sizeof((x)[0]) * (z))

#define Diz(x) (lower_bound(dz.begin(), dz.end(), (x)) - dz.begin())

#define DISCRETE(x) (x).push_back(INT_MIN), sort((x).begin(), (x).end()), (x).erase(unique((x).begin(), (x).end()), (x).end())

#ifdef LOCAL

#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;

#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;

void PRT() {} template <typename S, typename... T> void PRT(S x, T ... y){ cout << " " << x; PRT(y...); }

void PRTS() {} template <typename S> void PRTS(S x) { for (auto v : x) cout << " " << v; }

#define TIME cout << "RunTime: " << clock() << "ms\n", 0

#define __int128_t long long

#else

#define DEBUG(...) ;

#define DEBUGS(...) ;

#define TIME 0

#endif

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3f;

const int MOD = 1e9 + 7;

template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }

template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }

template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % MOD; if (a < 0) a += MOD; }

template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % MOD; }

template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }

template<typename T> inline T Gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; } return a; }

template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }

template<typename T> inline T MDis(T x1, T y1, T x2, T y2){ return abs(x1 - x2) + abs(y1 - y2); }

template<typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2){ return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); }

inline double EDis(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); }

inline ll Pow(ll a, ll n) { ll t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; }

#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;

inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }

//emplace unordered 能ll就开 函数内判非法

int main()

{

#ifdef LOCAL

	//freopen("C:/input.txt", "r", stdin);

	//freopen("C:/output.txt", "w", stdout);

#endif

	return TIME;

}


//#pragma GCC optimize(3)

#include <random>

#include <stdio.h>

#include <bits/stdc++.h>

#include <unordered_set>

#include <unordered_map>

using namespace std;

typedef long long ll;

typedef unsigned int uint;

typedef unsigned long long ull;

typedef pair<ll, ll> pll;

typedef pair<ll, int> pli;

typedef pair<int, ll> pil;

typedef pair<int, int> pii;

#define Continue(x) { x; continue; }

#define Break(x) { x; break; }

#define Return(x) { x; return; }

#define P2(x) ((x) * (x))

#define lowbit(x) ((x) & -(x))

#define SZ(x) ((int)(x).size())

#define ALL(x) (x).begin(), (x).end()

#define MEM(x, y) memset((x), (y), sizeof(x))

#define MEN(x, y, z) memset((x), (y), sizeof((x)[0]) * (z))

#define Diz(x) (lower_bound(dz.begin(), dz.end(), (x)) - dz.begin())

#define DISCRETE(x) (x).push_back(INT_MIN), sort((x).begin(), (x).end()), (x).erase(unique((x).begin(), (x).end()), (x).end())

#ifdef LOCAL

#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;

#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;

void PRT() {} template <typename S, typename... T> void PRT(S x, T ... y){ cout << " " << x; PRT(y...); }

void PRTS() {} template <typename S> void PRTS(S x) { for (auto v : x) cout << " " << v; }

#define TIME cout << "RunTime: " << clock() << "ms\n", 0

#define __int128_t long long

#else

#define DEBUG(...) ;

#define DEBUGS(...) ;

#define TIME 0

#endif

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3f;

const int MOD = 1e9 + 7;

template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }

template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }

template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % MOD; if (a < 0) a += MOD; }

template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % MOD; }

template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }

template<typename T> inline T Gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; } return a; }

template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }

template<typename T> inline T MDis(T x1, T y1, T x2, T y2){ return abs(x1 - x2) + abs(y1 - y2); }

template<typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2){ return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); }

inline double EDis(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); }

inline ll Pow(ll a, ll n) { ll t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; }

#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;

inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }

//emplace unordered 能ll就开 函数内判非法

const int N = 2e3 + 100;

struct node

{

	int t, d, p, id;

	bool operator < (const node &o) const

	{

		return d > o.d;

	}

}a[N];

int f[N][N]; //完成的任务 从当前时间开始 最大收益

int main()

{

#ifdef LOCAL

	freopen("C:/input.txt", "r", stdin);

	//freopen("C:/output.txt", "w", stdout);

#endif

	//不能一维直接记录转移!!

	int n;

	cin >> n;

	for (int i = 1; i <= n; ++i)

		scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p), a[i].id = i;

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n; ++i)

		for (int j = 0; j <= 2000; ++j)

		{

			Max(f[i][j], f[i - 1][j]);

			if (j < a[i].d - a[i].t)

				Max(f[i][j], f[i - 1][j + a[i].t] + a[i].p);

		}

	cout << f[n][0] << endl;

	vector<int> v;

	int i = n, j = 0;

	while (i >= 1 && j <= 2000 && f[i][j])

	{

		if (j < a[i].d - a[i].t && f[i][j] == f[i - 1][j + a[i].t] + a[i].p) //当前是从i转移 需要判断d-t

			v.push_back(a[i].id), j += a[i].t;

		--i;

	}

	cout << SZ(v) << endl;

	for (int x : v)

		printf("%d ", x);

	cout << endl;

	;;;

	return TIME;

}


//#pragma GCC optimize(3)
#include <random>
#include <stdio.h>
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
#define Continue(x) { x; continue; }
#define Break(x) { x; break; }
#define Return(x) { x; return; }
#define P2(x) ((x) * (x))
#define lowbit(x) ((x) & -(x))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MEM(x, y) memset((x), (y), sizeof(x))
#define MEN(x, y, z) memset((x), (y), sizeof((x)[0]) * (z))
#define Diz(x) (lower_bound(dz.begin(), dz.end(), (x)) - dz.begin())
#define DISCRETE(x) (x).push_back(INT_MIN), sort((x).begin(), (x).end()), (x).erase(unique((x).begin(), (x).end()), (x).end())
#ifdef LOCAL
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {} template <typename S, typename... T> void PRT(S x, T ... y){ cout << " " << x; PRT(y...); }
void PRTS() {} template <typename S> void PRTS(S x) { for (auto v : x) cout << " " << v; }
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128_t long long
#else
#define DEBUG(...) ;
#define DEBUGS(...) ;
#define TIME 0
#endif

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % MOD; if (a < 0) a += MOD; }
template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % MOD; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline T Gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; } return a; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }
template<typename T> inline T MDis(T x1, T y1, T x2, T y2){ return abs(x1 - x2) + abs(y1 - y2); }
template<typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2){ return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); }
inline double EDis(double x1, double y1, double x2, double y2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2){ return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); }
inline ll Pow(ll a, ll n) { ll t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; }
#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }
//emplace unordered 能ll就开 函数内判非法

const int N = 2e3 + 100;
struct node
{
	int t, d, p;
	bool operator < (const node &o) const
	{
		return d > o.d;
	}
}a[N];
int f[N], g[N]; //从当前时间开始的最大收益 记录转移

int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
	//freopen("C:/output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
		scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; ++i)
		for (int j = 0; j < a[i].d - a[i].t; ++j)
			if (Max(f[j], f[j + a[i].t] + a[i].p))
				g[j] = i;
	cout << f[0] << endl;
	vector<int> v;
	int t = 0;
	while (g[t])
	{
		v.push_back(g[t]);
		while (g[t + 1] == g[t])
			++t;
		t += a[g[t]].t;
	}
	cout << SZ(v) << endl;
	for (int x : v)
		printf("%d ", x);
	cout << endl;

	return TIME;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=2100000000;
int s,t,ans,i,j,k,n,m,a[310001],mid,b[310001],g[310001],l,r;
bool check(int x){
	int i,j,k=0;
	for (i=1;i<=m+1;i++){
		if (g[i]-g[i-1]>x) return 0;
		k+=min(x-g[i]+g[i-1],g[i]-g[i-1]);
	}
	return k>=2LL*(ll)s-t;
}
int main(){
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for (k=0,i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]),k=max(k,b[i]);
	for (i=1;i<=m;i++) scanf("%d",&g[i]);
	sort(g+1,g+1+m);g[0]=0;g[m+1]=s;
	if (!check(k)){
		puts("-1");return 0;
	}
	for (l=0,r=k;l<r-1;){
		mid=((ll)l+(ll)r)/2LL;
		if (check(mid)) r=mid;else l=mid+1;
	}
	k=(check(l))?l:r;
	for (i=1,ans=inf;i<=n;i++)
	 if (b[i]>=k) ans=min(ans,a[i]);
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn=2e5+10;
typedef pair<int ,int > mypair;
mypair mp[maxn],p[maxn];
long long l[maxn];
int si[maxn];

bool cmp(const mypair &a,const mypair &b)
{
    return a.first<b.first || (a.first==b.first && a.second > b.second);
}

int solve(const mypair &car,const int &k)
{
    long long t=0;
    long long v=car.second;
    for(int i=0;i<=k;i++){
        t+=max(l[i],3*l[i]-v);
        //printf("%lld>>%lld\n",l[k],max(l[k],3*l[k]-v));
    }
    //printf("%lld:%lld\n",v,t);
    return t;
}

int main ()
{
    int n,k,s,t;
    while(scanf("%d %d %d %d",&n,&k,&s,&t)!=EOF){
        for(int i=0;i<n;i++)
            scanf("%d %d",&mp[i].first,&mp[i].second);
        sort(mp,mp+n,cmp);
        int pos=0;
        p[0]=mp[0];
        for(int i=1;i<n;i++){
            if(mp[i].second>p[pos].second){
                pos++;
                p[pos]=mp[i];
            }
            //printf(">>>%d\n",pos);
        }
        int a=0,b;
        long long maxl=0;
        for(int i=0;i<k;i++)
            scanf("%d",si+i);
        sort(si,si+k);
        for(int i=0;i<k;i++){
            b=si[i];
            l[i]=b-a;
            maxl=max(maxl,l[i]);
            a=b;
        }
        l[k]=s-a;
        maxl=max(maxl,l[k]);
#if 0
        for(int i=0;i<n;i++)
            printf("%d %d\n",mp[i].first,mp[i].second);
        for(int i=0;i<=k;i++)
            printf("%d ",l[i]);
        for(int i=0;i<=pos;i++)
            printf("%d %d\n", p[i].first, p[i].second);
#endif
        a=-1;
        b=pos+1;
        while(b-a>1){
            int c=(a+b)>>1;
            if(p[c].second >= maxl && solve(p[c], k)<=t)
                b=c;
            else
                a=c;
        }
        if(b==pos+1)puts("-1");
        else printf("%d\n",p[b].first);
    }
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define FILL(a,x) memset(a,x,sizeof(a))
#define foreach( gg,ii ) for( typeof(gg.begin()) ii=gg.begin();ii!=gg.end();ii++)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define sz(a) int((a).size())
#define N 200010
#define MAX 30
#define mod 1000000007
#define REP(i,a) for(int i=0;i<a;++i)
#define REPP(i,a,b) for(int i=a;i<b;++i)
#define all(a) a.begin(),a.end()
const ll INF = 1e18+1;

ll c[N],v[N],g[N],cum[N];
vector <ll> d;

int main()
{
	ll n,k,s,t,ans=mod,val,x;
	scanf("%lld%lld%lld%lld",&n,&k,&s,&t);
	REP(i,n)
		scanf("%lld%lld",&c[i],&v[i]);
	REP(i,k) scanf("%lld",&g[i]);
	g[k]=0;
	g[k+1]=s;
	sort(g,g+k+2);
	REP(i,k+1)
		d.pb(g[i+1]-g[i]);
	sort(all(d));
	cum[0]=d[0];
	REPP(i,1,sz(d)) cum[i]=cum[i-1]+d[i];
	REP(i,n)
	{
		if (v[i]>=d[0])
		{
			if (v[i]<=2*d[0])
				val=3*cum[sz(d)-1]-(sz(d)-1)*v[i];
			else
			{
				x=upper_bound(all(d),v[i]/2)-d.begin();
				if (x==sz(d)) val=cum[sz(d)-1];
				else
					val=3*cum[sz(d)-1]-2*cum[x-1]-(sz(d)-x)*v[i];
			}
			if (val<=t) ans=min(ans,c[i]);
		}
	}
	if (ans==mod) printf("-1\n");
	else printf("%lld\n",ans);
return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define FILL(a,x) memset(a,x,sizeof(a))
#define foreach( gg,ii ) for( typeof(gg.begin()) ii=gg.begin();ii!=gg.end();ii++)
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define sz(a) int((a).size())
#define N 200010
#define MAX 30
#define mod 1000000007
#define REP(i,a) for(int i=0;i<a;++i)
#define REPP(i,a,b) for(int i=a;i<b;++i)
#define all(a) a.begin(),a.end()
const ll INF = 1e18+1;

ll c[N],v[N],g[N],cum[N];
vector <ll> d;

int main()
{
	ll n,k,s,t,ans=mod,val,x;
	scanf("%lld%lld%lld%lld",&n,&k,&s,&t);
	REP(i,n)
		scanf("%lld%lld",&c[i],&v[i]);
	REP(i,k) scanf("%lld",&g[i]);
	g[k]=0;
	g[k+1]=s;
	sort(g,g+k+2);
	REP(i,k+1)
		d.pb(g[i+1]-g[i]);
	sort(all(d));
	cum[0]=d[0];
	REPP(i,1,k+1) cum[i]=cum[i-1]+d[i];
	REP(i,n)
	{
		if (v[i]>=d[k])
		{
			if (v[i]<=2*d[0])
				val=3*cum[k]-(k+1)*v[i];
			else
			{
				x=upper_bound(all(d),v[i]/2)-d.begin();
				if (x==k+1) val=cum[k];
				else
					val=3*cum[k]-2*cum[x-1]-(k+1-x)*v[i];
			}
			if (val<=t) ans=min(ans,c[i]);
			// printf("%d\n",val);
		}
	}
	if (ans==mod) printf("-1\n");
	else printf("%lld\n",ans);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for(int i = (s); i < (e); i++)
#define FOE(i,s,e) for(int i = (s); i <= (e); i++)
#define FOD(i,s,e) for(int i = (s); i >= (e); i--)
#define ll long long
#define pb push_back

int n, m, x, y, z, k, w, need, ans, tmp, s, t;
int A[200005], B[200005], G[200005];
int high, low, mid;

int main ()
{
	scanf("%d %d %d %d", &n, &k, &s, &t);
	FOR(i, 0, n) scanf("%d %d", &A[i], &B[i]);
	FOR(i, 0, k) scanf("%d", &G[i]);
	sort(G, G + k);

	high = 20;
	low = 0;

	need = s * 2 - t;
	if (need <= 0)
	{
		ans = A[0];
		FOR(i, 0, n) ans = min(ans, A[i]);
		printf("%d\n", ans);
		return 0;
	}

	G[k] = s;

	while (high - low > 1)
	{
		mid = (high + low) / 2;

		x = 0; y = 1;
		tmp = 0;

		FOR(i, 0, k + 1)
		{
			if ((G[i] - x) * 2 <= mid) tmp += G[i] - x;
			else if ((G[i] - x) > mid) { y = 0; break; }
			else tmp += (mid - (G[i] - x));
			x = G[i];
		}

		if (y == 0) low = mid;
		else if (tmp >= need) high = mid;
		else low = mid;
	}

	ans = 1234567890;
	FOR(i, 0, n) if (B[i] >= high) ans = min(ans, A[i]);

	if (ans == 1234567890) ans = -1;
	printf("%d\n", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for(int i = (s); i < (e); i++)
#define FOE(i,s,e) for(int i = (s); i <= (e); i++)
#define FOD(i,s,e) for(int i = (s); i >= (e); i--)
#define ll long long
#define pb push_back

int n, m, x, y, z, k, w, need, ans, tmp, s, t;
int A[200005], B[200005], G[200005];
int high, low, mid;

int main ()
{
	scanf("%d %d %d %d", &n, &k, &s, &t);
	FOR(i, 0, n) scanf("%d %d", &A[i], &B[i]);
	FOR(i, 0, k) scanf("%d", &G[i]);
	sort(G, G + k);

	high = 1000000001;
	low = 0;

	need = s * 2 - t;
	if (need <= 0)
	{
		ans = A[0];
		FOR(i, 0, n) ans = min(ans, A[i]);
		printf("%d\n", ans);
		return 0;
	}

	G[k] = s;

	while (high - low > 1)
	{
		mid = (high + low) / 2;

		x = 0; y = 1;
		tmp = 0;

		FOR(i, 0, k + 1)
		{
			if ((G[i] - x) * 2 <= mid) tmp += G[i] - x;
			else if ((G[i] - x) > mid) { y = 0; break; }
			else tmp += (mid - (G[i] - x));
			x = G[i];
		}

		if (y == 0) low = mid;
		else if (tmp >= need) high = mid;
		else low = mid;
	}

	ans = 1234567890;
	FOR(i, 0, n) if (B[i] >= high) ans = min(ans, A[i]);

	if (ans == 1234567890) ans = -1;
	printf("%d\n", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for(int i = (s); i < (e); i++)
#define FOE(i,s,e) for(int i = (s); i <= (e); i++)
#define FOD(i,s,e) for(int i = (s); i >= (e); i--)
#define ll long long
#define pb push_back

int n, m, x, y, z, k, w, need, ans, tmp, s, t;
int A[200005], B[200005], G[200005];
int high, low, mid;

int main ()
{
	scanf("%d %d %d %d", &n, &k, &s, &t);
	FOR(i, 0, n) scanf("%d %d", &A[i], &B[i]);
	FOR(i, 0, k) scanf("%d", &G[i]);
	sort(G, G + k);

	high = 1000000001;
	low = 0;

	need = s * 2 - t;

//	printf("need acc %d\n", need);

	if (need <= 0) need = 0;

	G[k] = s;

	while (high - low > 1)
	{
		mid = (high + low) / 2;

		x = 0;
		y = 1;
		tmp = 0;

		FOR(i, 0, k + 1)
		{
			if ((G[i] - x) * 2 <= mid) tmp += (G[i] - x);
			else if ((G[i] - x) > mid) { y = 0; break; }
			else tmp += (mid - (G[i] - x));
			x = G[i];
		}

		if (y == 0) low = mid;
		else if (tmp >= need) high = mid;
		else low = mid;
	}

//	printf("need capa %d\n", high);

	ans = 1234567890;
	FOR(i, 0, n) if (B[i] >= high) ans = min(ans, A[i]);

	if (ans == 1234567890) ans = -1;
	printf("%d\n", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int INF=2e9+1,t,k;
int b[200020];
int cal(int dis,int cap)
{
    if(dis>cap) return INF;
    if(dis==cap) return 2*cap;
    if(dis*2<=cap) return dis;
    return dis*2-(cap-dis);
}
bool ch(int x)
{
    long long int ans=0;
    for(int i=1;i<k;i++)
    {
        ans+=cal(b[i+1]-b[i],x);
    }
    if(ans<=t) return true;
    return false;
}
typedef pair<int,int> ii;
ii a[200010];
int main()
{
    int n,s,imin=INF;
    //freopen("test.inp","r",stdin);
    scanf("%d %d %d %d",&n,&k,&s,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a[i].second,&a[i].first);
    }
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&b[i]);
    }
    k+=2; b[k-1]=0; b[k]=s;
    sort(b+1,b+1+k);
    int l=1,r=1e9+20;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(ch(mid)) r=mid;
        else l=mid+1;
    }
    for(int i=1;i<=n;i++) {
        if(l<=a[i].first) imin=min(imin,a[i].second);
    }
    cout<< (imin==INF?-1:imin);
}


#include<bits/stdc++.h>
using namespace std;
vector<int> ans;
int b;
typedef pair<int,int> ii;
queue<ii> qu;
void OUT()
{
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++) printf("%d ",ans[i]);
}
int solve(int last,int i)
{
    if(last==0) return 0;
    qu.push(ii(last,i));
    return (last-1)/b+1;
}
int main()
{
    int n,a,k,cnt=0,last2=0;
    string s;
    //freopen("test.inp","r",stdin);
    scanf("%d %d %d %d",&n,&a,&b,&k);
    cin>>s;
    int last=0;
    string tmp=s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='1'){
            for(last=max(last,i-b+1);last<=i;last++) tmp[last]='1';
            //for(last2=max(last,i);last2<=i+b-1;last2++) tmp[last2]='1';
        }
    }
    s=tmp;
    s+='1';
    last=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='1') cnt+=solve(last,i),last=0;
        else last++;
    }
    while(cnt>=a)
    {
        ans.push_back(qu.front().second); cnt--;
        if(qu.front().second>b) qu.front().second-=b,qu.front().first-=b;
        else qu.pop();
    }
    OUT();
}


#include<bits/stdc++.h>
using namespace std;
int INF=2e9+1,t,k;
int b[200020];
int cal(int dis,int cap)
{
    if(dis>cap) return INF;
    if(dis==cap) return 2*cap;
    if(dis*2<=cap) return dis;
    return dis*2-(cap-dis);
}
bool ch(int x)
{
    long long int ans=0;
    for(int i=1;i<k;i++)
    {
        ans+=cal(b[i+1]-b[i],x);
    }
    if(ans<=t) return true;
    return false;
}
typedef pair<int,int> ii;
ii a[200010];
int main()
{
    int n,s,imin=INF;
    //freopen("test.inp","r",stdin);
    scanf("%d %d %d %d",&n,&k,&s,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a[i].second,&a[i].first);
    }
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&b[i]);
    }
    k+=2; b[k-1]=0; b[k]=s;
    sort(b+1,b+1+k);
    int l=0,r=1e9;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(ch(mid)) r=mid;
        else l=mid+1;
    }
    for(int i=1;i<=n;i++) {
        if(l<=a[i].first) imin=min(imin,a[i].second);
    }
    cout<< (imin==INF?-1:imin);
}


#include<bits/stdc++.h>
using namespace std;
int INF=2e9+1,t,k;
int b[200020];
int cal(int dis,int cap)
{
    if(dis>cap) return INF;
    if(dis==cap) return 2*cap;
    if(dis*2<=cap) return dis;
    return dis*2-(cap-dis);
}
bool ch(int x)
{
    long long int ans=0;
    for(int i=1;i<k;i++)
    {
        ans+=cal(b[i+1]-b[i],x);
    }
    if(ans<=t) return true;
    return false;
}
typedef pair<int,int> ii;
ii a[200010];
int main()
{
    int n,s,imin=INF;
    //freopen("test.inp","r",stdin);
    scanf("%d %d %d %d",&n,&k,&s,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&a[i].second,&a[i].first);
    }
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&b[i]);
    }
    k+=2; b[k-1]=0; b[k]=s;
    sort(b+1,b+1+k);
    int l=0,r=2e9;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(ch(mid)) r=mid;
        else l=mid+1;
    }
    for(int i=1;i<=n;i++) {
        if(l<=a[i].first) imin=min(imin,a[i].second);
    }
    cout<< (imin==INF?-1:imin);
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <deque>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <complex>
#include <cstring>
#include <cfloat>
#include <iomanip>
#include <stack>
#include <bitset>

using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define repd(i, a, b) for(int i = (a); i > (b); i--)
#define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define ft(a) __typeof((a).begin())
#define ll long long
#define ld long double
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define bc(n) __builtin_popcountll(n)
#define pii pair<int, int>

typedef complex<ld> cplex;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

const int N = 200000 + 7;
const int M = 1007;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * inf + 7;
const double pi = acos(-1);
const double eps = 1e-6;
const bool multipleTest = false;

int n, k, s, t;
int c[N], v[N];

ll g[N];

bool canGo(ll x) {

    ll curTime = 0;
    ll curGas = x;

    for(int i = 0; i <= k; ++i) {
        {
            ll diff = g[i + 1] - g[i];

            if ((g[i + 1] - g[i]) * 2 <= curGas) {
                curTime += g[i + 1] - g[i];
                curGas = x;
            } else {
                if (curGas < g[i + 1] - g[i]) return false;

                curTime += diff * 2 - curGas % diff;
                curGas = x;
            }

//            cout << diff << " " << curTime << "\n";
        }
    }
//    cout << curTime << "\n";
    return curTime <= t;
}

void solve() {
    cin >> n >> k >> s >> t;

    for(int i = 0; i < n; ++i) scanf("%d%d", c + i, v + i);

    for(int i = 1; i <= k; ++i) {
        scanf("%I64d", g + i);
    }
    g[0] = 0;
    g[k + 1] = s;
    sort(g, g + k + 2);

//
//    cout << canGo(6) << "\n";
//    return;
////
    ll mnLimit = inf;
    ll lo = 0, hi = inf;
    while (lo <= hi) {
        ll g = (0ll + lo + hi) >> 1;

        if (canGo(g)) {
            mnLimit = g;
            hi = g - 1;
        } else lo = g + 1;
    }

    int ans = inf;

    for(int i = 0; i < n; ++i)
        if (v[i] >= mnLimit) {
            ans = min(ans, c[i]);
        }

    if (ans == inf) ans = -1;
    cout << ans << "\n";
}

void createTest() {
    freopen("in.txt", "w", stdout);
    cout << 100000 << "\n";
    for(int i = 1; i <= 100000; ++i) {
        cout << rand() % inf << " ";
    }

}

int main() {
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin);
    clock_t t1 = clock();
#endif
    //            createTest();
    //            return 0;
    //    freopen("out.txt", "w", stdout);

    if (fopen("f.in", "r")) {
        freopen("f.in", "r", stdin);
        freopen("f.out", "w", stdout);
    }

    int Test = 1;
    if (multipleTest) {
        cin >> Test;
    }

    for(int i = 0; i < Test; ++i) {
        //        printf("Case #%d: ", i + 1);
        solve();
    }

#ifdef _LOCAL_
    cout<<"\n" << 1.0 * (clock() - t1) / CLOCKS_PER_SEC<<endl;
#endif

}