Problem:


There are n processes in a queue. Each process has namei and timei.  The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)

First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)

Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)

Your task is to write a program which simulates the round-robin scheduling. 

Input

n q

name1 time1

name2 time2

...

namen timen

In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output

For each process, prints its name and the time the process finished in order.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 1000

1 ≤ timei ≤ 50000

1 ≤ length of namei ≤ 10

1 ≤ Sum of timei ≤ 1000000

Sample Input 1

5 100
p1 150
p2 80
p3 200
p4 350
p5 20

Sample Output 1

p2 180
p5 400
p1 450
p3 550
p4 800

Notes

Template in C



Sample input:
5 100
p1 150
p2 80
p3 200
p4 350
p5 20


Sample output:
p2 180
p5 400
p1 450
p3 550
p4 800

Submission status: Accepted

Solution code:
class ArrayDeque:
    def __init__(self):
        self.a_size = 1
        self.a = [None]
        self.n = 0
        self.head = 0
        self.tail = 0

    def __bool__(self):
        return self.n != 0

    def _resize(self):
        new_a = [None] * ((self.n + 1) << 1)
        for i in range(self.n):
            new_a[i] = self.a[(self.head + i) & (self.a_size - 1)]
        self.a = new_a
        self.a_size = (self.n + 1) << 1
        self.head = 0
        self.tail = self.n

    def append(self, val):
        if self.n == self.a_size - 1:
            self._resize()
        self.a[self.tail] = val
        self.tail = (self.tail + 1) & (self.a_size - 1)
        self.n += 1

    def appendleft(self, val):
        if self.n == self.a_size - 1: 
            self._resize()
        self.head = (self.head - 1) & (self.a_size - 1)
        self.a[self.head] = val
        self.n += 1

    def popleft(self):
        if self.n == 0:
            raise IndexError()
        val = self.a[self.head]
        self.head = (self.head + 1) & (self.a_size - 1)
        self.n -= 1
        if self.a_size >= 4 * self.n + 2:
            self._resize()
        return val

    def pop(self):
        if self.n == 0:
            raise IndexError()
        self.tail = (self.tail - 1) & (self.a_size - 1)
        val = self.a[self.tail]
        self.n -= 1
        if self.a_size >= 4 * self.n + 2:
            self._resize()
        return val


n, q = map(int, input().split())
info = [input().split() for i in range(n)]

que = ArrayDeque()
for name, time in info:
    que.append((name, int(time)))

ans_time = 0
ans = []
while que:
    name, time = que.popleft()
    if time > q:
        que.append((name, time - q))
        ans_time += q
    else:
        ans_time += time
        ans.append((name, ans_time))

for name, time in ans:
    print(name, time)

Submission status: Accepted

Solution code:
from collections import deque
a,b = map(int,input().split())
p = deque()
for i in range(a):
    v = input().split()
    p.append({v[0] : int(v[1])})

ans = 0
ans_list = []
while True:
    v = p.popleft()
    for k,i in v.items():
        if i - b <= 0:
            ans = ans + i
            l = {k:ans}
            ans_list.append(l)
        else:
            i = i -b
            v[k] = i
            p.append(v)
            ans = ans + b

    if len(p) == 0:
        break

for v in ans_list:
    for k,i in v.items():
        print('{} {}'.format(k,i))

Submission status: Accepted

Solution code:
import queue

n, q = map(int, input().split())
que = queue.Queue(n)

for i in range(n):
    name, time = input().split()
    que.put([name, int(time)])

c_time = 0
while True:
    try:
        name, time = que.get(block=False)
    except queue.Empty:
        break
    else:
        if time > q:
            que.put([name, time - q])
            c_time += q
        else:
            c_time += time
            print(name, c_time, sep=' ')

Submission status: Accepted

Solution code:
from typing import  Any


class FixedQueue:
    class Empty(Exception):
        pass

    class Full(Exception):
        pass

    def __init__(self, capacity: int) -> None:
        self.no = 0
        self.front = 0
        self.rear = 0
        self.capacity = capacity
        self.que = [None] * capacity

    def __len__(self) -> int:
        return  self.no

    def is_empty(self) -> bool:
        return self.no <= 0

    def is_full(self) -> bool:
        return  self.no >= self.capacity

    def enque(self, x: Any) -> None:
        if self.is_full():
            raise FixedQueue.Full
        self.que[self.rear] = x
        self.rear += 1
        self.no += 1
        if self.rear == self.capacity:
            self.rear = 0

    def deque(self) -> Any:
        if self.is_empty():
            raise FixedQueue.Empty
        x = self.que[self.front]
        self.front += 1
        self.no -= 1
        if self.front == self.capacity:
            self.front = 0
        return  x

