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:
import sys
input = sys.stdin.readline
import numpy as np

 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n = I()
mylist = tuple(tuple(MI()) for i in range(n-1))

def main(j):
    time = 0
    for i in range(j,n-1):
        c, s, f = mylist[i]
        if time < s:
            time = s
        if (time - s) % f != 0:
            time += (f - (time - s) % f) #待ち時間を足す
        time += c
    print(time)
    
    
for i in range(n-1):
    main(i)
print(0)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n = I()
mylist = [list(MI()) for i in range(n-1)]

def main(j):
    time = 0
    for i in range(j,n-1):
        c, s, f = mylist[i]
        if time < s:
            time = s
        if (time - s) % f != 0:
            time += (f - (time - s) % f) #待ち時間を足す
        time += c
    print(time)
    
    
for i in range(n-1):
    main(i)
print(0)

Submission status: Accepted

Solution code:
n = int(input())

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

# start ~ 最終駅行くまでにかかる時間
#   timeで合算の時間を扱う
def reach(start, time):
    if start == n-1:
        return time

    # 電車が開通していない場合は開通まで待つ
    if time <= csf[start][1]:
        time = csf[start][1]
    else:
        while time % csf[start][2] != 0:
            time += 1
    return reach(start+1, time + csf[start][0])

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
c=[]
s=[]
f=[]
if n==1:
    print(0)
    sys.exit()
for i 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):
    tmp=0
    while i<n-1:
        if tmp<=s[i]:
            a=s[i]
        elif (tmp-s[i])%f[i]==0:
            a=tmp
        elif tmp>s[i]:
            b=f[i]-(tmp-s[i])%f[i]
            a=tmp+b
        tmp=a+c[i]
        i+=1
    print(tmp)
print(0)

Submission status: Accepted

Solution code:
n=int(input())
L=[]
for _ in range(n-1):
    L.append(tuple(map(int,input().split())))
