Code:
#include<iostream>
#include<climits>
#include<algorithm>
using namespace std;
const int MAXN=110,MAXT=1010;
struct Node{
int t,h,l;
/*
bool operator<(Node B) const{
return t<B.t;
}
*/
}s[MAXN];
int D,N,ans,longgest;
bool r[MAXN][MAXT][MAXN],YN=false;
bool cmp(Node a,Node b){
return a.t<b.t;
}
void DFS(int num,int life,int height){
if(r[num][life][height]||YN)return;//剪枝
if(height>=D){//到达顶部
ans=s[num-1].t;
YN=true;
return;
}
longgest=max(longgest,life);
r[num][life][height]=true;
if(life>=s[num].t){//存活
DFS(num+1,life+s[num].l,height);
DFS(num+1,life,height+s[num].h);
}
return;
}
int main(){
scanf("%d%d",&D,&N);
for(int i=1;i<=N;i++)scanf("%d%d%d",&s[i].t,&s[i].l,&s[i].h);
s[N+1].t=INT_MAX;
sort(s+1,s+N+1,cmp);
r[0][10][0]=true;
DFS(1,10,0);
if(YN)printf("%d",ans);
else printf("%d",longgest);
return 0;
}