Problem:


Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0



Sample input:
3
6 5 1
1 10 1


Sample output:
12
11
0

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8
N = 0
C = []
S = []
F = []

# i駅での到着時間を指定すると、次の乗車時間が帰ってくる
def getNextTrain(i, time):
  t = S[i]
  while(1):
    if time <= 0:
      break
    if t >= time:
      break
    t += F[i]

  return t

# 入力部分
N = int(raw_input())
for i in range(N-1):
  c, s, f = map(int, raw_input().split())
  C.append(c)
  S.append(s)
  F.append(f)

# 処理部分
for k in range(N):
  arrive = 0
  for p in range(k, N-1): 
    # p駅から出発して
    arrive = getNextTrain(p, arrive)
    #print str(arrive) + '-> ' + str(p),
    arrive += C[p]
    #print '->' + str(arrive)
  print arrive

# 出力部分

Submission status: Time Limit Exceeded

Solution code:
m = []

def solve(index):
    time = 0
    for i in m[index:]:
        if time <= i[1]:
            time = i[1]
            time += i[0]
        else:
            tmp = i[1]
            while True:
                if time <= tmp:
                    break
                else:
                    tmp += i[2]
            time = tmp
            time += i[0]
    return time

n = input()
for i in xrange(n - 1):
    m.append(map(int, raw_input().split(" ")))
for i in xrange(n):
    print solve(i)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8
N = 0
C = []
S = []
F = []

# i駅での到着時間を指定すると、次の乗車時間が帰ってくる
def getNextTrain(i, time):
  t = S[i]
  while(1):
    if t >= time:
      break
    t += F[i]

  return t

# 入力部分
N = int(raw_input())
for i in range(N-1):
  c, s, f = map(int, raw_input().split())
  C.append(c)
  S.append(s)
  F.append(f)

# 処理部分
for k in range(N):
  arrive = 0
  for p in range(k, N-1): 
    # p駅から出発して
    arrive = getNextTrain(p, arrive)
    #print str(arrive) + '-> ' + str(p),
    arrive += C[p]
    #print '->' + str(arrive)
  print arrive

# 出力部分

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C, S, F = [], [], []
for _ in range(N - 1):
    c, s, f = map(int, input().split())
    C.append(c)
    S.append(s)
    F.append(f)


def next_time(i, t):
    a = S[i]
    while a < t:
        a += F[i]
    return a


for i in range(N):
    time = 0
    for j in range(i, N - 1):
        time = next_time(j, time)
        time += C[j]

    print(time)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = []
S = []
F = []

for i in range(N-1):
    c, s, f = list(map(int, input().split()))
    C.append(c)
    S.append(s)
    F.append(f)

loop_num = N - 1

time_set = []

for i in range(N):
    if i == N-1:
        time_set.append(0)
    else:
        loop_num -= 1
        next_station = C[i] + S[i]
        start_index = i
        for l in range(loop_num):
            waiting_time = S[start_index+1]
            while waiting_time < next_station:
                waiting_time += F[start_index+1]
            next_station = waiting_time + C[start_index+1]
            start_index += 1
        time_set.append(next_station)

for time in time_set:
    print(time)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
table = [[0 for i in range(3)] for k in range(n-1)]
for i in range(n-1):
	c,s,f = map(int,input().split())
	table[i][0] = c
	table[i][1] = s
	table[i][2] = f
for i in range(n-1):
	total = table[i][1]+table[i][0]
	for k in range(i+1,n-1):
		if total <= table[k][1]:
			total = table[k][1] + table[k][0]
		else:
			times = 1
			while (times*table[k][2] + table[k][1]) < total:
				times += 1
			total = times*table[k][2] + table[k][1] + table[k][0]
	print(total)
print('0')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python

import sys

def main(argv):
    line = sys.stdin.readline()
    while line:
        N = int(line.rstrip("\n"))

        timestamps = [0]
        for i in xrange(N - 1):
            if i > 0:
                timestamps.append(0)

            c, s, f = map(
              lambda i: int(i), sys.stdin.readline().rstrip("\n").split(" "))

            for i in xrange(len(timestamps)):
                if timestamps[i] <= s:
                    timestamps[i] = s + c
                else:
                    next_s = s
                    while timestamps[i] > next_s:
                        next_s = next_s + f
                    timestamps[i] = next_s + c

        for timestamp in timestamps:
            print timestamp
        print 0

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
sta = []

