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:
from collections import deque

def receive():
    v = input()
    return v.split(' ')

input_val = receive()
n = int(input_val[0])
q = int(input_val[1])
tasks = deque([])
time_total = 0

for _ in range(n):
    input_val = receive()
    name = input_val[0]
    time = int(input_val[1])
    tasks.append({"name": name, "time": time})

while len(tasks) > 0:
    current_task = tasks.popleft()
    if current_task["time"] <= q:
        time_total += current_task["time"]
        print(current_task["name"] + ' ' + str(time_total))
    else:
        current_task["time"] -= q
        time_total += q
        tasks.append(current_task)

Submission status: Accepted

Solution code:
n,q = list(map(int,input().split(" ")))
time = [tuple(input().split(" ")) for i in range(n)]


def process_queue(n,q,time):
    r = []
    c = 0
    while time:
        if int(time[0][1]) > q:
            time.append((time[0][0],int(time[0][1])-q,))
            time.pop(0)
            c+=q
        else:
            c+=int(time[0][1])
            r.append((time[0][0],c,))
            time.pop(0)
    return r
lst = [("p1","150",),("p2","80",),("p3","200"),("p4","350",),("p5","20")]
r = process_queue(n,q,time)
for i in r:
    print("{} {}".format(i[0],i[1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from queue import Queue

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

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

while que.qsize() > 0:
    name, time = que.get()
    total += min(q, time)
    if q < time:
        que.put((name, time-q))
    else:
        print(name, total)

Submission status: Accepted

Solution code:
from collections import deque
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
class Process:
    def __init__(self,arg_name,arg_value):
        self.name = arg_name
        self.value = arg_value
        self.finish_time = BIG_NUM
 
Q = deque()
 
N,LIMIT = map(int,input().split())
 
#プロセスの取得
for loop in range(N):
    input_str = list(map(str,input().split()))
    tmp_Process = Process(input_str[0],int(input_str[1]))
    Q.append((tmp_Process))   #末尾に追加
 
num_finish = 0
current_time = 0
 
while  True:
    tmp_Process = Q.popleft() #先頭から取り出し、FIFOを実現
 
    if tmp_Process.value <= LIMIT: #終了
        current_time += tmp_Process.value
        print("%s %d"%(tmp_Process.name,current_time))
        num_finish += 1
    else:
        tmp_Process.value -= LIMIT
        current_time += LIMIT
        Q.append((tmp_Process))
 
    if num_finish == N:
        break

Submission status: Accepted

Solution code:
from collections import deque

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

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

for _ in range(n):
    name, time = input().split()
    processes.append(Process(name, int(time)))

completion_time = 0
while len(processes) != 0:
    process = processes.popleft()
    tmp_time = process.time - q
    if tmp_time <= 0:
        completion_time += tmp_time + q
        print(process.name, completion_time)
    else:
        completion_time += q
        process.time = tmp_time
        processes.append(process)

Submission status: Accepted

Solution code:
n,q=[int(i) for i in input().split()]
nt=[[c for c in input().split()] for i in range(n)]
for i in range(n):
    nt[i][1]=int(nt[i][1])
t=0
while len(nt)>0:
    p=nt.pop(0)
    if p[1]<=q:
        t+=p[1]
        print(p[0],t)
    else:
        t+=q
        p[1]-=q
        nt.append(p)

Submission status: Accepted

Solution code:
processQueueList = []
completeList = []
time = 0
amount , quantum=map(int,input().split())
for process in range(amount):
    name ,needtime = input().split()
    processQueueList.append([name,needtime])
while len(processQueueList)!=0:
    process = processQueueList.pop(0)
    process[1] = int(process[1]) - quantum
    if process[1] <= 0:
        time = time + process[1] + quantum
        completeList.append([process[0],time])
    else:
        time = time + quantum
        processQueueList.append(process)
for process in completeList:
    print(*process)

Submission status: Accepted

Solution code:
def enque(x):
  global tail
  global n
  global queue_list
  queue_list[tail]=x
  if tail==n:
    tail=0
  else:
    tail+=1

def deque():
  global head
  global n
  global queue_list
  _=queue_list[head]
  if head==n:
    head=0
  else:
    head+=1
  return _

n,q=map(int,input().split())
queue_list=[['',0] for i in range(n+1)]
sum_time=0
head=0
tail=n
for i in range(n):
  queue_list[i][0],_time=input().split()
  queue_list[i][1]=int(_time)
while head!=tail:
  _deque=deque()
  if _deque[1]-q >0:
    sum_time+=q
    _deque[1]-=q
    enque(_deque)
  else:
    sum_time+=_deque[1]
    print(_deque[0],sum_time)

Submission status: Accepted

Solution code:
from collections import deque

n, quantum = map(int, input().split())
que = deque()
elapsed_time = 0

for _ in range(n):
    name, time = input().split()
    process = {'name': name, 'time': int(time)}
    que.append(process)
    
while que:
    process = que.popleft()
    name = process['name']
    time = process['time']
    result_time = time - quantum
    if result_time > 0:
        elapsed_time += quantum
        process = {'name': name, 'time': result_time}
        que.append(process)
    else:
        elapsed_time += time
        print(f"{process['name']} {elapsed_time}")

Submission status: Accepted

Solution code:
def schedule(A, q):
    count = 0
    while A != []:
        if A[0][1] <= q :
            count += A[0][1]
            w = A.pop(0)
            print(w[0], str(count))
        else :
            count += q
            A[0][1] -= q
            out = A.pop(0)
            A.append(out)

if __name__ == '__main__':
    n, q = input().split()
    n = int(n)
    q = int(q)
    que = [0]*n
    for i in range(n):
        name, time = input().split()
        que[i] = [name, int(time)]
    schedule(que, q)

Submission status: Accepted

Solution code:
class Queue():
    def __init__(self, n):
        self.Max = n + 1
        self.Q = [0 for i in range(self.Max)]
        self.head = 0
        self.tail = 0

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

    def isFull(self):
        return self.head == (self.tail + 1) % self.Max

    def enqueue(self, x):
        self.Q[self.tail] = x
        if self.tail+1 == self.Max:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.head+1 == self.Max:
            self.head = 0
            return self.Q[self.head-1]
        else:
            self.head += 1
            return self.Q[self.head-1]


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

for i in range(n):
    name_time = list(input().split())
    name_time[1] = int(name_time[1])
    que.enqueue(name_time)

while True:
    name_time = que.dequeue()
    if name_time[1]-q > 0:
        time += q
        name_time[1] -= q
        que.enqueue(name_time)
    elif name_time[1]-q == 0:
        time += q
        name_time[1] = 0
        print(name_time[0], time)
    else:
        time += name_time[1]
        name_time[1] = 0
        print(name_time[0], time)
    if que.isEmpty():
        break

Submission status: Accepted

Solution code:
from queue import Queue


if __name__ == "__main__":
    q: Queue = Queue()
    process_num, quantum = map(lambda x: int(x), input().split(" "))
    total_time = 0

    for _ in range(process_num):
        process, time_str = input().split(" ")
        time = int(time_str)
        q.put([process, time])

    while not q.empty():
        process_name, process_time = q.get()
        if process_time <= quantum:
            total_time += process_time
            print(f"{process_name} {total_time}")
        else:
            total_time += quantum
            process_time -= quantum
            q.put([process_name, process_time])

Submission status: Accepted

Solution code:
L=input().split( )
n=int(L[0])
q=int(L[1])
Que=[0 for i in range(n)]

for i in range(n):
    Que[i]=input().split( )
    Que[i][1]=int(Que[i][1])
    Que[i].append(0)

head=0
Fin=[]
time=0
while Que!=[]:
    l=len(Que)
    if head>l-1:
        head=0
    else:
        if Que[head][1]>q:
            Que[head][1]-=q
            head+=1
            time+=q
        else:
            time+=Que[head][1]
            Que[head][2]+=time
            temp=Que.pop(head)
            Fin.append(temp)
for i in range(n):
    print(Fin[i][0],Fin[i][2])

Submission status: Accepted

Solution code:
class Queue:
    res=0
    def __init__(self):
        self.li = []
        self.top = 0
    def push(self,value):
        self.li.insert(0,value)
        #self.li.append(int(value))
        self.top += 1
    def pop(self):
        self.top -= 1
        return self.li.pop()
    def isEmpty(self):
        return len(self.li)==0
    def __str__(self):
        return str(self.li)

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

for i in range(n):
    name,time = map(str,input().split())
    save.push([name,int(time)])

while(not save.isEmpty()):
    # print(save)
    tmp = save.pop()
    if tmp[1] <= q:
        Queue.res += tmp[1]
        print(tmp[0],Queue.res)
    else:
        Queue.res += q
        save.push([tmp[0],tmp[1]-q])

Submission status: Accepted

Solution code:
class Queue:
    _items = []
    _capacity = 0
    _head = 0
    _tail = 0

    def __init__(self, cap):
        self._capacity = cap + 1
        for i in range(self._capacity):
            self._items.append(None)

    def enqueue(self, val):
        index = (self._tail + 1)%self._capacity
        if index == self._head:
            raise Exception('Queue is full')
        self._items[self._tail] = val
        self._tail = index

    def dequeue(self):
        if self._head == self._tail:
            raise Exception('Queue is empty')
        val = self._items[self._head]
        self._items[self._head] = None
        self._head = (self._head + 1)%self._capacity
        return val

    def is_empty(self):
        return  self._head == self._tail
        


class Task:
    name = ''
    time = 0

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

    def __str__(self):
        return '%s %d'%(self.name, self.time)

if __name__ == '__main__':
    N, p = map(int, input().split())

    tasks = Queue(N)
    finished_tasks = []
    total_time = 0  

    for i in range(N):
        n, t = input().split()
        tasks.enqueue(Task(n, int(t)))

    while not tasks.is_empty():
        cur = tasks.dequeue()
        if cur.time <= p:
            total_time += cur.time
            cur.time = total_time
            finished_tasks.append(cur)
        else:
            total_time += p
            cur.time -=p
            tasks.enqueue(cur)

    for i in finished_tasks:
        print(i)

Submission status: Accepted

Solution code:
class Queue:
    def __init__(self, data=[]):
        self.data = data

    def enqueue(self, x):
        self.data.append(x)

    def dequeue(self):
        return self.data.pop(0)

    def is_Empty(self):
        if len(self.data) == 0:
            return True
        else:
            return False


n, q = map(int, input().split())  # プロセスの数n, 各界の処理能力q
queue = Queue()
for _ in range(n):
    name, t = map(str, input().split())
    queue.enqueue([name, int(t)])

done_process = {}

now_time = 0
while not queue.is_Empty():
    name, t = queue.dequeue()
    if t > q:
        t -= q
        now_time += q
        queue.enqueue([name, t])
    else:
        now_time += t
        done_process[name] = now_time


for k, v in done_process.items():
    print(k, v)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    n, q = map(int, input().split(' '))
    inputs = [input().split(' ') for _ in range(n)]
    inputs = [(p[0], int(p[1])) for p in inputs]
    queue = deque(inputs)
    t = 0
    while len(queue) > 0:
        p = queue.popleft()
        if p[1] > q:
            t += q
            queue.append((p[0], p[1]-q))
        else:
            t += p[1]
            print('{} {}'.format(p[0], t))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
N, Q = [int(_) for _ in stdin.readline().rstrip().split()]
d = deque(stdin.readline().rstrip().split() for _ in range(N))
finished_time = 0
while len(d) > 0:
    p = d.popleft()
    time = int(p[1])
    remain = time - Q
    if remain > 0:
        d.append([p[0], remain])
        finished_time += Q
    else:
        finished_time += time
        print(p[0], finished_time)

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd
from collections import deque

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n,q = map(int, sys.stdin.readline().split())
    que=deque([])

    for i in range(n):
        name, time = sys.stdin.readline().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:

class Queue:
    def __init__(self, n):
        self.A = [0 for i in range(n)]
        self.head = 0
        self.tail = 0
        self.len = n

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

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

    def enqueue(self, x):
        if not self.is_full():
            self.A[self.tail] = x
            self.tail += 1
            if self.tail == self.len:
                self.tail = 0

    def dequeue(self):
        if not self.is_empty():
            x = self.A[self.head]
            self.head += 1
            if self.head == self.len:
                self.head = 0
            return x


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


if __name__ == '__main__':
    Q = Queue(100005)
    n, q = map(int, raw_input().split())
    for i in range(n):
        l = raw_input().split()
        t = Task(l[0], int(l[1]))
        Q.enqueue(t)

    cnt = 0
    while not Q.is_empty():
        t = Q.dequeue()
        if t.time > q:
            cnt += q
            t.time -= q
            Q.enqueue(t)
        else:
            cnt += t.time
            print t.name, cnt

    pass

Submission status: Accepted

Solution code:
from collections import deque

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

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

while len(que)!=0:
    now=que.popleft()
    nt=now[1]-q
    if nt<=0:
        tmcnt+=now[1]
        print(now[0],tmcnt)
    else:
        now[1]-=q
        tmcnt+=q
        que.append(now)

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
nt=[]
for i in range(n):
    nt.append(input().split())
Queue=[]
time=0
def Enqueue(x):
    return Queue.append(x)

def Dequeue():
    temp=Queue[0]
    del Queue[0]
    return temp
    
for j in range(n):
    Enqueue(nt[j])

while len(Queue)!=0:
    if int(Queue[0][1])<=q:
        time+=int(Queue[0][1])
        print(Queue[0][0],time)
        Dequeue()
    else:
        time+=q
        Temp=Dequeue()
        Temp[1]=int(Temp[1])-int(q)
        Enqueue(Temp)

Submission status: Accepted

Solution code:
# stack.py
class Cell:
    def __init__(self, value, name):
        self.name = name
        self.value = value
        self.next = None


class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def Enqueue(self, d,name):
        new = Cell(d,name)
        if self.front is None:
            self.front = new
            self.rear = self.front
        else:
            self.rear.next = new
            self.rear = new

    def Dequeue(self):
        if self.front is None:
            print('Queue Under Flow!')
            return
        else:
            res = self.front
            nex = self.front.next
            del self.front
            self.front = nex
            return res

    def is_empty(self):
        return self.front==None


n,q = map(int,input().split())
que = Queue()
for i in range(n):
    name,val = input().split()
    que.Enqueue(int(val),name)

t = 0
while not que.is_empty():
    a = que.Dequeue()
    if a.value>q:
        que.Enqueue(a.value-q,a.name)
        t+=q
    else:
        t+=a.value
        print(a.name,t)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
tasks = []
sumtime = 0

for _ in range(n):
    task = input().split()
    time = int(task[1])
    if time <= q:
        sumtime += time
        print(task[0], sumtime)
    else:
        sumtime += q
        time = time - q
        tasks.append([task[0], time])

while tasks:
    command = []
    for j, task in enumerate(tasks[:]):
        time = task[1]
        if time <= q:
            sumtime += time
            print(task[0], sumtime)
            del tasks[tasks.index(task)]
        else:
            sumtime += q
            time = time - q
            tasks[(tasks.index(task))] = [task[0], time]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# ALDS1_3_B_queue

from queue import Queue

def processing(p, d, now, q):
    name, time = p.get()
    
    if time <= q:
        now += time
        d.append([name, now])
    else:
        now += q
        time -= q
        p.put([name, time])
    
    return p, d, now

p = Queue()
now = 0
d = []

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

for _ in range(n):
    name, time = input().split()
    time = int(time)
    p.put([name,time])
    
while not p.empty():
    p, d, now = processing(p, d, now, q)
        
for name, time in d:
    print(name, time)

Submission status: Accepted

Solution code:
"""
queue

dequeを使おう
stack q.pop()
queue q.popleft()
"""
from collections import deque
queue = deque([])
n,q = map(int, input().split())

for i in range(n):
    name,time = input().split()
    queue.append((name,int(time)))
ans = 0
while queue:
    name,time = queue.popleft()
    if time-q > 0:
        queue.append((name,time-q))
        ans += q   
    else:
        ans += time
        print(name,ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	n,q = map(int,input().split())
	process = []
	time = []
	for i in range(n):
		p,t = map(str,input().split())
		process.append(p)
		time.append(int(t))
	total = 0
	while len(process) != 0:
		run = process.pop(0)
		t = time.pop(0)
		diff = t - q
		if diff <= 0:
			print("%s %d" % (run,total+t))
			total+=t
		else:
			total += q
			process.append(run)
			time.append(diff)

Submission status: Accepted

Solution code:
from collections import deque

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

gTime = 0

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

t = 0

while len(que) > 0:
    top = que.popleft()
    x = min(top[1], p)
    top[1] -= x
    t += x

    if top[1] == 0:
        print(top[0], t)
    else:
        que.append(top)

Submission status: Accepted

Solution code:

from collections import deque
a=list(map(int,input().split()))
n,q=a[0],a[1]
data=deque()
for i in range(n):
    process=input().split()
    process[1]=int(process[1])
    data.append(process)
time=0

while len(data)!=0:
    task=data.popleft()
    if task[1]>q:
        task[1]-=q
        data.append(task)
        time+=q
    else:
        time+=task[1]
        print(task[0],time)

Submission status: Accepted

Solution code:
from collections import deque


queue = deque()

num, q = map(int, input().split())
for _ in range(num):
    job, time = input().split()
    queue.append([job, int(time)])

res = []
timer = 0
while queue:
    curr_job, job_time = queue.popleft()
    if job_time <= q:
        timer += job_time
        res.append([curr_job, timer])
        continue
    else:
        job_time -= q
        timer += q
        queue.append([curr_job, int(job_time)])

for job in res:
    print(*job)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
a = []
for i in range(n):
  a.append(list(input().split()))
  a[i][1] = int(a[i][1])
time = 0
while a:
  if a[0][1] > q:
    time += q
    a[0][1] -= q
    a.append([a[0][0],a[0][1]])
    del a[0]
  else:
    time += a[0][1]
    print(a[0][0] + ' ' + str(time))
    del a[0]

Submission status: Accepted

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

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

current_time = 0
while True:
    if len(task_list) == 0:
        break
    process_name, remaining_time = task_list.pop(0)
    if remaining_time > q:
        task_list.append((process_name, remaining_time - q))
        current_time = current_time + q
    else:
        current_time = current_time + remaining_time
        print(process_name + ' ' + str(current_time))

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
names = []
times = []
now = 0
count = 1

for i in range(n):
    name, time = input().split()
    names.append(name)
    times.append(int(time))

idx = 0
while True:
#    print('\n# count: ' + str(count))
    if (len(names) == 0):
        break

    if q < times[idx]:
        times[idx] = times[idx] - q
        now += q
        idx += 1
    else:
        now += times[idx]
        print(names[idx] + ' ' + str(now))
        del names[idx]
        del times[idx]
#        continue
    if idx == len(names):
        idx = 0

    count += 1

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

n,q = map(int,input().split())
que = Queue()#FIFOキュー作成
for i in range(n):
    name, time = input().split()
    que.put((name,int(time)))

total = 0
while que.qsize()>0:
    name,time = que.get()
    total += min(q,time)
    if q < time:
        que.put((name,time-q))
    else:
        print(name,total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
     n,lim = map(int,input().split())
     que = []
     for i in range(n):
          ni,ti = input().split()
          que.append([ni,int(ti)])
          
     time = 0
     while que != []:
          now = que.pop(0)
          if now[1] <= lim:
               time += now[1]
               print(now[0],time)
          else:
               now[1] -= lim
               time += lim
               que.append(now)
solve()

Submission status: Accepted

Solution code:
import collections

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

a = collections.deque()

for i in range(n):
    a.append(input().split())
    #print(a)

time = 0

while len(a) > 0:
    p = int(a[0][1])
    #print(p)
    if p > q:
        p -= q
        a[0][1] = str(p)
        a.append(a.popleft())
        time += q
        #print(a,time)
    else:
        time += p
        print(a.popleft()[0],time)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
pro = []
for _ in range(n):
    x = input().split()
    pro.append([x[0],int(x[1])])
curti = 0

while len(pro)>0:
    curpro = pro.pop(0)
    if curpro[1]>q:
        curpro[1] -= q
        curti += q
        pro.append(curpro)
    else:
        curti += curpro[1]
        print(curpro[0],curti)

Submission status: Accepted

Solution code:
import collections
que = collections.deque()
n,q = map(int,input().split())
T = 0
for i in range(n):
    que.append(list(input().split()))
while len(que) > 0:
    A = que.popleft()
    if int(A[1]) > q:
        A[1] = str(int(A[1])-q)
        que.append(A)
        T += q
    else:
        T += int(A[1])
        print(A[0],T)

Submission status: Accepted

Solution code:
def main():
    n, q = map(int, input().split())
    queue = []
    for i in range(n):
        name, time = input().split()
        time = int(time)
        queue.append([name, time])

    t = 0
    while queue:
        process = queue.pop(0)
        if process[1] > q:
            t += q
            queue.append([process[0], process[1] - q])
        else:
            t += process[1]
            print("{} {}".format(process[0], t))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


# INPUT
n, q = map(int, input().split())

que = deque([])

for _ in range(n):
    name, time = input().split()

    que.append([name, int(time)])

# PROCESS
time_sum = 0

while len(que):

    name, time = que.popleft()

    if time <= q:
        time_sum += time

        print(name, time_sum)
    else:
        time_sum += q

        que.append([name, time - q])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
queue = []
for i in range(n):
  k, v = input().split()
  queue.append([k, int(v)])

time = 0

while len(queue) > 0:
  if queue[0][1] > q:
    time += q
    queue[0][1] -= q
    queue.append(queue.pop(0))
  else:
    time += queue[0][1]
    print(queue.pop(0)[0], time)

#print(" ".join(map(str, S)))

Submission status: Accepted

Solution code:
N, q = map(int, input().split())
job = {}
for i in range(N):
    name, t = input().split()
    job[name] = int(t)

from collections import deque
Q = deque(job.keys())


time = 0
while len(Q):
    cur = Q.popleft()
    if job[cur] > q:
        job[cur] -= q
        Q.append(cur)
        time += q
    else:
        time += job[cur]
        print("{} {}".format(cur, time))

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]
queue = []
for ni in range(n):
    name, time = input().split()
    queue.append([name, int(time)])
total_time = 0
while queue != []:
    queue_pop = queue[0]
    if queue_pop[1] <= q:
        print(queue_pop[0],total_time+queue_pop[1])
        total_time += queue_pop[1]
        del queue[0]
    else:
        total_time += q
        del queue[0]
        queue_pop[1] -= q
        queue.append(queue_pop)

Submission status: Accepted

Solution code:
info=list(map(int,input().split()))
p_name=[]
p_time=[]
time=0
for i in range(info[0]):
	input_pro=input().split()
	p_name.append(input_pro[0])
	p_time.append(int(input_pro[1]))

while len(p_time)!=0:
	nt=p_time.pop(0)
	nn=p_name.pop(0)
	if nt<=info[1]:
		time+=nt
		print(nn+' '+str(time))

	else:
		p_name.append(nn)
		p_time.append(nt-info[1])
		time+=info[1]

Submission status: Accepted

Solution code:
def enqueue(x):
  global tail
  que.append(x)
  tail = (tail + 1) % N

def dequeue():
  head_v = que.pop(0)
  return head_v


N, Q = map(int, input().split())
list = [input().split() for _ in range(N)]
que = []
elaps = 0

for i in range(N):
  que.append([list[i][0], int(list[i][1])])
tail = N

while que:
  target = dequeue()
  c = min(Q, target[1])
  target[1] -= c
  elaps += c
  if target[1] > 0:
    enqueue(target)
  else:
    print(target[0], elaps)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
order = []
dic = {}
time = 0
for _ in range(n):
    x, y = input().split()
    order.append(x)
    dic[x] = int(y)

while order:
    x = dic[order[0]] - q
    if  x > 0:
        dic[order[0]] = x
        order.append(order[0])
        del order[0]
        time += q
    else:
        time += (x + q)
        print(order[0], time)
        del order[0]

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())


class Queue:
    def __init__(self, n):
        self.n = n
        self.q = [0] * (n + 1)
        self.head = 0
        self.tail = 0

    def enqueue(self, e):
        self.q[self.tail] = e
        self.tail = (self.tail + 1) % (self.n + 1)

    def dequeue(self):
        x = self.q[self.head]
        self.head = (self.head + 1) % (self.n + 1)
        return x

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


queue = Queue(N)
for _ in range(N):
    i = input().split()
    queue.enqueue([i[0], int(i[1])])
time = 0
while not queue.isEmpty():
    p = queue.dequeue()
    if p[1] <= Q:
        time += p[1]
        print(p[0], time)
    else:
        time += Q
        queue.enqueue([p[0], p[1] - Q])

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
nt = [list(input().split()) for _ in range(n)];
que = deque(nt)
time = 0

while len(que)>0:
  a = que.popleft()
  if int(a[1]) > q:
    que.append([a[0], int(a[1])-q])
    time += q
  else:
    time += int(a[1])
    print(a[0], time)