current_time = 0
n, p = map(int, input().split())
Q = FixedQueue(n + 1)

for i in range(n):
    name, time = input().split()
    time = int(time)
    Q.enque([name, time])

while len(Q) > 0:
    u = Q.deque()
    c = min(p, u[1])
    u[1] -= c
    current_time += c
    if u[1] > 0:
        Q.enque(u)
    else:
        print(str(u[0]) + ' ' + str(current_time))

Submission status: Accepted

Solution code:
def readinput():
    n,q = list(map(int,input().split()))
    procs = []
    for i in range(n):
        name, strtime = input().split()
        inttime = int(strtime)
        procs.append([name, inttime])
    return n,q,procs

def dispach(t, q, procs):
    t0 = t
    #print(t0, t, procs)

    proc = procs.pop(0)
    procname = proc[0]
    tremain = proc[1]
    if (tremain > q):
        proc[1] = tremain - q
        procs.append(proc)
        t0 += q
    else:
        t0 += tremain
        print(procname+' '+str(t0))

    #print(t0, t, procs)
    return t0

def main(n,q,procs):
    t = 0
    #print(procs)
    while(len(procs)>0):
        t = dispach(t, q, procs)
        #print(t, procs)
    return

if __name__=='__main__':
    n,q,procs=readinput()
    #print(procs)
    main(n,q,procs)

Submission status: Accepted

Solution code:
time = 0
overtime = 0
n,q = map(int,input().split())
L = [input().split() for i in range(n)]

while True:
    if len(L) == 0:
        break
    else:
        s = L.pop(0)
        s[1] = int(s[1])
        if s[1] > q:
            s[1] = s[1] - q
            time += q
            L.append(s)
        else:
            time += s[1]
            print(s[0],time)

Submission status: Accepted

Solution code:
n_quant =  [int(t) for t in input().split()]
que = []
for i in range(n_quant[0]):
    tmp = input().split()
    que.append((tmp[0], int(tmp[1])))

time = 0
while len(que) > 0:

    (name, val) = que.pop(0)
    if(val > n_quant[1]):
        que.append((name, val - n_quant[1]))
        time += n_quant[1]
    else:
        time += val
        print(name, time)

Submission status: Accepted

Solution code:
import collections


def main():
    n, q = map(int, input().split())

    dq = collections.deque()
    for i in range(n):
        s = input().split()
        name = s[0]
        time = int(s[1])
        dq.append((name, time))

    cur_time = 0
    while len(dq) > 0:
        top = dq.popleft()
        # not finish
        if top[1] > q:
            dq.append((top[0], top[1] - q))
            cur_time += q
        else:
            print(top[0] + " " + str(cur_time+top[1]))
            cur_time += top[1]


main()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())

que = deque()
for _ in range(n):
    k, v = input().split()
    que.append([k, int(v)])

#%%
p_time = 0
while True:
    k, v = que.popleft() # 先頭にあるプロセスを取り出す
    if v <= q: # プロセス制限時間q以内に終わるならば、そのまま削除してこれまでの実行時間を表示
        p_time += v
        print(k, p_time)
    else: # プロセス制限時間q以内に終わらないときは、再びqueに追加する
        p_time += q
        que.append([k, v-q])

    if len(que) == 0: # すべてのqueがなくなったら終了
        break

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,input().split())
l = deque(list(map(str,input().split())) for _ in range(n))
t = 0
while l :
  a,b = l.popleft()
  b = int(b)
  if b<=q:
    t += b
    print(a,t)
  else:
    b -= q
    t += q
    l.append([a,b])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def roundRobin(task_list, q):
    total_time = 0
    task_result = []

    while len(task_list) > 0:
        task = task_list.pop(0)

        if task[1] > q:
            total_time += q
            task[1] -= q
            task_list.append(task)
        else:
            total_time += task[1]
            task_result.append([task[0], str(total_time)])

    for res in task_result:
        print(' '.join(res))