for i in range(n - 1):
    c, s, f = map(int, input().split())
    sta += [dict(c = c, s = s, f = f)]

for i in range(n):
    time = 0
    while i < n - 1:
        n_time = sta[i]['s']
        while time > n_time:
            n_time += sta[i]['f']
        time = n_time + sta[i]['c']
        i += 1
    print(str(time))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
clis = [];slis =[];flis = []
for i in range(N-1):
    c,s,f = map(int,input().split())
    clis.append(c);slis.append(s);flis.append(f)
for i in range(N):
    count =0
    for i2 in range(N-i-1):
        s = slis[i+i2]
        f = flis[i+i2]
        i3 = 0
        while True:
            if s+f*i3 >= count:
                count = s+f*i3+clis[i+i2]
                break
            i3 += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
info = []
arrive = []
for i in range(N-1):
	info.append(list(map(int,input().split(" "))))


for i in range(N-1):
	C = info[i][0]
	S = info[i][1]
	F = info[i][2]
	next_arrive = []
	for j in arrive:
		if j <= S:
			next_arrive.append(S+C)
		else:
			if j % F == 0:
				next_arrive.append(j+C)
			else:
				cnt = S + F
				while cnt < j:
					cnt += F
				next_arrive.append(cnt+C)

	next_arrive.append(S+C)
	arrive = next_arrive[:]

arrive.append(0)

for ans in arrive:
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

stations = []

for i in range(n - 1):
    c, s, f = map(int, input().split())
    stations += [dict(c = c, s = s, f = f)]

for i in range(n - 1):
    time = stations[i]['s'] + stations[i]['c']
    i += 1
    while i < n - 1:
        n_time = stations[i]['s']
        while time > n_time:
            n_time += stations[i]['f']
        time = n_time + stations[i]['c']
        i += 1
    print(str(time))
print(str(0))

Submission status: Time Limit Exceeded

Solution code:
def do():

    N=int(input())
    inp=[]
    for j in range(N-1):
        inp.append(input())

    for i in range(N):
        ti=0
        for j in range(i,N-1):
            temp=list(map(int,inp[j].split(" ")))
            ti+=getTime(ti,temp[1],temp[2])
            ti+=temp[0]

        print(ti)

def getTime(a,s,i):
    ti=0
    while(True):
        if a<=s+i*ti:
            break
        ti+=1

    return (s+i*ti)-a



if __name__=='__main__':
    do()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
table = [[0 for i in range(3)] for k in range(n-1)]
for i in range(n-1):
	c,s,f = map(int,input().split())
	table[i][0] = c
	table[i][1] = s
	table[i][2] = f
sum = []
for i in range(n-1):
	total = table[i][1]+table[i][0]
	for k in range(i+1,n-1):
		if total <= table[k][1]:
			total = table[k][1] + table[k][0]
		else:
			times = 1
			while (times*table[k][2] + table[k][1]) < total:
				times += 1
			total = times*table[k][2] + table[k][1] + table[k][0]
	sum.append(total)
for i in range(n-1):
	print(sum[i])
print('0')

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# C:移動時間 S:start時間 F:運行間隔 SはFで割り切れる
CSF = [list(map(int, input().split())) for i in range(N-1)]

for i in range(N-1):
    time = 0
    time += CSF[i][1]
    for j in range(i, N-1):
        start = CSF[j][1]
        while True:
            if time <= start:
                break
            else:
                start += CSF[j][2]
        time += (start - time)
        time = max(time, CSF[j][1])
        time += CSF[j][0]

    print(time)

print(0)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

N = int(raw_input())
CSF = [map(int, raw_input().split()) for _ in range(N-1)]

for i in range(N-1):
    ans = CSF[i][0] + CSF[i][1]

    for j in range(i+1, N-1):
        t = CSF[j][1]
        while t < ans:
            t += CSF[j][2]

        ans = t+CSF[j][0]

    print ans

print 0

Submission status: Memory Limit Exceeded

Solution code:
N = int(input())
train = [[] for _ in range(N)]
take_time = []
for cnt in range(N - 1):
    x, y , z = map(int,input().split())
    take_time.append(x)
    for _ in range(10**5+1):
        train[cnt].append(y)
        y += z

