# The Morning Commute Codechef Solution

The Chef commutes to work every day using the city’s underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the station nearest to his restaurant.

The Chef doesn’t want to change the route he took before, so he simply has to find out how long it will take to reach his restaurant along his usual route. This route is given by a sequence of stations s_{0}, s_{1}, …, s_{n} where s_{0} is the station where the Chef enters the metro and s_{n} is the station where the Chef exits the metro.

Trains are scheduled to run between every two consecutive stations s_{i-1} and s_{i}. Such a schedule is specified by three integers x_{i}, l_{i}, and f_{i}. This means that the first train on this line starts operating at time x_{i}. The time it takes this train to travel from s_{i-1} and s_{i} is exactly l_{i} units. Finally, a train departs from station s_{i-1} every f_{i} minutes following the previous train. That is, a train departs at time x_{i}, x_{i}+f_{i}, x_{i}+2f_{i}, and so on.

The Chef is very experienced at navigating the metro so the time it takes him to transfer between trains at a given station is essentially zero. Thus, if the Chef arrives at a station, say s_{i}, the moment that the train from s_{i} to s_{i+1} is scheduled to depart, he skillfully hops on this next train. However, if the Chef arrives when no train to s_{i+1} is scheduled to depart, he must wait until the scheduled departure time.

Help the Chef figure out how long it will take him to travel from station s_{0} to station s_{n}. You may assume that the Chef is already at station s_{0} at time 0.

### Input

The first line consists of a single integer denoting the number of test cases (at most 50). Each test case begins with a line containing a single integer n between 1 and 1000 indicating the number of lines the Chef must traverse (so there are n+1 stations s_{0}, s_{1}, …, s_{n}). The next n lines describe the train schedules between stations, one per line. The i’th such line gives the values x_{i}, l_{i}, and f_{i} for the train that travels between stations s_{i-1} and s_{i}.

The x_{i} values will be between 0 and 1000 and the l_{i} and f_{i} values will be between 1 and 1000.

### Output

For each test case you are to output a single integer denoting the minimum time t for which the Chef can reach station s_{n} using the given route. Remember, the Chef starts at s_{0} at time 0.

### Example

Input:3 2 0 4 7 0 6 5 2 0 1 2 6 2 10 2 1 2 3 0 2 3Output:11 8 5

## The Morning Commute – CodeChef Solution in JAVA

import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Codechef { public static void main (String[] args) throws java.lang.Exception { try { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n; n=sc.nextInt(); int x[]=new int[n]; int l[]=new int[n]; int f[]=new int[n]; int i; int a,b,c; for(i=0;i<n;i++) { a=sc.nextInt(); b=sc.nextInt(); c=sc.nextInt(); x[i]=a; l[i]=b; f[i]=c; } int time=0; i=0; int y=0; while(i<n) { if(time<=x[i]) { time=x[i]+l[i]; i++; } else { if((time-x[i])%f[i]==0) { time+=l[i]; } else { y=(time-x[i])/f[i]; y++; y=x[i]+y*f[i]; time=y+l[i]; } i++; } } System.out.print(time); System.out.print("\n"); } } catch(Exception ee) { } } }

## The Morning Commute- CodeChef Solution in CPP

#include <bits/stdc++.h> using namespace std; #define ll long long int int main() { int t; int x,l,f,n; cin>>t; while(t--){ cin>>n; cin>>x>>l>>f; ll ans=x+l; for(int i=1;i<n;i++){ cin>>x>>l>>f; if(x<ans){ ll tmp=(ll)((ans-x)/f); if(((ans-x)%f)!=0){ tmp++; } x+=(f*tmp); } ans=x+l; } cout<<ans<<endl; } return 0; }

## The Morning Commute -CodeChef Solution in Python

t=int(input()) ans=[] for _ in range(t): n=int(input()) schedule=[] for i in range(n): l=list(map(int,input().split())) schedule.append(l) time=schedule[0][0]+schedule[0][1] for i in range(1,len(schedule)): if time<schedule[i][0]: time+=((schedule[i][0]-time)+schedule[i][1]) elif time==schedule[i][0]: time+=schedule[i][1] else: if (time-schedule[i][0])%schedule[i][2]==0: time+=schedule[i][1] else: wait=((time-schedule[i][0])%schedule[i][2]) time+=(schedule[i][2]-wait)+schedule[i][1] ans.append(time) for _ in ans: print(_)

Disclaimer: The above Problem (The Morning Commute) is generated by CodeChef but the solution is provided by Codeworld19.This tutorial is only for **Educational** and **Learning** purpose.