if __name__ == "__main__":
    n, q = [int(i) for i in input().split()]

    task_list = []

    for _ in range(n):
        key, value = input().split()
        task_list.append([key, int(value)])
    roundRobin(task_list, q)

Submission status: Accepted

Solution code:
from collections import deque

class task:
  
  def __init__(self,name,time):
    self.name = name
    self.time = time
    self.remain = time
  
  def getName(self):
    return self.name
  
  def getTime(self):
    return self.time
  
  def getRemain(self,wtime):
    self.remain -= wtime
    return self.remain

  

queue = deque()  

n, q = map(int,input().split())
for i in range(n):
  aN,aQ = input().split()
  aTask = task(aN,int(aQ))
  queue.append(aTask)

elapse = 0
remain = 0

while len(queue) > 0:
  aTask = queue.popleft()
  remain = aTask.getRemain(q)
  if remain <= 0 :
    elapse += remain + q
    print(aTask.getName() , elapse)
  else:
    elapse += q
    queue.append(aTask)

Submission status: Accepted

Solution code:
number=input().split()
n=int(number[0])
q=int(number[1])
answer=[]
time=0
head=0

Que=[input().split() for i in range(n)]
for i in range(n):
    Que[i][1]=int(Que[i][1])
    Que[i].append(0)
    
while Que!=[]:
    l=len(Que)
    if head==l:
        head=0
        
    else:
        if Que[head][1]<=q:
            time+=Que[head][1]
            Que[head][2]+=time
            tmp=Que.pop(head)
            answer.append(tmp)
            
        else:
            time+=q
            Que[head][1]-=q
            head+=1
            
for i in range(n):
    print(answer[i][0],answer[i][2])

Submission status: Accepted

Solution code:
n,q = input().split()
n = int(n)
q = int(q)

queue = []

class process:
    def __init__(self,a,b):
        self.name = a
        self.time = b


for i in range(n):
    a,b = input().split()
    b = int(b)
    queue.append(process(a,b))

def enqueue(proc):
    global queue
    queue.append(proc)

def dequeue():
    global queue
    return queue.pop(0)

def isEnpty():
    global queue
    return len(queue) != 0


def RoundRobinScheduling():
    now_time = 0
    while  isEnpty(): 
        now_process = dequeue()
        if now_process.time - q > 0:
            now_process.time = now_process.time - q
            enqueue(now_process)
            now_time = now_time + q
        else:
            now_time = now_time + now_process.time
            print(now_process.name,end=" ")
            print(now_time)


RoundRobinScheduling()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

n,g = map(int, stdin.readline().split())
processes = deque([(name,int(time)) for name,time in [stdin.readline().split() for _ in range(n)]])
total_time = 0
while len(processes) > 0:
    name,left_time = processes.popleft()
    if left_time > g:
        total_time += g
        left_time -= g
        processes.append((name,left_time))
    else:
        total_time += left_time
        print(name, total_time)

Submission status: Accepted

Solution code:
n, q = map(int,input().split())
queue = []
for i in range(n):
    proccess = list(input().split())
    queue.append([proccess[0],int(proccess[1])])
cnt = 0
time = 0

while cnt < n:
    if queue[0][1] <= q:
        time += queue[0][1]
        print(queue[0][0] + " " +str(time))
        cnt += 1
        del queue[0]
    elif queue[0][1] > q:
        time += q
        queue[0][1] -= q
        queue.append(queue.pop(0))

Submission status: Accepted

Solution code:
n,q = list(map(int,input().split()))

from collections import deque
que = deque([])

for i in range(n):
    que.append((int(x) if x.isdigit() else x for x in input().split()))

totaltime = 0

while que:
    name,lefttime = que.popleft()
    if lefttime <= q:
        totaltime += lefttime
        print(name,totaltime)
    else:
        lefttime -= q
        totaltime += q
        
        que.append((name,lefttime))

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
p = deque()

for _ in range(n):
    p_i = input().split()
    p.append([p_i[0], int(p_i[1])])

time = 0


while p:
    if p[0][1] > q:
        p[0][1] -= q
        time += q
        p.append(p.popleft())
    else:
        time += p[0][1]
        print(f"{p[0][0]} {time}")
        p.popleft()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
