#include <stdio.h>
#include <stdlib.h>
#define SWAP(a,b,c) ((c)=(b), (b)=(a), (a)=(c))
struct process
{
int p_num;
int a_time;
int b_time;
}p[50];
int main(void)
{
FILE *in,*out;
in=fopen("fcfs.inp", "r");
out=fopen("fcfs.out", "w");
int p_c=0;
int i,j,temp=0,result=0, temp1=0;
int a;
if(in==NULL)
{
printf("file open error!\n");
exit(1);
}
fscanf(in,"%d",&p_c);
if(p_c>50)
{
fprintf(out,"허용 범위를 넘었습니다.\n");
return 0;
}
for(int i=0; i<p_c; i++)
{
fscanf(in,"%d",&p[i].p_num);
fscanf(in,"%d",&p[i].a_time);
fscanf(in,"%d",&p[i].b_time);
}
// arrival 순으로 정렬
for(i=p_c-1; i>0; i--)
{
for(j=0; j<i; j++)
{
if(p[j].a_time > p[j+1].a_time ){
SWAP(p[j].a_time, p[j+1].a_time, temp);
SWAP(p[j].b_time, p[j+1].b_time, temp);
SWAP(p[j].p_num, p[j+1].p_num, temp);
}
else if(p[j].a_time == p[j+1].a_time && p[j].p_num > p[j+1].p_num){
SWAP(p[j].a_time, p[j+1].a_time, temp);
SWAP(p[j].b_time, p[j+1].b_time, temp);
SWAP(p[j].p_num, p[j+1].p_num, temp);
}
}
}
for(a=0; a<p_c; a++){
if(temp1 < p[a].a_time)
{
while(temp1!=p[a].a_time)
temp1+=1;
}
result += temp1 - p[a].a_time;
temp1+=p[a].b_time ;
}
fprintf(out,"%d",result);
fclose(in);
fclose(out);
return 0;
}