from bisect import bisect_left
for place in range(N):
    now = 0
    for i in range(place,N - 1):
        index = bisect_left(train[i],now)
        now = train[i][index]
        now += take_time[i]
    print(now)

Submission status: Accepted

Solution code:
N=int(input())
C=[]
S=[]
F=[]
for i in range(N-1):
    CSF=list(map(int,input().split()))
    C.append(CSF[0])
    S.append(CSF[1])
    F.append(CSF[2])

ans=[0]*(N)

for i in range(N-1):
    t=0

    for j in range(i,N-1):
        m=-(-t//F[j])*F[j]
        t=max(S[j],m)
        t+=C[j]
    
    ans[i]=t

print(*ans,sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
csf = [list(map(int, input().split())) for _ in range(N-1)]

for i in range(N-1):
    t = csf[i][0]+csf[i][1]
    for j in range(i+1, N-1):
        if t <= csf[j][1]:
            t = csf[j][1]
        else:
            t = (t+csf[j][2]-1)//csf[j][2] * csf[j][2]
        t += csf[j][0]
    print(t)
print(0)

Submission status: Accepted

Solution code:
n = int(input())
c = [0] * n
s = [0] * n
f = [0] * n
for i in range(n - 1):
    c[i], s[i], f[i] = map(int, input().split())

ans = [0] * n
for i in range(n - 1):
    for j in range(i, n - 1):
        if ans[i] < s[j]:
            ans[i] = s[j]
        elif ans[i] % f[j] > 0:
            ans[i] += f[j] - ans[i] % f[j]
        ans[i] += c[j]

for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
n = int(input())
T = [0]*n
c,s,f = [0]*(n-1),[0]*(n-1),[0]*(n-1)
 
for i in range(n-1):
	c[i],s[i],f[i] = map(int,input().split())

for	i in range(n-1):
	T[i] = s[i] + c[i]
	for j in range(i+1,n-1):
		if T[i] > s[j]:
			T[i] += (-T[i])%f[j] + c[j]
		else:
			T[i] = s[j] + c[j]
	print(T[i])
print(0)

Submission status: Accepted

Solution code:
n = int(input())
T = [0]*n
c,s,f = [0]*(n-1),[0]*(n-1),[0]*(n-1)
 
for i in range(n-1):
	c[i],s[i],f[i] = map(int,input().split())

for	i in range(n-1):
	T[i] = s[i] + c[i]
	for j in range(i+1,n-1):
		if T[i] > s[j]:
			T[i] = -((-T[i]//f[j]))*f[j] + c[j]
		else:
			T[i] = s[j] + c[j]
	print(T[i])
print(0)

Submission status: Accepted

Solution code:
n=int(input())
C=[]
S=[]
F=[]
for i in range(n-1):
	c,s,f=map(int, input().split())
	C.append(c)
	S.append(s)
	F.append(f)

def d(x,t): #駅x時間ｔで移動するのにかかる時間
	if S[x]>=t:
		return C[x]+S[x]-t
	else:
		if (t-S[x])%F[x]==0:
			return C[x]
		else:
			return C[x]+F[x]-(t-S[x])%F[x]

for i in range(n-1):
	x=i
	ans=0
	for j in range(x,n-1):
		ans+=d(j,ans)
		x=+1
	print(ans)
print(0)

Submission status: Accepted

Solution code:
N = int(input())

CSF = []
for _ in range(N-1):
  C, S, F = map(int, input().split())
  CSF.append((C, S, F))

def cnt(x):
  ret = 0
  for i in range(x, N-1):
    C, S, F = CSF[i]
    if ret < S:
      ret = S
      ret += C
    else:
      ret = -(-ret//F)*F
      ret += C
  return ret

for x in range(N):
  print(cnt(x))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = list()
    for _ in range(n-1):
        c, s, f= map(int, input().split())
        inlis.append([c, s, f])

    anslis = []

    for j in range(n-1):
        tmp = 0
        for i in range(j,n-1):
            c, s, f= inlis[i][0], inlis[i][1], inlis[i][2]
            if tmp < s:
                tmp = s
            
            amari = tmp % f
            if amari != 0:
                tmp += f - amari

            tmp += c
            #print(j, c, s, f, amari, tmp)
        anslis.append(tmp)
    anslis.append(0)
    
    for k in anslis:
        print(k)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
CSF=[list(map(int,input().split())) for _ in range(N-1)]
for j in range(N):
    t=0
    for i in range(j,N-1):
        if t<CSF[i][1]:
            t=CSF[i][1]
        else:
            x=(t-1)//CSF[i][2]+1
            t=CSF[i][2]*x
        t+=CSF[i][0]
    print(t)

Submission status: Accepted

Solution code:
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def ceil(A, B):
    if A%B == 0:
        return A//B
    return A//B+1

def solve() -> Any:
    N = read_int()
    C, S, F = [], [], []
    for _ in range(N-1):
        c, s, f = read_ints()
        C.append(c)
        S.append(s)
        F.append(f)
    dp = [
        [0 for _ in range(N)] for _ in range(N)
    ]
    # dp[i][j] - minimum time for i-th train to arrive at j-th station
    for j in range(1, N):
        for i in range(j):
            if dp[i][j-1] <= S[j-1]:
                dp[i][j] = S[j-1]+C[j-1]
            else:
                k = ceil((dp[i][j-1]-S[j-1]), F[j-1])
                dp[i][j] = k*F[j-1]+S[j-1]+C[j-1]
                # k*F[j-1]+S[j-1] >= dp[i][j-1]
    for i in range(N):
        print(dp[i][N-1])


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():


    n = I()
    time = [0 for _ in range(n)]
    start = [0 for _ in range(n)]
    freq = [0 for _ in range(n)]

    for i in range(n-1):
        time[i], start[i], freq[i] = LI()

    ans = [0 for _ in range(n)]

    for i in range(n):
        before = 0
        after = 0
        for j in range(i,n-1):
            if before < start[j]:
                after = start[j]
            else:
                after = before - before%freq[j] + freq[j]*(before%freq[j] != 0)

            before = after + time[j]

        ans[i] = before

    print(*ans, sep="\n")



main()

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    L = [i_list() for i in range(n-1)]

    ans = []

    for l in range(n-1):
        time = L[l][1] + L[l][0]
        for j in range(n-l-2):
            t = l+j+1
            if time <= L[t][1]: # まだ出発していない
                time = L[t][1] + L[t][0]
            elif (time - L[t][1]) % L[t][2] == 0: # ちょうど出発タイミング
                time += L[t][0]
            else: # すでに出発はしていて、待ち時間発生
                s = (time - L[t][1])//L[t][2] + 1
                time = L[t][1] + L[t][2]*s + L[t][0]
        print(time)
    print(0)




if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
sys.setrecursionlimit(10**6)
n = int(input())

csf = [list(map(int, input().split())) for i in range(n-1)]

for i in range(n-1):
    csf_tmp = csf[i:]

    c, s, f = csf_tmp[0]

    t = c+s
    for j in range(1, len(csf_tmp)):
        c, s, f = csf_tmp[j]

        if t <= s:
            t = s
        else:
            mod = (t-s) % f
            if mod != 0:
                t += f-mod
        # print(t)
        t += c
        # print(t)
    print(t)
    # print()
print(0)

Submission status: Accepted

Solution code:
N=int(input())
CSF=[list(map(int,input().split())) for _ in range(N-1)]
for j in range(N):
    t=0
    for i in range(j,N-1):
        if t<CSF[i][1]:
            t=CSF[i][1]
        else:
            x=(t-1)//CSF[i][2]+1
            t=CSF[i][2]*x
        t+=CSF[i][0]
    print(t)

Submission status: Accepted

Solution code:
N = int(input())
lis = [[0, 0, 1]]
for i in range(N - 1):
    C, S, F = map(int, input().split())
    lis.append([C, S, F])

# i 駅スタート の電車
for i in range(N):
    C, S_ij, F = 0, 0, 1
    for j in range(i + 1, N):  # j 駅に着く
        C, S, F = lis[j]
        S = max(S, S_ij)  # max（j 駅のスタート時間、 ij 間の時間）
        S_ij = C + ((S - 1) // F + 1) * F
    print(S_ij)

Submission status: Accepted

Solution code:
from math import ceil

n = int(input())
cs = []
ss = []
fs = []

for _ in range(n-1):
    c, s, f = map(int, input().split())
    cs.append(c)
    ss.append(s)
    fs.append(f)

for i in range(n):
    t = 0
    for j in range(i, n-1):
        if t < ss[j]:
            t = ss[j]
        t = ceil(t / fs[j]) * fs[j]
        t += cs[j]
    print(t)

Submission status: Accepted

Solution code:
from math import ceil

n=int(input())
lst=[]
ans=[]

for i in range(n-1):
    x,y,z=map(int,input().split())
    lst.append([x,y,z])


for i in range(n-1):
    pin=0
    for c,s,f in lst[i:]:
        xxx=max(ceil((pin-s)/f),0)
        pin=s+xxx*f+c
    
    ans.append(pin)


for i in ans:
    print(i)

print(0)

Submission status: Accepted

Solution code:
N = int(input())

x = []
for i in range(N - 1):
    c, s, f = map(int, input().split())
    x.append([c, s, f])

ans = []
for i in range(N - 1):
    t = x[i][0] + x[i][1]
    for j in range(i + 1, N - 1):
        if t % x[j][2] == 0:
            t = max(t, x[j][1])
        else:
            t = max(t + (x[j][2] - t % x[j][2]), x[j][1])
        t += x[j][0]
    ans.append(t)
ans.append(0)

for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
n = int(input())

now = 0
ans = [0] * (n)
CSF = [list(map(int, input().split())) for _ in range(n - 1)]
for i in range(n - 1):
    now = 0
    for j in range(n - i - 1):
        c, s, f = CSF[i + j]
        if now <= s:
            now = s
        else:
            now = f * ((now + f - 1) // f)
        now += c
    ans[i] = now

print("\n".join(map(str, ans)))

Submission status: Accepted

Solution code:

def main():
    with open(0) as f:
        N = int(f.readline())
        train = [tuple(map(int, line.split())) for line in f.readlines()]
    
    ans = []
    for start in range(N-1):
        arrive = 0
        for station in range(start, N-1):
            c, s, f = train[station]
            #出発時間:区間[arrive,)の下限     
            departure = lambda arrive:(arrive+f-1)//f*f if arrive >= s else s
            arrive = departure(arrive) + c
        ans.append(arrive)
    ans.append(0)
    for x in ans: print(x)

main()

Submission status: Accepted

Solution code:
url = "https://atcoder.jp/contests/abc087/tasks/arc090_a"
import copy
def main():
    n = int(input())
    t = [list(map(int, input().split())) for _ in range(n-1)]
    for i in range(len(t)):
        time = t[i][0] + t[i][1]
        for j in range(i+1, len(t)):
            st = t[j]
            if time <= st[1]:
                time = st[1]
            elif time % st[2] == 0:
                pass
            else:
                time += st[2] - (time % (st[2]))
            time += st[0]
        print(time)
    print(0)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    CSF = [NLI() for _ in range(N-1)]
    CSF.append([0,0,1])

    
    for n in range(N-1):
        time = CSF[n][1]
        
        for m in range(n,N-1):
            time += CSF[m][0]
            if time < CSF[m+1][1]:
                time = CSF[m+1][1]
            else:
                if time % CSF[m+1][2] != 0:
                    time = ((time//CSF[m+1][2])+1)*CSF[m+1][2]
        print(time)
    print(0)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    d = [list(map(int, input().split())) for _ in range(N-1)]
    for i in range(N-1):
        measure_time(d[i], d[i+1:])
    print(0)


def measure_time(start, remain):
    t = 0
    t = start[1] + start[0]
    for r in remain:
        if t < r[1]:
            t = r[1]
        elif (t - r[1]) % r[2] != 0:
            t += r[2] - (t - r[1]) % r[2]
        t += r[0]
    print(t)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=[list(map(int,input().split())) for i in range(n-1)]
for i in range(n):
    ans=0
    for j in range(i,n-1):
        if ans%x[j][2]==0:
            a=0
        else:
            a=x[j][2]-(ans%x[j][2])
        ans=(max(ans+a,x[j][1]))
        ans+=x[j][0]
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
csf = [list(map(int,input().split())) for i in range(N-1)]
for i in range(N-1):
    ans = csf[i][1]+csf[i][0]
    for j in range(i+1,N-1):
        if ans <= csf[j][1]:
            ans = csf[j][1]
        else:
            f = ans%csf[j][2]
            if f != 0:
                ans += csf[j][2]-f
        ans += csf[j][0]
    print(ans)
print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

n = int(input())
c = [0 for _ in range(n-1)]
s = [0 for _ in range(n-1)]
f = [0 for _ in range(n-1)]
for i in range(n-1):
    c[i], s[i], f[i] = map(int, input().split())

t = [[-1 for _ in range(n)] for _ in range(n-1)]

for i in range(n-1):
    t[i][i] = s[i]

for i in range(n-1):
    for j in range(i, n): 
        if i != j:
            t[i][j] = t[i][j-1]+c[j-1]
            if j == n-1:
                break
            if t[i][j]-t[j][j] <= 0:
                t[i][j] = t[j][j]
            else:
                if (t[i][j]-t[j][j])%f[j] != 0:
                    t[i][j] = t[j][j] + ((t[i][j]-t[j][j])//f[j]+1) * f[j]


#print('t =', t)

# output 
for i in range(n-1):
    print(t[i][n-1])
print(0)

Submission status: Accepted

Solution code:
n=int(input());A=[0]*n
for i in range(1,n):
  c,s,f=map(int,input().split())
  for j in range(i):A[j]=max(-A[j]//f*-f,s)+c
print(*A,sep="\n")

Submission status: Accepted

Solution code:
# import numpy as np
import sys, math, heapq
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

N = int(input())
CSF = [list(map(int, input().split())) for _ in range(N - 1)]

for i in range(N - 1):
    c, s, f = CSF[i]
    time = s + c
    for csfj in CSF[i + 1 :]:
        c, s, f = csfj
        if time < s:
            time = s
        else:
            time = f * ((time + f - 1) // f)
        time += c
    print(time)

print(0)

Submission status: Accepted

Solution code:
N = int(input())
csf = [list(map(int, input().split())) for i in range(N - 1)]

for i in range(N):
    ans = 0
    for c, s, f in csf[i::]:
        if ans <= s:
            ans = s
        else:
            if ans % f == 0:
                pass
            else:
                ans = (ans // f + 1) * f
        ans += c
    print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    csf = [list(map(int,input().split())) for _ in range(n-1)]
    
    for i in range(n):
        time = 0
        while True:
            if i == n-1:
                print(time)
                break
            if time < csf[i][1]:
                time = csf[i][1] + csf[i][0]
            else:
                if time%csf[i][2] == 0:
                    time += csf[i][0]
                else:
                    time += csf[i][0] + csf[i][2] -  time%csf[i][2]
            i += 1

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n = int(input())
csf = [list(map(int,input().split())) for _ in range(n-1)]

for i in range(n):
    time = 0
    while True:
        if i == n-1:
            print(time)
            break
        if time < csf[i][1]:
            time = csf[i][1] + csf[i][0]
        else:
            if time%csf[i][2] == 0:
                time += csf[i][0]
            else:
                time += csf[i][0] + csf[i][2] -  time%csf[i][2]
        i += 1

Submission status: Accepted

Solution code:
n = int(input())
csf = [list(map(int,input().split())) for _ in range(n-1)]

for i in range(n):
    time = 0
    while True:
        if i == n-1:
            print(time)
            break
        if csf[i][1] <= time and time%csf[i][2] == 0:
            time += csf[i][0]
        elif csf[i][1] > time:
            time = csf[i][1] + csf[i][0]
        else:
            time += csf[i][2] - time%csf[i][2] + csf[i][0]
        i += 1

Submission status: Accepted

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    C = []
    S = []
    F = []
    for i in range(n - 1):
        c, s, f = nm()
        C.append(c)
        S.append(s)
        F.append(f)
    for i in range(n - 1):
        time = 0
        for j in range(i, n - 1):
            if time <= S[j]:
                time = S[j] + C[j]
            else:
                time = time + (F[j] - (time - S[j]) % F[j]) % F[j] + C[j]
        print(time)
    print(0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    times = []
    for _ in range(n - 1):
        times.append(input_int_list())

    for i in range(n - 1):
        cur = 0
        for j in range(i, n - 1):
            c, s, f = times[j]
            # 次の電車の出発時間を探索する
            time = s
            while time < cur:
                time += f
            cur = time + c
        print(cur)
    # はじめからゴールにいるケースを出力
    print(0)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N=intput()
L=[]
for _ in range(N-1):
    L.append(lint())
for i in range(N):
    tmp = 0
    for j in range(i,N-1):
        c,s,f=L[j]
        tmp = max(((tmp-1)//f+1)*f,s)+c
    print(tmp)