que = deque([])

for i in range(n):
    name, time = input().split()
    time = int(time)
    que.append([name, time])

t = 0

while len(que) > 0:
    atop = que.popleft()
    spend = min(atop[1], q)
    atop[1] -= spend
    t += spend
    if atop[1] == 0:
        print(atop[0], t)
    else:
        que.append(atop)

Submission status: Accepted

Solution code:
from collections import deque

dq = deque()
n, q = map(int, input().split())
for i in range(n):
  p, t = map(str, input().split())
  dq.append([p, int(t)])

t = 0
while dq:
  if dq[0][1] <= q:
    t += dq[0][1]
    print(str(dq[0][0]) + " " + str(t))
    dq.popleft()
  else:
    t += q
    dq[0][1] -= q
    dq.append(dq[0])
    dq.popleft()

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
deq = []
total = 0
for _ in range(n):
    name, time = input().split()
    deq.append([name, int(time)])

Deq = deque(deq)
ans = []
time = 0
while Deq:
    process = Deq.popleft()
    tmp = process[1]
    remain = max(tmp-q, 0)
    if remain == 0:
        time += tmp
        ans.append([process[0], time])
    else:
        time += q
        Deq.append([process[0],remain])

for name, time in ans:
    print(name, time)

Submission status: Accepted

Solution code:
N,Q=map(int, input().split())
A=[input().split() for _ in range(N)]
time=0
while True:
    a=A.pop(0)
    n=a[0]
    v=int(a[1])-Q
    if v <= 0:
        time+=Q+v
        print(f'{n} {time}')
    else:
        A.append([n, v])
        time+=Q
    if len(A) <= 0: break

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
que = deque()
for _ in range(n):
    x, y = input().split()
    que.append((x, int(y)))
ans = []
time = 0
while len(que) > 0:
    x, y = que[0]
    que.popleft()
    if y > q:
        que.append((x, y - q))
        time += q
    else:
        time += y
        ans.append((x, time))
ans.sort(key = lambda x: x[1])
for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
n,q=list(map(int,input().split()))
p=[]
for i in range(n):
    name_,time_=input().split()
    p.append([str(name_),int(time_)])

done_=[]
time_passed=0
while len(p)>0:
    box_=[]
    del_index=[]
    for i,process_ in enumerate(p):
        amount_=process_[1]-q
        if amount_>0:
            box_.append([process_[0],amount_])
            time_passed+=q
        else:
            time_passed+=min(q,process_[1])
            done_.append([process_[0],time_passed])   
            del_index.append(i)
    cnt=0
    for d in del_index:
        del p[d-cnt]
        cnt+=1
    p=box_
    
for k in range(n):
    print(*done_[k])

Submission status: Accepted

Solution code:
import queue

N, Q = map(int, input().split())

P = queue.Queue()
for _ in range(N):
    p = input().split()
    P.put([p[0], int(p[1])])

time = 0
while not P.empty():
    p = P.get()
    if p[1] <= Q:
        time += p[1]
        print(p[0], time)
    else:
        time += Q
        P.put([p[0], p[1] - Q])

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,input().split())
que = deque([])
ans = []
t = 0
for i in range(n):
    name,time = input().split()
    que.append([name,int(time)])
while que:
    p = que.popleft()
    if p[1] > q:
        t += q
        p[1] -= q
        que.append(p)
    else:
        t += p[1]
        ans.append([p[0],t])

for namae,num in ans:
    print(namae,num)

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,input().split())
time = 0
tl=deque()
d=dict()
for i in range(n):
    key,value = input().split()
    d[key] = int(value)

for i in d.items():
    tl.append(i)
#print(tl)

while len(tl)!=0:
    tmp = tl.popleft()
    if list(tmp)[1] <= q:
        time+=list(tmp)[1]
        #print(m[1])
        #print(time)
        #list(m)[1]=time
        print(f"{list(tmp)[0]} {time}")
    else:
        time+=q
        l=list(tmp)#tupleの中身書き換える時はlist型にしなきゃいけないからめんどい
        l[1]-=q
        nt=tuple(l)
        tl.append(nt)
        