ans=[]
for i in range(n-1):
    c,t,f=L[i]
    t += c
    for j in range(i+1,n-1):
        cn,sn,fn=L[j]
        if t>sn:
            t=-int(-t//fn)*fn
        else:
            t=sn
        t += cn
    ans.append(t)
ans.append(0)
for i in ans:
    print(i)

Submission status: Accepted

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

for i in range(N-1):
    t = scf[i][1]+scf[i][0]
    for j in range(i,N-2):
        if t <= scf[j+1][1]:
            t = scf[j+1][1] + scf[j+1][0]
        else:
            #始発より後に到着したら、待つかどうかの判定を行う。
            q = (t-scf[j+1][1]) // scf[j+1][2]
            mod = (t-scf[j+1][1]) % scf[j+1][2]
            if mod == 0:
                t = t + scf[j+1][0]
            else:
                t = scf[j+1][1] + scf[j+1][2] * (q+1) + scf[j+1][0]
    print(t)
print(0)

Submission status: Accepted

Solution code:
N = int(input())

ans = [0] * N
for i in range(N-1):
  C, S, F = map(int, input().split())
  for j in range(i+1):
    if S >= ans[j]:
      ans[j] = S + C
    else:
      ans[j] = F * (1 + (ans[j]-1)//F) + C

for ansi in ans:
  print(ansi)

Submission status: Accepted

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

ans = 0
memo = {}
memoset = set()
def dfs(i, time):
    if i==N-1:
        return time
    c, s, f = CSF[i]
    if time<s:
        time = s
    elif time%f!=0:
        time = (time//f+1)*f
    if (time, i) in memoset:
        return memo[(time, i)]
    T = dfs(i+1, time+c)
    memo[(time, i)] = T
    memoset.add((time, i))
    return T

for i in range(N-1):
    print(dfs(i, 0))
print(0)

Submission status: Accepted

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

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

  print(t)
print(0)

Submission status: Accepted

Solution code:
def main():
    from itertools import islice

    N = int(input())

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

    ans = []
    for i in range(N - 1):
        curr = 0
        for c, s, f in islice(trains, i, N - 1, 1):
            if curr < s: curr = s
            r = curr % f
            if r != 0: curr += f - r
            curr += c
        ans.append(curr)
    ans.append(0)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from itertools import islice

    N = int(input())

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

    ans = [0] * N
    for i, (ci, si, fi) in enumerate(trains):
        curr = si + ci
        for j, (cj, sj, fj) in enumerate(islice(trains, i + 1, N, 1)):
            if curr < sj: curr = sj
            r = curr % fj
            if r != 0: curr += (fj - r)
            curr += cj
        ans[i] = curr

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(n):
    # from i
    # now = 1ループごとにnow
    now = 0
    for j in range(i, n - 1):
        (c, s, f) = a[j]
        if now <= s:
            now = s + c
        else:
            # matsu
            now = s + (now - s + f - 1) // f * f + c
    print(now)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = [list(map(int,input().split())) for _ in range(N-1)]
for i in range(N):
    t = 0
    for C,S,F in A[i:]:
        if t < S:
            t = S+C
        else:
            t = math.ceil((t-S)/F)*F+S+C
    print(t)

Submission status: Accepted

Solution code:
n = int(input())
loss = []

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [list(map(int,input().split())) for _ in range(N-1)]
A.insert(0,0)
B = [0 for _ in range(N+1)]
for i in range(1,N):
    C1,S1,F1 = A[i]
    T = S1
    for j in range(i+1,N):
        C2,S2,F2 = A[j]
        if T+C1>=S2:
            dt = (T+C1)%F2
            if dt>0:
                T += F2-dt+C1
            else:
                T += C1
        else:
            T = S2
        C1 = C2
        S1 = S2
        F1 = F2
    T += C1
    B[i] = T
for i in range(1,N+1):
    print(B[i])

Submission status: Accepted

Solution code:
N = int(input())
ans = [0 for i in range(N)]
import math
for i in range(N-1):
  c, s, f = map(int, input().split())
  for j in range(i+1):
    if ans[j]>=s:
      x = math.ceil((ans[j]-s)/f)
      ans[j] = s+f*x+c
    else:
      ans[j] = s+c
for i in range(N):
  print(ans[i])

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    CSF = [list(map(int, input().split())) for _ in range(n - 1)]

    for i in range(n):
        if i == n - 1:
            print(0)
            break
        c1, s1, _ = CSF[i]
        t = s1 + c1
        for j in range(i + 1, n - 1):
            c2, s2, f2 = CSF[j]
            if t <= s2:
                t = s2 + c2
            else:
                if t % f2 == 0:
                    wait = 0
                else:
                    wait = f2 - (t - s2) % f2
                t += c2 + wait
        print(t)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

ans = [0] * N
for i in range(N):
    time = 0
    for j in range(i, N - 1):
        c, s, f = CSF[j]
        d = max(0, time - s)
        ride = (d + f - 1) // f
        time = s + ride * f + c
    ans[i] = time

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

Submission status: Accepted

Solution code:
def examA():
    C1 = SI()
    C2 = SI()
    ans = "YES"
    if C1[0]!=C2[2] or C1[1]!=C2[1] or C1[2]!=C2[0]:
        ans = "NO"
    print(ans)
    return

def examB():
    A, B, C, X, Y = LI()
    loop = max(X,Y)
    ans = inf
    for i in range(loop+1):
        cost = i*2*C
        if X>i:
            cost += A*(X-i)
        if Y>i:
            cost += B*(Y-i)
        ans = min(ans,cost)
    print(ans)
    return

def examC():
    def gcd(x, y):
        if y == 0:
            return x
        while (y != 0):
            x, y = y, x % y
        return x
    N, x = LI()
    X = LI()
    if N==1:
        print(abs(X[0]-x))
        return
    for i in range(N):
        X[i] = abs(X[i]-x)
    now = gcd(X[0],X[1])
    for i in range(2,N):
        now = gcd(now,X[i])
    ans = now
    print(ans)
    return

def examD():
    N = I()
    C = [LI()for _ in range(N-1)]
    ans = [inf]*N
    for i in range(N):
        cur = 0
        for j in range(i,N-1):
            c, s, f = C[j]
            next = 0
            if cur<=s:
                cur = s+c
            else:
                cur = (1+(cur-1)//f)*f + c
        ans[i] = cur
    for v in ans:
        print(v)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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


def get(i):
    ans = 0
    for c, s, f in csf[i:]:
        ans = max(ans, s)
        ans = (ans + f - 1) // f * f
        ans += c
    return ans


for i in range(n):
    print(get(i))

Submission status: Accepted

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

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):
        if t <= L[j][1]:
            s = L[j][1]
        else:
            if t % L[j][2]:
                s = t + L[j][2] - (t % L[j][2])
            else:
                s = t
        t = s + L[j][0]
    print(t)
print(0)

Submission status: Accepted

Solution code:
N = int(input())

stations = [tuple(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, s, f = stations[j]
        t = max(t, s)
        if t % f != 0:
            t += f - t % f
        t += c
    print(t)
print(0)

Submission status: Accepted

Solution code:
N = int(input())
CC = [0] * (N-1)
SS = [0] * (N-1)
FF = [0] * (N-1)

for i in range(N-1):
    C, S, F = map(int,input().split())
    CC[i] = C
    FF[i] = F
    SS[i] = S

for i in range(N-1):
    T = 0
    Max = 0
    for k in range(i,N-1):
        Max = max(T, SS[k])
        U = Max % FF[k]
        if U == 0:
            T = Max + CC[k]
        else:
            T = Max - U + FF[k] + CC[k]
        #print(i,k,T)
    print(T)
print(0)

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):
    tmp = 0
    for j in range(i, n-1):
        for k in range(10**8):
            if tmp <= s[j] + f[j] * k:
                tmp = s[j] + f[j] * k + c[j]
                break
    else:
        print(tmp)

print(0)

Submission status: Accepted

Solution code:
n = int(input())
t = [0 for i in range(n)]
for i in range(n-1):
    c,s,f = map(int,input().split())
    for j in range(i+1):
        if t[j] <= s:
            t[j] = c + s
        elif t[j] % f == 0:
            t[j] += c
        else:
            t[j] += f - (t[j] % f) + c
for i in t:
    print(i)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    csf = [tuple(map(int, input().split())) for _ in range(n - 1)]
    r = [0] * n
    for i1 in range(n - 1):
        t = 0
        for i2 in range(i1, n - 1):
            if t <= csf[i2][1]:
                t = csf[i2][1]
            else:
                if t % csf[i2][2] != 0:
                    t = ((t // csf[i2][2]) + 1) * csf[i2][2]
            t += csf[i2][0]
        r[i1] = t

    p2d = lambda x: print(*x, sep="\n")
    p2d(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def mips():
    return map(int,input().split())
def ii():
    return int(input())
N = ii()
L = []
for i in range(N-1):
    C,S,F = mips()
    L.append([C,S,F])
for i in range(N-1):
    x = L[i][0] + L[i][1]
    j = i + 1
    while j != N-1:
        if x >= L[j][1] and x % L[j][2] == 0: #no waiting time :)
            x += L[j][0]
            j += 1
        else:
            if x < L[j][1]: #1st train will arrive shortly ;)
                x += (L[j][1]-x + L[j][0])
                j += 1
            else: #if x % L[j][2] != 0, 1st train gone, wait for next :-(
                while x % L[j][2] != 0:
                    x += 1
                x += L[j][0] #a train arrived, go to next station.
                j += 1
    print(x)
print(0)

Submission status: Accepted

Solution code:
from math import ceil
n = int(input())
lst = [list(map(int,input().split())) for _ in range(n-1)]
for i in range(n-1):
    ans = lst[i][0]+lst[i][1]
    for j in range(i+1, n-1):
        k = max(0, ceil((ans-lst[j][1])/lst[j][2]))
        ans = lst[j][1]+k*lst[j][2]+lst[j][0]
    print(ans)
print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[list(map(int,input().split())) for _ in range(n-1)]
ans=[]
for i in range(n):
    now=0
    for j in range(i,n-1):
        c,s,f=a[j]
        if s==now:
            k=0
        elif s>now:
            k=s-now
        else:
            if now%f!=0:
                k=f-now%f
            else:
                k=0
        now+=c+k
    print(now)

Submission status: Accepted

Solution code:
n = int(input())

csf = []

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

ans = []
for i in range(n - 1):
    tmp = csf[i][0] + csf[i][1]
    for j in range(i + 1, n - 1):
        if tmp < csf[j][1]:
            tmp = csf[j][0] + csf[j][1]
        elif (tmp - csf[j][1]) % csf[j][2] == 0:
            tmp += csf[j][0]
        else:
            tmp = (tmp // csf[j][2] + 1) * csf[j][2] + csf[j][0]
    ans.append(tmp)
ans.append(0)

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

Submission status: Accepted

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

def time(m):
    c, s, f = csf[m-1]
    depart_next = s
    arrive_next = s+c
    while m < n-1:
        nc, ns, nf = csf[m]
        i = 0
        while ns + nf*i < arrive_next:
            i += 1
        depart_next = ns + nf*i
        arrive_next = depart_next + nc
        m += 1
    return arrive_next


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

Submission status: Accepted

Solution code:
n=int(input())
csf=[list(map(int,input().split())) for _ in range(n-1)]
ans=[float("inf") for _ in range(n)]
ans[n-1]=0
for i in range(n-1):
  cnt=csf[i][1]+csf[i][0]
  for j in range(i+1,n-1):
    if csf[j][2]<cnt:
      if cnt%csf[j][2]==0:
        cnt+=csf[j][0]
      else:
        cnt+=csf[j][0]+csf[j][2]-cnt%csf[j][2]
    else:
      cnt+=cnt-csf[j][2]
  ans[i]=cnt
ans=ans[::-1]
for i in range(n-1):
  if ans[i]>ans[i+1]:
    ans[i+1]=ans[i]
for i in ans[::-1]:
  print(i)

Submission status: Accepted

Solution code:
n=int(input())
csf=[list(map(int,input().split())) for _ in range(n-1)]
ans=[float("inf") for _ in range(n)]
ans[n-1]=0
for i in range(n-1):
  cnt=csf[i][1]+csf[i][0]
  for j in range(i+1,n-1):
    if csf[j][2]<cnt:
      if cnt%csf[j][2]==0:
        cnt+=csf[j][0]
      else:
        cnt+=csf[j][0]+csf[j][2]-cnt%csf[j][2]
    else:
      cnt+=cnt-csf[j][2]
  ans[i]=cnt
ans=ans[::-1]
ans.append(float("inf"))
for i in range(n):
  if ans[i]>ans[i+1]:
    ans[i+1]=ans[i]
ans.pop(-1)
for i in ans[::-1]:
  print(i)

Submission status: Accepted

Solution code:
import math
N = int(input())
list_time = []
for i in range(N- 1):
  list_time.append(tuple(map(int, input().split())))
  
"""
駅iを時刻tに発ったとき，駅i+1に到着するのはt+ list_time[i- 1][0]
駅i+1を発つのはt+ list_time[i- 1][0]<= list_time[i][1]+ k* list_time[i][2] となる最小の
k，すなわち
k = max(0, math.ceil((t+ list_time[i- 1][0]- list_time[i][1])/ list_time[i][2]))
について時刻list_time[i][1]+ k* list_time[i][2]
"""

def lastdeparturetime(i, t):
  if(i == N- 1):
    return t
  else:
    k = max(0, math.ceil((t+ list_time[i- 1][0]- list_time[i][1])/ list_time[i][2]))
    T = list_time[i][1]+ k* list_time[i][2]
    return lastdeparturetime(i+ 1, T)
  
for i in range(1, N):
  arrivaltime = lastdeparturetime(i, list_time[i- 1][1])+ list_time[N- 2][0]
  print(arrivaltime)
  
print(0)

Submission status: Accepted

Solution code:
N = int(input())
A = list()
for i in range(N-1):
  A.append(list(map(int, input().split())))
for i in range(N):
  ans = 0
  j = i
  while(j < N-1):
    C = A[j][0]
    S = A[j][1]
    F = A[j][2]
    
    if(S > ans):
      ans = S
    else:
      if(ans % F != 0):
        ans = ((ans//F)+1)*F
    ans += C
    j += 1
    
  print(ans)

Submission status: Accepted

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

def to_next(start_time, sta):
    
    time = 0
    if dia[sta][1] > start_time:
        time += dia[sta][1] - start_time
    elif start_time % dia[sta][2] != 0:
        time += dia[sta][2] * (start_time // dia[sta][2] + 1) - start_time
    time += dia[sta][0]
    return time

def to_end(sta):
    time = 0
    while sta != N-1:
        time += to_next(time, sta)
        sta += 1
    return time
for i in range(N - 1):
    print(to_end(i))
print(0)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
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())

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

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    timeschedule = [na() for _ in range(n - 1)]

    for i in range(n):
        time = 0

        for j in range(i, n - 1):
            c, s, f = timeschedule[j]
            if time < s:
                time = s
            if (time - s) % f:
                time += f - ((time - s) % f)
            time += c

        print(time)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

from functools import lru_cache
@lru_cache(None)
def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    if t <= ss:
        return dfs(eki+1, ss+cc)
    else:
        m = -(-t//ff)
        return dfs(eki+1, ff*m+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
def calc_time_to_next_station(t, c, s, i):
  if t <= s:
    return s + c
  else:
    f = lambda x, y: (x + y - 1) // y
    return f(t-s, i) * i + s + c  


def calc_needed_time(i, cost, start, interval):
  """
  calculate the needed time to reach n-th station when we are at i-th station 
  Args:
    i (int): 0-indexed number
    cost (list)
    start (list)
    interval (list)
  """
  if i == n - 1:
    return 0
  m = len(start)
  t = 0
  for j in range(i, m):
    t = calc_time_to_next_station(t, cost[j], start[j], interval[j])
  return t
 



n = int(input())
cost, start, interval = [], [], []
for _ in range(n-1):
  c, s, i = map(int, input().split())
  cost.append(c)
  start.append(s)
  interval.append(i)


for i in range(n):
  print(calc_needed_time(i, cost, start, interval))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    if t <= ss:
        return dfs(eki+1, ss+cc)
    else:
        m = -(-t//ff)
        return dfs(eki+1, ff*m+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    if t <= ss:
        return dfs(eki+1, ss+cc)
    else:
        return dfs(eki+1, -(-t//ff)*ff+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    if t < ss:
        return dfs(eki+1, ss+cc)
    else:
        return dfs(eki+1, -(-t//ff)*ff+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    m = max(0,-(-t+ss)//ff)
    if t < ss:
        return dfs(eki+1, ss+cc)
    else:
        return dfs(eki+1, -(-t//ff)*ff+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    m = max(0,-(-t+ss)//ff)
    if t < ss:
        return dfs(eki+1, ss+cc)
    elif t%ff==0:
        return dfs(eki+1, t+cc)
    else:
        return dfs(eki+1, -(-t//ff)*ff+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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())

def dfs(eki,t):
    if eki == n-1:
        return t

    cc = c[eki]
    ss = s[eki]
    ff = f[eki]

    m = max(0,-(-t+ss)//ff)
    if t < ss:
        return dfs(eki+1, ss+cc)
    elif t%ff==0:
        return dfs(eki+1, t+cc)
    else:
        return dfs(eki+1, t+ff-(t%ff)+cc)

for i in range(n):
    print(dfs(i,0))

Submission status: Accepted

Solution code:

import numpy as np

N = int(input())

Cs = []
Ss = []
Fs = []
ans = [0] * N
ans_fix = [False] * N
for i in range(N-1):
    C,S,F = list(map(int, input().split()))
    Cs.append(C)
    Ss.append(S)
    Fs.append(F)

ans_fix[-1] = True

for i in range(N):
    for j in range(i, N-1):
        if i == 0:
            ans[j] = Ss[j] + Cs[j]
        elif not ans_fix[j-i]:
            if ans[j-i] <= Ss[j]:
                ans[j-i] = Ss[j]
            wait_time = Fs[j] - (ans[j-i] % Fs[j]) if ans[j-i] % Fs[j] != 0 else 0
            ans[j-i] += wait_time + Cs[j]

    ans_fix[N-2-i] = True

for a in ans:
    print(a)

Submission status: Accepted

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

for i in range(N):
    if i == N-1:
        print(0)
    else:
        t = S[i]
        for j in range(N-i-1):
            t = max(((t+F[i+j]-1)//F[i+j])*F[i+j], S[i+j])+C[i+j]
        print(t)

Submission status: Accepted

Solution code:
(n,),*d=[list(map(int,c.split()))for c in open(0)]
for i in range(n-1):
 t=0
 for j in range(i,n-1):c,s,f=d[j];t=[t+(f-t%f)%f,s][t<s]+c
 print(t)
print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
(n,), *d = [list(map(int, c.split())) for c in open(0)]
n -= 1
for i in range(n):
    t = 0
    for j in range(i, n):
        c, s, f = d[j]
        t = (s if t < s else t if t % f == 0 else t + f - t%f) + c
    print(t)
print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
"""
後ろから順番に漸化式でいけるかと思ったが、それぞれの駅での電車の間隔が一定でないので、むり
O(n**2)で順番に計算する。
"""


n = int(input())
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())
ans = [0] * (n - 1)

for i in range(n - 1):
    time = s[i] + c[i]  # 次の役につく時刻
    for j in range(i + 1, n - 1):
        time = max(math.ceil(time / f[j]) * f[j], s[j]) + c[j]
    ans[i] = time
    print(time)
print(0)

Submission status: Accepted

Solution code:
n = int(input())
data = []
for i in range(n-1):
    c,s,f = map(int,input().split())
    s //= f
    data.append((c,s,f))
for i in range(n):
    if i == n-1:
        print(0)
        break
    for j in range(i,n-1):
        if j == i:
            ans = data[j][0]+data[j][1]*data[j][2]
            continue
        ans = (max((ans-1)//data[j][2]+1,data[j][1])*data[j][2]) + data[j][0]
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
CSF = [tuple(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
        elif t % f != 0:
            t += f - t % f
        t += c
    print(t)

Submission status: Accepted

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

ans = [0] * n

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