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: Accepted

Solution code:
N = int(input())
CSF = [[] for T in range(0,N-1)]
for TN in range(0,N-1):
    CSF[TN] = [int(T) for T in input().split()]
for TN in range(0,N):
    Time = 0
    for TT in range(TN,N-1):
        if Time>=CSF[TT][1]:
            Time += CSF[TT][0] + (CSF[TT][2]-(Time%CSF[TT][2]))%CSF[TT][2]
        else:
            Time += CSF[TT][0] + (CSF[TT][1]-Time)
    print(Time)

Submission status: Accepted

Solution code:
n = int(input())
csf = [list(map(int, input().split())) for _ in range(n-1)]
ans = []
for i in range(n-1):
    time = 0
    for p in range(i, n - 1):
        c, s, f = csf[p]
        if s >= time:
            time = s+c
        else:
            d = time - s
            time = s + f * (-(-d // f)) + c
    ans.append(time)

ans.append(0)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
(n, ), *q = [[*map(int, i.split())] for i in open(0)]
ans = [0] * n
for i, p in enumerate(q):
    c, s, f = p
    for j in range(i):
        ans[j] = c + max(s, -(-ans[j] // f) * f)
    ans[i] = c + s
print(*ans, sep="\n")

Submission status: Accepted

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)

for i in range(N):
    t = 0
    for j in range(i, N-1):
        if t < S[j]:
            t = S[j]
        elif t % F[j] != 0:
            t += F[j]-t%F[j]
        t += C[j]
    print(t)

Submission status: Accepted

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

Submission status: Accepted

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

def shortest_path(start,end,times):
    if start == end:
        return times
    elif times<=graph[start][2]:
        times = (graph[start][2]+graph[start][1])
        return shortest_path(start+1,end,times)
    elif times%graph[start][3]==0:
        times += (graph[start][1])
        return shortest_path(start+1,end,times)
    else:
        times = times+graph[start][3]-times%graph[start][3]+graph[start][1]
        return shortest_path(start+1,end,times)


for i in range(n):
    print(shortest_path(i,n-1,0))

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    n = getN()
    den = []
    ans = [0 for i in range(n)]
    for i in range(n-1):
        den.append(getList())

    den.append([0,0,0])

    # for i in range(1,n):
    #     tgt = n - i - 1
    #     tou = den[tgt][1] + den[tgt][0]
    #     if tou <= den[tgt+1][1]:
    #         ans[tgt] = ans[tgt+1]
    #
    for i in range(n-1):
        cur = 0
        for j in range(i, n-1):
            if cur <= den[j][1]:
                cur = den[j][1]
            cur = int(math.ceil(cur/den[j][2]) * den[j][2])
            cur += den[j][0]
        ans[i] = cur

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


def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[18]:


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


# In[19]:


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


# In[ ]:

Submission status: Accepted

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

arrtime=lambda i,cs: max(csf[i][2]*(cs//csf[i][2]+(cs%csf[i][2]>0)),csf[i][1])+csf[i][0]
for i in range(n-1):
  arrivN=csf[i][1]
  for j in range(i,n-1):
    arrivN=arrtime(j,arrivN)
  print(arrivN)
print(0)

Submission status: Accepted

Solution code:
#c
N=int(input())
CSF = [None]*(N-1)
for i in range(N-1):
    CSF[i] = list(map(int, input().split()) )

for I in range(N):
    now = 0
    for csf in CSF[I:]:
        now = max( -(-now//csf[2])*csf[2] , csf[1]) + csf[0]
    print(now)

Submission status: Accepted

Solution code:
def est_time(sta,N,csf_list):
  SUM = 0
  for i in range(sta,N):
    if i == sta:
      SUM += csf_list[i-1][0] + csf_list[i-1][1]
    else:
      if SUM >= csf_list[i-1][1]:
        SUM = ((SUM+csf_list[i-1][2]-1) // csf_list[i-1][2])*csf_list[i-1][2]
        SUM += csf_list[i-1][0]
      else:
        SUM = csf_list[i-1][1]
        SUM += csf_list[i-1][0]
  return SUM

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

for i in range(N):
  print(est_time(i+1,N,csf_list))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = lambda:sys.stdin.readline().strip()
    
    N = int(input())
    #C:所要時間,S:開始後,F:間隔
    CSF = [list(map(int,input().split())) for _ in range(N-1)]

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

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
csf = []
for i in range(n - 1):
    csf.append(list(map(int, input().split())))
for i in range(n):
    t = 0
    for j in range(n - i - 1):
        c, s, f = (csf[i + j][k] for k in range(3))
        t = (max(t - 1, s - 1) // f + 1) * f + c
    print(t)

Submission status: Accepted

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

for i in range(N-1):
    now = 0
    for j in range(i,N-1):
        now = max(C[j][1], -(-now//C[j][2])*C[j][2])
        now += C[j][0]
    print(now)
print(0)

Submission status: Accepted

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

for i in range(n-1):
    total = 0
    for j in range(i, n-1):
        if total < inp[j][1]:
            total = inp[j][1]
        while total % inp[j][2] != 0:
            total += 1
        total += inp[j][0]
    print(total)
print(0)

Submission status: Accepted

Solution code:
import math
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)


for i in range(N - 1):
    count = 0
    count += S[i]
    count += C[i]
    for j in range(i + 1, N - 1):
        if count < S[j]:
            count = S[j]
        else:
            count = math.ceil((count - S[j]) / F[j]) * F[j] + S[j]
        count += C[j]
    print(count)
print(0)

Submission status: Accepted

Solution code:
import math

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

for i in range(N):
    t = 0

    for c, s, f in CSF[i:]:
        # 待ち時間
        if t < s:
            t = s
        else:
            t = math.ceil((t - s) / f) * f + s

        # i -> i+1に移動
        t += c

    print(t)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read


def main():
    N, *CSF = map(int, read().split())
    train = [0] * (N - 1)
    for i, t in enumerate(zip(*[iter(CSF)] * 3)):
        train[i] = t

    ans = [0] * N
    for i in range(N - 1):
        t = 0
        for c, s, f in train[i:]:
            if t < s:
                t = s + c
            elif t % f == 0:
                t += c
            else:
                t += f - (t % f) + c
        ans[i] = t

    print('\n'.join(map(str, ans)))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *CSF = map(int, read().split())
    train = [0] * (N - 1)
    for i, t in enumerate(zip(*[iter(CSF)] * 3)):
        train[i] = t

    ans = [0] * N
    for i in range(N - 1):
        t = 0
        for c, s, f in train[i:]:
            if t < s:
                t = s + c
            else:
                t += f - ((t - 1) % f + 1) + c
        ans[i] = t

    ans[N - 1] = 0
    print(*ans, sep='\n')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
c,s,f=[],[],[]
for _ in range(n-1):
    x,y,z=map(int,input().split())
    c.append(x)
    s.append(y)
    f.append(z)

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

Submission status: Accepted

Solution code:
#a以上のbの倍数の最小値
def baisu(a,b):
  if a%b==0:
    return a
  else:
    return ((a//b)+1)*b
C=list()
S=list()
F=list()
N=int(input())
for i in range(N-1):
  c,s,f=map(int,input().split())
  C.append(c)
  S.append(s)
  F.append(f)
L = [[0 for j in range(N)] for i in range(N)]
for i in range(N-1):
  L[i][i]=S[i]
  for j in range(i+1,N):
    L[i][j]=baisu(max(L[i][j-1],S[j-1]),F[j-1])+C[j-1]
  print(L[i][-1])
print(0)

Submission status: Accepted

Solution code:
n=int(input())
num=[]
for i in range(n-1):
    num.append(list(map(int,input().split())))
for i in range(n-1):
    ans=0
    for j in range(i,n-1):
        if ans<=num[j][1]:
            ans=num[j][1]
        else:
            ans=num[j][1]+((-((ans-num[j][1])//-num[j][2]))*num[j][2])
        ans+=num[j][0]
    print(ans)
print(0)

Submission status: Accepted

Solution code:
N = int(input())
l = []
for i in range(N-1):
  c, s, f = map(int, input().split())
  l.append([c, s, f])
  
for i in range(N-1):
  time = l[i][1] + l[i][0]
  for j in range(i+1, N-1):
    if time-l[j][1] >= 0:
      wait = time % l[j][2]
      if wait == 0:
        time += l[j][0]
      else:
        time += l[j][2] - wait + l[j][0]
    else:
      time = l[j][1] + l[j][0]
  print(time)
print(0)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def cc(al):
	ret = 0
	for (c, s, f) in al:
		if ret <= s:
			ret = s+c
		else:
			st = ((ret + f-1)//f)*f
			ret = st + c
	print(ret)
		

def main():
	n = int(input())
	#if n==4:
	#	return 1/0
	#b , c = tin()
	#s = input()
	al = [lin() for _ in range(n-1)]
	for i in range(n):
		cc(al[i:])
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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


for i in range(n):
  s=0
  for j in range(i,n-1):
    s=max(t[j][1],((s-1)//t[j][2]+1)*t[j][2])+t[j][0]
  print(s)

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
ls = []
for i in range(n-1):
    c, s, f = MAP()
    ls.append([c, s, f])
for i in range(n-1):
    ans = 0
    for j in range(i, n-1):
        ans = max(ans, ls[j][1])
        ans = ls[j][0] + ( ans + ls[j][2] - 1 )// ls[j][2] * ls[j][2]
    print(ans)
print(0)

Submission status: Accepted

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 f(n, s, f):
    if n <= s:
        return s
    if n % f == 0:
        return n
    else:
        return n // f * f + f

for i in range(n-1):
    now = S[i] + C[i]
    for j in range(i+1, n-1):
        now = f(now, S[j], F[j]) + C[j]
    print(now)
print(0)

Submission status: Accepted

Solution code:
N = int(input())
trains = []
for _ in range(N - 1):
    cost, start, f = map(int, input().split())
    trains.append([cost, start, f])

# print(trains)
for station_index in range(N - 1):
    ans = 0
    for j in range(station_index, N - 1):
        cost, start, f = trains[j]
        if ans < start:
            ans = start + cost
        elif ans == start:
            ans += cost
        else:
            if (ans - start) % f == 0:
                ans += cost
            else:
                ans += (f - (ans - start) % f) + cost
    print(ans)
print(0)

Submission status: Accepted

Solution code:
from math import ceil
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())
for i in range(n):
  t=s[i]+c[i]
  if i<n-1:
    for j in range(i+1,n-1):
      if s[j]>=t:
        t=s[j]+c[j]
      else:
        t=ceil(t/f[j])*f[j]+c[j]
  print(t)

Submission status: Accepted

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

for j in range(0,N,1):
    t = 0
    for i in range(j,N-1,1):
        C,S,F = CSF[i]

        if t < S:#まだ１ポンも出てないなら待つしかない
            t = S
        
        if t%F != 0:
            t += F-t%F
        
        t += C

    print(t)

Submission status: Accepted

Solution code:
import math
N = int(input())

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

for i in range(N-1):
    total = 0
    for j in range(i,N-1):
        total = max(math.ceil(total/C[j][2])*C[j][2],C[j][1])
        total += C[j][0]
    print(total)

print(0)

Submission status: Accepted

Solution code:
(n,),*csf = [list(map(int, s.split())) for s in open(0)]

for j in range(n):
    t = 0
    for i in range(j,n-1):
        c,s,f = csf[i]
        t = max(t,s)
        t = s + (0-(s-t)//f * f)
        t += c
    print(t)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
def main():
    import sys

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

    n = int(input())
    c, s, f = [], [], []
    for i in range(n-1):
        ci, si, fi = map(int, input().split())
        c.append(ci)
        s.append(si)
        f.append(fi)
    ans = [0]*n
    
    for i in range(n-1):
        for j in range(i,n-1):
            ans[i] = max(ans[i]+(-ans[i]%f[j]), s[j])
            ans[i] += c[j]
    for x in ans:
        print(x)
        
            
                

             
            
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
C, S, F = [0]*(N-1), [0]*(N-1), [0]*(N-1)
for i in range(N-1):
    c, s, f = zz()
    C[i], S[i], F[i] = c, s, f

ans = [0]*N
ans[N-2] = C[N-2] + S[N-2]
C_sum = [0]*(N-1)
C_sum[N-2] = C[N-2]
for i in reversed(range(0, N-2)):
    C_sum[i] = C_sum[i+1] + C[i]

for i in range(0, N - 1):
    ans[i] = S[i] + C[i]
    for j in range(i+1, N - 1):
        if (ans[i] <= S[j]):
            wait_time = S[j] - ans[i]
        else:
            wait_time = (-ans[i] % F[j])
        ans[i] += (wait_time + C[j])

    # for i in reversed(range(0, N-2)):
    #     if(S[i] + C[i] <= S[i+1]):
    #         print("つける")
    #         ans[i] = S[i] + C[i] + ans[i+1]
    #     else:
    #         print("つけない")
    #         wait_time = F[i+1] - ((S[i] + C[i]) % F[i+1])
    #         print("wait_time", wait_time)
    #         ans[i] = S[i] + C[i] + ans[i+1] + wait_time

    # else:
    #     ans[i] = ans[i+1] + C[i] + F[i]*2

for ans_ in ans:
    print(ans_)

Submission status: Accepted

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

for i, [ci, si, fi] in enumerate(csf):
    time = si + ci
    for cj,sj,fj in csf[i + 1:]:
        time = max(sj, time)
        time += (fj - (time % fj)) % fj + cj
    print(time)
print(0)

Submission status: Accepted

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

import math
result = []
for i, item in enumerate(maps):
    cost = item[0] + item[1]
    for j in range(i+1, n-1):
        next_cost = maps[j][1]
        if cost <= next_cost:
            cost = next_cost + maps[j][0]
        else:
            gap = cost - next_cost
            gap = math.ceil(gap/maps[j][2])
            cost = next_cost + maps[j][2]*gap + maps[j][0]
    result.append(cost)

for i in result:
    print(i , flush=True)
print(0 , flush=True)

Submission status: Accepted

Solution code:
import math

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

cost = []

for i in range(N):
    time = 0
    for j in range(i, N-1):
        C, S, F = lines[j]
        if time < S:
            time = S + C
        else:
            time = F*math.ceil(time/F) + C
    cost.append(time)

for c in cost:
    print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]  
    n = int(input())
    a = []
    for _ in range(n-1):
        a.append([int(x) for x in input().split()])

    for i in range(n-1):
        c, s, f = a[i]

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

            cur += c
            
        print(cur)

    print(0)

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""
a b a
d c d
a b a
"""

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    L = []
    for i in range(N-1):
        c, s, f = MI()
        L.append([i, s])
        for j in range(i+1):
            k, t = L[j-1]
            if s <= t:
                if t % f == 0:
                    L[j-1] = [k, t+c]
                else:
                    t = t + f - (t % f)
                    L[j-1] = [k, t+c]
            else:
                L[j-1] = [k, s+c]
    # print(L)

    for (i, t) in L:
        print(t)
    print(0)
            
        
        
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def ceil(n, m):
    return -(-n // m)

N = int(input())
C, S, F = zip(*[[int(x) for x in input().split()] for _ in range(N - 1)])

t = [0] * N
for i in range(N - 1):
    # 駅iから出発する人
    for j in range(i, N - 1):
        t[i] = F[j] * ceil(t[i], F[j])  # 駅jから出発する時刻（S[j]=0の場合）
        t[i] = max(S[j], t[i]) # 駅jから出発する時刻
        t[i] = t[i] + C[j] # 駅j+1に到着する時刻

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

Submission status: Accepted

Solution code:
N = int(input())
a = N*[0]

for i in range(1,N):
  C,S,F = map(int,input().split())
  for j in range(i):
    a[j] = max(S,((F+a[j]-1)//F)*F)+C

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    trains = [NLI() for _ in range(N-1)]
    if N == 1:
        print(0)
        exit()

    for start in range(N-1):
        t = 0
        for now in range(start, N-1):
            C, S, F = trains[now]
            if t < S:
                t = S + C
            else:
                k = math.ceil((t-S)/F)
                t = S + k * F + C
        print(t)

    print(0)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(N):
    t = 0
    for j in range(i, N-1):
        if t < S[j]:
            t = S[j]
        elif t % F[j] == 0:
            pass
        else:
            t = t + F[j] - (t % F[j])
        t += C[j]
    print(t)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    n = int(input())
    train = []
    for _ in range(n - 1):
        c, s, f = map(int, input().split())
        train.append((c, s, f))
    
    for i in range(n):
        if i == n - 1:
            print(0)
            continue
        start = i + 1
        time = train[i][0] + train[i][1]
        while start < n - 1:
            if time < train[start][1]:
                time = train[start][0] + train[start][1]
            elif time % train[start][2] == 0:
                time += train[start][0]
            else:
                time += train[start][2] - (time % train[start][2])
                time += train[start][0]
            start += 1
        print(time)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
c=[];s=[];f=[]
for i in range(n-1):
    ci,si,fi=map(int,input().split())
    c.append(ci);s.append(si);f.append(fi)
for i in range(n-1):
    ans=s[i]+c[i]
    for j in range(i+1,n-1):
        tmp=ans
        if s[j]>=tmp:
            ans+=s[j]-tmp+c[j]
        else:
            ans+=f[j]-((tmp-s[j])%f[j] if (tmp-s[j])%f[j]!=0 else f[j])+c[j]
    print(ans)
print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# 整数の入力
N = int(input())
data = [list(map(int, input().split())) for _ in range(N-1)]
# print(N)
#print(data)


for i in range(N-1):
    t = 0
    for j in range(i, N-1):
        #print(j)
        c = data[j][0]
        s = data[j][1]
        f = data[j][2]
        if t < s: 
            t = s
        elif t % f == 0:
            pass
        else:
            t += f - (t%f)
        t += c
    print(t)
        

print(0)

Submission status: Accepted

Solution code:
N = int(input())
# 問題文では駅は1-indexだが以下では0-indexにして考えている
CSF = [list(map(int, input().split())) for _ in range(N - 1)]
for i in range(N):
    ans = 0
    for j in range(i, N - 1):
        # i,i+1,...,N-1番の駅からの電車について考える
        c, s, f = CSF[j][0], CSF[j][1], CSF[j][2]

        # ans = 今いる駅に着いた時刻

        # 以下、まず待ち時間に着いて考える

        # 開通前に着いてしまったら開通まで待つしかない
        # 発射時刻はs
        if ans < s:
            ans = s
        else:
            # 待ち時間無しで次に乗れる場合
            if ans % f == 0:
                ans += 0
            # 待ち時間が発生する場合
            # ex. F=5でt=7に到着する場合、待ち時間は5-7%5=3
            else:
                ans += f - ans % f

        # 次の駅までc秒かかる
        ans += c
    # i=0,1,2,...,n-1の全てについて、i駅からかかった時間
    print(ans)

Submission status: Accepted

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

for i in range(n-1):
    time=0
    for j in range(i,n-1):
        if time<=lst[j][1]:
            time=lst[j][1]+lst[j][0]
            continue
        if time%lst[j][2]==0:
            time+=lst[j][0]
            continue
        time+=(lst[j][2]-time%lst[j][2])+lst[j][0]
    print(time)
print(0)