"""

def main():
    n,q = map(int,input().split())
    que = []
    for _ in range(n):
        name,time = input().split()
        que += [[name, int(time)]]
    anst = 0
    while que ! = []:
        temp = que.pop(0)
        if temp[1] <= q:
            anst += temp[1]
            print(f"{temp[0]} {anst}")
        else:
            anst += q
            que += [[temp[0],temp[1]-q]]
main()
"""

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
query = []

for i in range(n):
	n, t = map(str, input().split())
	query.append([n, int(t)])

d = deque(query)

ans = []

time = 0
while len(d) > 0:
	tmp = d.popleft()
	if tmp[1] > q:
		res = [tmp[0], tmp[1] - q]
		time += q
		d.append(res)
	else:
		time += tmp[1]
		ans.append([tmp[0], time])

for v in ans:
	print(v[0], v[1])

Submission status: Accepted

Solution code:
class Queue():

    def __init__(self, max_size):
        self.head = 0
        self.tail = 0
        self.max = max_size + 1
        self.q = [None for _ in range(self.max)]

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.max

    def enqueue(self, x):
        if self.is_full():
            print("Error: Overflow")
        self.q[self.tail] = x
        if self.tail + 1 == self.max:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            print("Error: Underflow")

        x = self.q[self.head]
        if self.head + 1 == self.max:
            self.head = 0
        else:
            self.head += 1

        return x


n, q = [int(x) for x in input().split()]
names = Queue(n)
time = Queue(n)

for _ in range(n):
    data = input().split()
    names.enqueue(data[0])
    time.enqueue(int(data[1]))

t = 0
while not names.is_empty():
    process = names.dequeue()
    process_time = time.dequeue()

    if process_time > q:
        names.enqueue(process)
        time.enqueue(process_time - q)
        t += q
    else:
        t += process_time
        print(process, t)

Submission status: Accepted

Solution code:
class Process():
    name = ""
    time = 0

    def __init__(self, name, time):
        self.name = name
        self.time = time


n, q = map(int, input().split(" "))

queue = []
for i in range(n):
    n, t = input().split(" ")
    queue.append(Process(n, int(t)))

qw = q
lap_time = 0
while len(queue) > 0:
    p = queue.pop(0)
    if qw != q and p.time - qw - q > 0:
        lap_time += qw + q
        p.time = p.time - qw - q
        qw = q
        queue.append(p)
    elif p.time - qw > 0:
        lap_time += qw
        p.time = p.time - qw
        qw = q
        queue.append(p)
    else:
        lap_time += p.time
        qw = q
        print(p.name, lap_time)

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split(' '))
d = deque()
for _ in range(n):
    l = list(input().split(' '))
    l[1] = int(l[1])
    d.appendleft(l)

_q = q
s = 0
while len(d) > 0:
    e = d.pop()
    if e[1] - _q > 0:
        s += _q
        e[1] -= _q
        d.appendleft(e)
    else:
        s += e[1]
        print(e[0], s)
    _q = q

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n, q = map(int, input().split())
l = [input().split() for _ in range(n)]
r = []
t = 0
while len(l) > 0:
    p = l.pop(0)
    pt = int(p[1])
    if pt >= q:
        t += q
        pt -= q
        if pt > 0:
            l.append([p[0], pt])
        else:
            r.append([p[0], t])
    else:
        t += pt
        r.append([p[0], t])
for x in r:
    print(' '.join(map(str, x)))

Submission status: Accepted

Solution code:
from collections import deque

n,qtime = list(map(int,input().split()))

Q = deque()

class Info:
    def __init__(self,arg_name,arg_rest_time):
        self.name = arg_name
        self.rest_time = arg_rest_time

for _ in range(n):
    tmp = input().split()
    Q.append(Info(tmp[0],int(tmp[1])))

current_time = 0

while len(Q)>0:
    info = Q.popleft()
    if info.rest_time > qtime:
        info.rest_time = info.rest_time - qtime
        current_time += qtime
        Q.append(info)
    else:
        current_time += info.rest_time
        print("%s %d"%(info.name,current_time))

Submission status: Accepted

Solution code:
import sys
from collections import deque

n,q = map(int,sys.stdin.readline().split())

dq = deque()

for x in range(n):
    x = sys.stdin.readline().split()
    dq.append([x[0],int(x[1])])
t = 0
while True:
    pr = dq.popleft()
    pr[1] -= q

    if pr[1] > 0:
        dq.append(pr)
        t += q
    else:
        t += pr[1] + q
        print(pr[0], t)

    if len(dq) == 0:
        break

