WA on #3 #4,但是通过了讨论区所有hack
求dlhack qwq
#include<bits/stdc++.h>
using namespace std;
namespace IO{
#define ll long long
#define ischar(ch) (32<ch&&ch<127)
#define SIZE 1<<18
char ibuf[SIZE],*is=ibuf,*it=ibuf;
#define getchar() (is==it&&(it=(is=ibuf)+fread(ibuf,1,SIZE,stdin),is==it)?EOF:*is++)
template<typename Type>
inline void read(Type &a){
Type x=0;int y=0,ch=getchar();
while(!isdigit(ch)&&(ch^EOF))y|=(ch=='-'),ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
a=y?-x:x;
}
inline void read(char &a){
int ch=getchar();
while(!ischar(ch)&&(ch^EOF))ch=getchar();
a=ch;
}
inline void read(string &a){
a="";char ch;
while(!ischar(ch)&&(ch^EOF))ch=getchar();
while(ischar(ch))a+=ch,ch=getchar();
}
inline void read(short &a){
int ch=getchar();
while(!isdigit(ch)&&(ch^EOF))ch=getchar();
a=(ch^48);
}
template<typename Type>
inline void read(Type a[]){
Type *pos=a;
int ch=getchar();
while(!ischar(ch)&&(ch^EOF))ch=getchar();
while(ischar(ch))*pos++=ch,ch=getchar();
*pos=0;
}
inline void read(double &a){
int zh=0,w=0,ch=getchar();
double xi=0.0,x=1.0;
while (!isdigit(ch)&&(ch^EOF))w|=ch=='-',ch=getchar();
while(isdigit(ch))zh=(zh<<1)+(zh<<3)+(ch^48),ch=getchar();
if(ch^'.')return void(a=w?-zh:zh);
ch=getchar();
while(isdigit(ch))x*=10.0,xi+=(ch^48)*1.0/x,ch=getchar();
a=w?-zh-xi:zh+xi;
}
template<typename Type,typename ...Args>
inline void read(Type &a,Args &...as){read(a);read(as...);}
char obuf[SIZE],*op=obuf;
inline void putch(const char &c){
if(op-obuf==SIZE)fwrite(obuf,1,op-obuf,stdout),op=obuf;
*op++=c;
}
template<typename Type>
inline void write(Type x){
if(x<0)putch('-'),x=-x;
static int s[35],top;top=0;
do s[top++]=x%10,x/=10;
while(x);
while(top)putch(s[--top]+'0');
}
int precision;
inline void write(double x){
ll n=1,base=10,k=precision;
while(k)n=(k&1)?n*base:n,base*=base,k>>=1;
x=round(x*n)/n;
if(!x){
putch('0'),putch('.');
for(int i=1;i<=precision;++i)putch('0');
return;
}
if(x<0)putch('-'),x=-x;
ll y=(ll)round(x*n)%n;x=(ll)x;
write((ll)x);putch('.');
int s[35],top=0;
for(;top<precision;y/=10)s[++top]=y%10;
while(top)putch(s[top--]+'0');
}
inline void write(char ch){putch(ch);}
inline void write(const char* s){for(int i=0;i<strlen(s);++i)putch(s[i]);}
inline void write(string s){for(int i=0;i<s.size();++i)putch(s[i]);}
template<typename Type,typename ...Args>
inline void write(Type a,Args ...as){write(a);write(as...);}
inline void oclear(){fwrite(obuf,1,op-obuf,stdout);}
#undef SIZE
#undef ischar
}
using namespace IO;
#define N 10010
int n,idx,endt[N],L[N],R[N],T,ans2;
basic_string<int>vis;//内存
#define pait pair<int,int>
priority_queue<pait,vector<pait>,greater<pait> >q;//处理队列
inline void print(){
for(int v:vis)cerr<<v<<' ';cerr<<'\n';
}
inline int find(int len){
for(int i=1;i<vis.size();i+=2)
if(len<=vis[i]-vis[i-1]-1)return vis[i-1]+1;
return -1;
}
struct node{
int t,m,p;
};
queue<node>tmp;
inline void ins(node x){
int t=x.t,m=x.m,p=x.p;
// cout<<T<<' '<<"ins: "<<x.t<<' '<<x.m<<' '<<x.p<<'\n';
++idx;
if(t<T)t=T;
L[idx]=find(m);
R[idx]=L[idx]+m-1;
vis.insert(lower_bound(vis.begin(),vis.end(),L[idx]),L[idx]);
vis.insert(lower_bound(vis.begin(),vis.end(),R[idx]),R[idx]);
endt[idx]=t+p;
// cout<<"endtime: "<<endt[idx]<<'\n';
q.emplace(pait{endt[idx],idx});
}
inline void erase(int u){
vis.erase(vis.find(L[u]),1);
vis.erase(vis.find(R[u]),1);
}
int main(){
read(n);
vis+=-1;
vis+=n;
for(int t,m,p;1;){
read(t,m,p);
if(!t&&!m&&!p)break;
while(q.size()&&q.top().first<=t){
T=q.top().first;
erase(q.top().second);
q.pop();
if(q.size()&&q.top().first==T)continue;
while(tmp.size()&&(~find(tmp.front().m)))
ins(tmp.front()),tmp.pop();
}
T=t;
if(~find(m))ins({t,m,p});
else tmp.emplace(node{t,m,p}),++ans2;
// print();
}
while(q.size()){
T=q.top().first;
erase(q.top().second);
q.pop();
while(tmp.size()&&(~find(tmp.front().m)))
ins(tmp.front()),tmp.pop();
}
write(T,'\n',ans2);
oclear();
return 0;
}