Submission status: Accepted

Solution code:
from collections import deque

time = 0
finished = 0
n,q = map(int,input().split())


def rotate(A):
    global time,finished
    b = A.popleft()
    if b[1] - q > 0:
        b[1] -= q
        time += q
        A.append(b)
    else:
        time += b[1]
        finished += 1
        print(b[0],time)


def main():    
    order = deque([])
    for _ in range(n):
        tmp = list(input().split())
        tmp[1] = int(tmp[1])
        order.append(tmp)

    ##print("-----")
    while finished < n:
        rotate(order)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

N,Q = map(int,input().split())
deque_name = deque()
deque_time = deque()

for _ in range(N):
    a = list(input().split())
    deque_name.append(a[0])
    deque_time.append(int(a[1]))

now = 0
num=0
finished={}

while deque_name:

    name = deque_name.popleft()
    time = deque_time.popleft()

    if time<=Q:
        now+=time
        num+=1
        lst = [name,now]
        finished[num] = lst

    else:
        now+=Q
        time-=Q
        deque_name.append(name)
        deque_time.append(time)
    
for i in range(1,num+1):
    value = finished[i]
    print(' '.join(map(str,value)))

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque  # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():
    n, q = map(int, input().split())
    queue = deque([])  # 処理すべきプロセスをqueueで管理
    for i in range(n):
        name, time = input().split()
        time = int(time)
        queue.append([name, time])

    totaltime = 0  # 経過時間
    while queue:
        [name, time] = queue.popleft()
        if time <= q:  # 今回で処理完了
            totaltime += time
            print(name, totaltime)
        else:
            time -= q
            queue.append([name, time])  # 処理しきれなかった時間分を後回し
            totaltime += q


resolve()

Submission status: Accepted

Solution code:
from collections import deque

N,Q=map(int,input().split())
name_time=[[0]*2 for _ in range(N)]
for i in range(N):
    tmp=input().split()
    name_time[i][0]=tmp[0]
    name_time[i][1]=tmp[1]

q=deque()
q.extend(name_time)
now=0
while q:
    tmp=q.popleft()
    name=tmp[0]
    time=int(tmp[1])
    if time <= Q:
        now += time
        print(name,now)
    else:
        now += Q
        time -= Q
        q.append([name,time])

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int,input().split())

A = deque()
for i in range(n):
    name, time = input().split()
    A.append([name, int(time)])

t = 0
seq = []
while len(A)>0:
    name, time = A.popleft()
    if time > q:
        time -= q
        t += q
        A.append([name, time])
    else:
        t += time
        seq.append([name, t])

for name, time in seq:
    print(name, time)

Submission status: Accepted

Solution code:
from collections import deque

n,q = map(int, input().split())
que = deque([])

for i in range(n):
    name, time = input().split()
    time = int(time)
    que.append([name, time])

t = 0

while len(que) > 0:
    atop = que.popleft()
    spend = min(atop[1],q)
    t += spend
    atop[1] -= spend
    if atop[1] == 0:
        print(atop[0],t)
    else:
        que.append(atop)

Submission status: Accepted

Solution code:
# ALDS1_3_B: Queue

#n, q = [5, 100]
#A = [('p1', 150), ('p2', 80), ('p3', 200), ('p4', 350), ('p5', 20)]

A = []
n, q = map(int, input().split())
for i in range(n):
    name, time = input().split()
    A.append((name, int(time)))

totaltime = 0
while len(A) > 0:
    #process, A = A[0], A[1:]
    name, time = A[0]
    A.remove(A[0])
    if (time - q) <= 0:
        totaltime += time
        print(name, totaltime)
    else:
        totaltime += q
        A.append((name, time - q))

Submission status: Accepted

Solution code:
N ,q = map(int,input().split())
A = [input() for i in range(N)]

B = []
for raw in A:
    name,time = raw.split()
    B.append({
    "name":name,
    "time":int(time)
    })

total_time = 0
now = 0
while len(B)> 0:
    if B[now]["time"] > q:
        B[now]["time"] = B[now]["time"] - q
        total_time += q
        if now == len(B)-1:
            now = 0
        else:
            now += 1
    else:
        total_time += B[now]["time"]
        print("{} {}".format(B[now]["name"],total_time))
        del B[now]
        if now == len(B):
            now = 0

Submission status: Accepted

Solution code:
import queue
n, q = map(int, input().split())
processes = queue.Queue()
logs = []
time_count = 0

for i in range(n):
  name, time = input().split()
  time = int(time)
  processes.put([name, time])

while processes.empty() == False:
  process = processes.get()
  if process[1] > q:
    time_count += q
    process[1] -= q
    processes.put(process)
  else:
    time_count += process[1]
    logs.append([process[0], time_count])

for i in range(n):
  print(" ".join(map(str, logs[i])))

Submission status: Accepted

Solution code:
queue = []
sum_prc_time = 0

n, max_time = map(int, input().split())

def main_prc(prc_dat):
  global queue
  global sum_prc_time

  prc_time = int(prc_dat[-1])
  diff = prc_time - max_time

  if diff <= 0:
    sum_prc_time += prc_time
    print(f"{prc_dat[0]} {sum_prc_time}")
  else:
    sum_prc_time += max_time
    queue.append([prc_dat[0], diff])

for i in range(n):
  main_prc(input().split())

while 0 < len(queue):
  main_prc(queue.pop(0))

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
Q = deque()

ans = []
total = 0
for _ in range(n):
    p, t = input().split()
    Q.append([p, int(t)])

while len(Q) > 0:
    p, t = Q.popleft()
    if t<=q:
        total += t
        ans.append([p, total])
    else:
        total += q
        Q.append([p, t-q])

for v in ans:
    print(v[0], v[1])

Submission status: Accepted

Solution code:
class P:
    def __init__(self,name,time):
        self.name = name
        self.time = time
n,q = map(int,input().split())
LEN = 100004 #要素数nより少し大きければよい
head = 0
tail = n
Q = [P("",0) for _ in range(LEN)]
for i in range(n):
    Q[i].name,ti = input().split()
    Q[i].time = int(ti)
def enqueue(Q,x,tail):#末尾にクラスPの要素xを加える。ぐるっと回った場合
    Q[tail] = x       #以前dequeueで取り出したものが書き換えられる。
    if tail+1 == LEN:
        tail = 0
    else:
        tail += 1
    return tail
def dequeue(Q,head):
    x = Q[head]
    if head+1 == LEN:
        head = 0
    else:
        head += 1
    return x,head
totime = 0
while head != tail:
    x,head = dequeue(Q,head)
    if x.time <= q:
        totime += x.time
        print(x.name + " " + str(totime))
    else:
        totime += q
        x.time -= q
        tail = enqueue(Q,x,tail)

Submission status: Accepted

Solution code:
from collections import deque

i,qtime = map(int,input().split())
ttime = 0
deque = deque()
dic= {}
for s in range(i):
    name,value = map(str,input().split())
    dic[name] = int(value)
    deque.append(name)
while len(deque) > 0:
    time = qtime
    while time > 0:
        if dic[deque[0]] > time:
            dic[deque[0]] = dic[deque[0]] - time
            deque.append(deque.popleft())
            ttime += time
            time = 0
        else:
            ttime += dic[deque[0]]
            time = 0
            dic[deque[0]] = 0
            print("{0} {1}".format(deque[0],ttime))
            deque.popleft()

Submission status: Accepted

Solution code:
from collections import deque
n,q=map(int,input().split())
que=deque([])
for _ in range(n):
    que.append((int(x) if x.isdigit() else x for x in input().split()))
log=0
while que:
    name,lefttime=que.popleft()
    if lefttime <= q:
        log+=lefttime
        print(name,log)
    else:
        lefttime-=q
        log+=q
        que.append((name,lefttime))

Submission status: Accepted

Solution code:
queue = []
sum_prc_time = 0

n, max_time = map(int, input().split())

def main_prc(prc_dat):
  global queue
  global sum_prc_time

  prc_time = int(prc_dat[-1])
  diff = prc_time - max_time

  if diff <= 0:
    sum_prc_time += prc_time
    print(f"{prc_dat[0]} {sum_prc_time}")
  else:
    sum_prc_time += max_time
    queue.append([prc_dat[0], diff])

for i in range(n):
  main_prc(input().split())

while 0 < len(queue):
  main_prc(queue.pop(0))