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 Queue:
    def __init__(self, n):
        self.values = [None]*n
        self.n = n
        self.s = 0
        self.t = 0

    def next(self, p):
        ret = p+1
        if ret >= self.n:
            ret = 0
        return ret

    def enqueue(self, x):
        if self.next(self.s) == self.t:
            raise Exception("Overflow")
        self.values[self.s] = x
        self.s = self.next(self.s)

    def dequeue(self):
        if self.s == self.t:
            raise Exception("Underflow")
        ret = self.values[self.t]
        self.t = self.next(self.t)
        return ret

n, q = map(int, raw_input().split(' '))
queue = Queue(n+1)
for _ in range(n):
    name, time = raw_input().split(' ')
    time = int(time)
    queue.enqueue((name, time))

completed = []
cur = 0
while len(completed) < n:
    name, time = queue.dequeue()
    res = time-q
    if res <= 0:
        cur += time
        completed.append((name, cur))
    else:
        cur += q
        queue.enqueue((name, res))

for name, time in completed:
    print name, time

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
#print(n,p)
round_robin=[]
for i in range(n):
    x=input().split()
    name=x[0]
    t=int(x[1])
    round_robin.append([name,t])
#print(round_robin)
finished=[]
counter=0
while round_robin:
    left_time=round_robin[0][1]
    if left_time<=q:
        counter+=left_time
        finished.append([round_robin[0][0],counter])
        del round_robin[0]
    else:
        counter+=q
        left_time-=q
        round_robin[0]=[round_robin[0][0],left_time]
        round_robin.append(round_robin.pop(0))

for i in finished:
    print(*i)

Submission status: Accepted

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

A = []
D = {}
for l in range(N):
    n, t = input().strip().split()
    A.append([n, int(t)])

T = 0
while A:
    l = A.pop(0)
    if l[1] > Q:
        l[1] -= Q
        T += Q
        A.append(l)
    else:
        T += l[1]
        print(l[0], T)

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, raw_input().split())
task = [raw_input().split() for _ in range(n)]

for t in task:
    t[1] = int(t[1])

Q = deque(task)
elaps = 0

while len(Q) > 0:
    u = Q.popleft()
    c = min(u[1], q)
    u[1] -= c
    elaps += c

    if u[1] > 0:
        Q.append(u)
    else:
        print u[0], elaps

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
t = 0
queue = deque([])
for _ in range(n):
    name, time = input().split()
    time = int(time)
    queue.append([name, time])
while len(queue):
    if queue[0][1] > q:
        queue[0][1] -= q
        t += q
        queue.rotate(-1)
    else:
        t += queue[0][1]
        queue[0][1] = t
        print(" ".join(list(map(str, queue.popleft()))))

Submission status: Accepted

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

count = 0
while len(works) != 0:
    work = works.pop(0)
    time = int(work[1])
    if time > q:
        count += q
        work[1] = time - q
        works.append(work)
    else:
        count += time
        print(work[0] , count)

Submission status: Accepted

Solution code:
from collections import deque

def doRoundRobin(nt,q):
    result = []

    countTime = 0

    while len(nt) > 0:
        tmp = nt.popleft()
        if tmp[1] <= q:
            countTime += tmp[1]
            result.append([tmp[0],countTime])
        else:
            tmp[1] -= q
            countTime += q
            nt.append(tmp)
    return result


if __name__ == '__main__':
    n, q = map(int,input().split())
    
    nameTime = deque()
    for _ in range(n):
        a, b =  input().split()
        nameTime.append([a,(int)(b)])

    resultNameTime = doRoundRobin(nameTime,q)

    for x in resultNameTime:
        print(x[0],x[1])

Submission status: Accepted

Solution code:
from collections import deque

n,q = tuple([int(i) for i in input().split()])
lines = [input().split() for i in range(n)]
pro = deque([[i[0], int(i[1])] for i in lines])
time = 0

while True:
    left = pro.popleft()

    if left[1] - q > 0:
        time += q
        pro.append([left[0], left[1] - q])
    else:
        time += left[1]
        print(' '.join([left[0], str(time)]))

    if len(pro) == 0:
        break;

Submission status: Accepted

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

processes = []

for i in range(int(n)):
    process_name, process_time = input().split()
    processes.append([process_name, int(process_time)])

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

Submission status: Accepted

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

processes = [[li[0], int(li[1])] for li in [input().split() for _ in range(n)]]


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

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
que = []
t = 0
for i in range(n):
    que.append(input().split())

while len(que) != 0:
    proc =  que.pop(0)
    if int(proc[1]) <= q:
        t += int(proc[1])
        print(proc[0],t)
    else:
        t += q
        proc[1] = int(proc[1]) - q
        que.append(proc)

Submission status: Accepted

Solution code:
from collections import deque

queue = deque([])

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

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

timestamp = 0
while len(queue) > 0:
    h = queue.popleft()
    if h[1] <= q:
        print(h[0], timestamp+h[1])
        timestamp += h[1]
    else:
        h[1] -= q
        queue.append(h)
        timestamp += q

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

n, q = map(int, input().split())
queue = deque([])
for i in range(n):
    a = input().split()
    queue.append([a[0], int(a[1])])

time = 0

while queue:
    b = queue.popleft()
    if b[1] <= q:
        time += b[1]
        print(b[0] + ' ' + str(time))
    else:
        b[1] -= q
        time += q
        queue.append(b)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def ALDS1_3B():
    n, q = map(int, input().split())
    PQ = [[l[0], int(l[1])] for l in  [input().split() for i in range(n)]  ]
    times = 0
    while PQ:
        p = PQ.pop(0)
        if p[1] <= q:
            times += p[1]
            print('%s %d' %(p[0], times))
        else:
            times += q
            PQ.append([p[0], p[1]-q])


if __name__ == '__main__':
    ALDS1_3B()

Submission status: Accepted

Solution code:
class Queue(object):
    def __init__(self, _max):
        if type(_max) != int:
            raise ValueError
        self._array = [None for i in range(0, _max)]
        self._head = 0
        self._tail = 0
        self._cnt = 0

    def enqueue(self, value):
        if self.is_full():
            raise IndexError
        self._array[self._head] = value
        self._cnt += 1
        if self._head + 1 >= len(self._array):
            self._head = 0
        else:
            self._head += 1

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        value = self._array[self._tail]
        self._cnt -= 1
        if self._tail + 1 >= len(self._array):
            self._tail = 0
        else:
            self._tail += 1
        return value

    def is_empty(self):
        return self._cnt <= 0

    def is_full(self):
        return self._cnt >= len(self._array)


def round_robin(quantum, jobs):
    queue = Queue(100000)
    total = 0
    for job in jobs:
        queue.enqueue(job)

    while not queue.is_empty():
        name, time = queue.dequeue()
        if time > quantum:
            total += quantum
            queue.enqueue((name, time - quantum))
        else:
            total += time
            print("{0} {1}".format(name, total))


if __name__ == "__main__":
    n, quantum = input().split()
    jobs = [input().split() for i in range(0, int(n))]
    jobs = [(job[0], int(job[1])) for job in jobs]
    round_robin(int(quantum), jobs)

Submission status: Accepted

Solution code:
class Queue(object):
    def __init__(self, _max):
        if type(_max) != int:
            raise ValueError
        self._array = [None for i in range(0, _max)]
        self._head = 0
        self._tail = 0
        self._cnt = 0

    def enqueue(self, value):
        if self.is_full():
            raise IndexError
        self._array[self._head] = value
        self._cnt += 1
        if self._head + 1 >= len(self._array):
            self._head = 0
        else:
            self._head += 1

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        value = self._array[self._tail]
        self._cnt -= 1
        if self._tail + 1 >= len(self._array):
            self._tail = 0
        else:
            self._tail += 1
        return value

    def is_empty(self):
        return self._cnt <= 0

    def is_full(self):
        return self._cnt >= len(self._array)


def round_robin(quantum, jobs):
    queue = Queue(len(jobs))
    total = 0
    for job in jobs:
        queue.enqueue(job)

    while not queue.is_empty():
        name, time = queue.dequeue()
        if time > quantum:
            total += quantum
            queue.enqueue((name, time - quantum))
        else:
            total += time
            print("{0} {1}".format(name, total))


if __name__ == "__main__":
    n, quantum = input().split()
    jobs = [input().split() for i in range(0, int(n))]
    jobs = [(job[0], int(job[1])) for job in jobs]
    round_robin(int(quantum), jobs)

Submission status: Accepted

Solution code:
queue = []
 
n, q = map(int, input().split())
for i in range(n):
  name, time = input().split()
  queue.append([name, int(time)])
 
t = 0
while len(queue) > 0:
  entry = queue.pop(0)
  t += min(entry[1], q)
  entry[1] -= q
  if entry[1] > 0:
    queue.append(entry)
  else:
    print("{} {}".format(entry[0], t))

Submission status: Accepted

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

class my_queue:
    def __init__(self, queue):
        self.queue = queue
    def enqueue(self, item):
        self.queue = self.queue + [item]
        self.queue.append(item)
    def dequeue(self):
        if len(self.queue) != 0:
            item = self.queue[0]
            self.queue = self.queue[1:]
        else :
            item = None
        return item


time = 0
finish_task = []

while(len(queue) != 0):
    item = queue.pop(0)
    if item == None:
        break
    elif item[1] <= q:
        time += item[1]
        finish_task.append([item[0], time])
    else :
        time += q
        queue.append([item[0], item[1] - q])

for item in finish_task:
    print(item[0], item[1])

Submission status: Accepted

Solution code:
class My_Queue:

    def __init__(self, S):
        self.S = S
        self.q = [0 for i in range(S)]
        self.head = 0
        self.tail = 0

    def enqueue(self, x):
        if self.isFull():
            print('overflow')
            raise
        else:
            self.q[self.tail] = x

            if self.tail + 1 == self.S:
                self.tail = 0
            else:
                self.tail += 1

    def dequeue(self):
        if self.isEmpty():
            print('underflow')
            raise
        else:
            x = self.q[self.head]
            self.q[self.head] = 0

            if self.head + 1 == self.S:
                self.head = 0
            else:
                self.head += 1
            return x

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

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


def main():
    n, qms = map(int, input().split())
    elapsed_time = 0
    q = My_Queue(n + 1)
    for i in range(n):
        name, time = input().split()
        time = int(time)

        q.enqueue([name, time])

    while(q.head != q.tail):
        a = q.dequeue()
        if a[1] <= qms:
            elapsed_time += a[1]
            print(a[0], elapsed_time)
        else:
            a[1] -= qms
            elapsed_time += qms
            q.enqueue(a)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

n , q = map(int,input().split())
p , t = deque() , deque()
for i in range(n):
    x , y = input().split()
    p.append(x)
    t.append(int(y))
time = 0
temp1 = ""
temp2 = 0

while p:
    temp1 , temp2 = p.popleft() , t.popleft()
    if temp2 <= q :
        time += temp2
        print(temp1,time)
        temp1 , tmep2 = "" , 0
    else:
        time += q
        temp2 -= q
        p.append(temp1)
        t.append(temp2)

Submission status: Accepted

Solution code:
n,q = map(int,raw_input().split())
task = []
lst = []
a = 0
for i in range(n):
    name,time = raw_input().split()
    task.append(int(time))
    lst.append(name)
while True:
	if task[0] > q:
		task[0] = task[0] - q
		task.append(task.pop(0))
		lst.append(lst.pop(0))
		a += q
	else:
		a += task[0]
		print lst[0],a
		task.pop(0)
		lst.pop(0)
		if len(lst) == 0:
			break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
data = raw_input().split()
n = int(data[0])
q = int(data[1])

queue = []
for i in range(n):
	process = raw_input().split()
	process[1] = int(process[1])
	queue.append(process)

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

Submission status: Accepted

Solution code:
# encoding: utf-8
 
import sys
from collections import deque
 
input_ = sys.stdin.readlines()
 
 
def message_queue():
    # write your code here
    n, shard = map(int, input_[0].split())
 
    task_deque = deque(map(lambda x: x.split(), input_[1:]))
    t_accu = 0
 
    while task_deque:
        t_name, t_time = task_deque.popleft()
        t_time = int(t_time)
        if t_time <= shard:
            t_accu += t_time
            print(t_name, t_accu)
        else:
            t_time -= shard
            t_accu += shard
            task_deque.append([t_name, t_time])
 
 
message_queue()

Submission status: Accepted

Solution code:
# encoding: utf-8
from collections import deque

def round_robin_scheduling(N, Q, A):
    t = 0
    while A:
        process = A.popleft()
        if process[1] <= Q:
            t += process[1]
            print(process[0],t) 
        else:
            t += Q
            A.append([process[0],process[1]-Q])

if __name__ == '__main__':
    N,Q = map(int,input().split())
    A = deque()
    for i in range(N):
        a = input()
        A.append([a.split()[0],int(a.split()[1])])

    round_robin_scheduling(N,Q,A)

Submission status: Accepted

Solution code:
class Queue:
    def __init__(self, limit):
        self.Q = [None]*limit
        self.head = 0
        self.tail = 0
        self.limit = limit

    def initialize(self):
        self.head = 0
        self.tail = 0

    def enqueue(self,x):
        self.Q[self.tail] = x
        self.tail = (self.tail+1) % self.limit

    def dequeue(self):
        if self.isEmpty():
            print('error')
        else:
            self.head = (self.head+1) % self.limit
            return self.Q[self.head-1]

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

if __name__ == '__main__':

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

    Q = Queue(n+10)

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

    c = 0

    while Q.head != Q.tail:
        proc = Q.dequeue()
        if proc[1]-q > 0:
            Q.enqueue([proc[0],proc[1]-q])
            c += q
        else:
            c += proc[1]
            print(proc[0],c)

Submission status: Accepted

Solution code:
def main() :
    n, q = [int(i) for i in input().split()]
    names = []
    times = []
    elapsed_time = 0
    for i in range(n) :
        inp = input().split()
        names.append(inp[0])
        times.append(int(inp[1]))


    while len(names) != 0 :
        if times[0] > q :
            tmp1 = names.pop(0)
            names.append(tmp1)
            tmp2 = times.pop(0) - q
            times.append(tmp2)
            elapsed_time += q
        else :
            tmp = names.pop(0)
            elapsed_time += times[0]
            times.pop(0)
            print(tmp, elapsed_time)


if __name__ == '__main__' :
    main()

Submission status: Accepted

Solution code:
import queue
procs = queue.Queue()

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

for i in range(n):
    name,time = input().split()
    procs.put((name,int(time)))
time=0
doneprocs = queue.Queue()
while True:
    if procs.empty():
        break
    temp = procs.get()
    if temp[1] <= q:
        time += temp[1]
        doneprocs.put((temp[0],time))
    else:
        time+= q
        procs.put((temp[0],temp[1]-q))
for i in range(n):
    temp = doneprocs.get()
    print(temp[0],temp[1])

Submission status: Accepted

Solution code:
from collections import OrderedDict

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

for i in range(n):
    name, time = input().split()
    od.update({name: int(time)})
total = 0

while len(od) > 0:
    name, time = od.popitem(False)
    if time <= q:
        total += time
        print("{} {}".format(name, total))
    else:
        total += q
        time -= q
        od.update({name: time})

Submission status: Accepted

Solution code:
from collections import deque


def main():
    process_count, process_time, processes = get_input()
    result = run_roundrobin(processes=processes, time=process_time)
    for r in result:
        print(r.name, r.time)


def run_roundrobin(processes, time, total_time=0, result=[]):
    while processes:
        p = processes.popleft()
        if p.time <= time:
            total_time += p.time
            p.time = total_time
            result.append(p)
        else:
            total_time += time
            p.time -= time
            processes.append(p)

    return result


def get_input():
    count, time = list(map(lambda x: int(x), input().split(' ')))

    processes = deque([])
    for _ in range(count):
        im = input().split(' ')
        p = Process(im[0], int(im[1]))
        processes.append(p)

    return count, time, processes


class Process():

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


main()

Submission status: Accepted

Solution code:
class Queue():
    def __init__(self):
        self.l = []
    
    def push(self, e):
        self.l.append(e)
        
    def pop(self):
        return self.l.pop(0)


queue = Queue()
n, q = tuple([int(x) for x in input().split()])
for _ in range(n):
    tmp = input().split()
    queue.push([tmp[0], int(tmp[1])])

time = 0
while queue.l:
    process = queue.pop()
    if process[1] <= q:
        time += process[1]
        print(' '.join([process[0], str(time)]))
    else:
        time += q
        queue.push([process[0], process[1]-q])

Submission status: Accepted

Solution code:
class Process:
    def __init__(self, name_time):
        self.name = name_time[0]
        self.time = int(name_time[1])


process_amount, quontam = (int(i) for i in input().split(' '))

processes = [Process(input().split(' ')) for i in range(process_amount)]

i = 0
total_time = 0
while len(processes) != 0:
    process = processes[0]
    if process.time <= quontam:
        total_time += process.time
        print('{} {}'.format(process.name, total_time))
    else:
        total_time += quontam
        process.time -= quontam
        processes.append(process)

    processes.pop(0)

Submission status: Accepted

Solution code:
import queue

n, quantum = map(int, input().split())
q = queue.Queue()
for _ in range(n):
    name, time = input().split()
    q.put([name, int(time)])

spend_time = 0
while q.qsize() > 0:
    name, time = q.get()
    tmp = min(quantum, time)
    spend_time += tmp
    time -= tmp
    if time == 0:
        print('{} {}'.format(name, spend_time))
    else:
        q.put([name, time])

Submission status: Accepted

Solution code:
def Queue(l, nowtime, sec):
	if len(l) == 1:
		print(l[0][0], nowtime + l[0][1])
		return [], 0

	else:
		tl = l.pop(0)

		if tl[1] <= sec:
			nowtime += tl[1]
			print(tl[0], nowtime)
			return l, nowtime

		else:
			nowtime += sec
			l.append([tl[0], tl[1] - sec])
			return l, nowtime



if __name__ == '__main__':
	N, sec = input().split()
	N, sec = int(N), int(sec)
	lists = list()

	for i in range(N):
		obj, times = input().split()
		lists.append([obj, int(times)])

	now = 0
	while lists:
		lists, now = Queue(lists, now, sec)

Submission status: Accepted

Solution code:
n, f = map(int, input().split())
d = []

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

t = 0
while len(d):
    if d[0][1] > f:
        t += f
        d[0][1] -= f
        d.append(d.pop(0))
    else:
        t += d[0][1]
        a = d.pop(0)
        print(a[0], t)

Submission status: Accepted

Solution code:

import collections
#データの入力
a=input().split()

number=int(a[0])
quantum=int(a[1])

#保持データの作成
time=0

#キューの作成
name=[]
name=collections.deque()
q=[]
q=collections.deque()

#結果の保持
result=[[0 for i in range(2)]for j in range(number)]
endednumber=0

totaltime=0

for i in range(number):
    b=input().split()
    name.append(b[0])
    q.append(int(b[1]))
    totaltime+=int(b[1])

#キューの処理
while totaltime>time:
    cn=name.popleft()
    cq=q.popleft()

    if cq<=quantum:
        time+=cq

        result[endednumber][0]=cn
        result[endednumber][1]=time
        endednumber+=1
    else :
        time+=quantum
        newq=cq-quantum
        name.append(cn)
        q.append(newq)

#結果の表示
for i in range(number):
    print(result[i][0],result[i][1])

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque(f(*input().split()) for _ in range(n))
ans = 0
li = []
while dq:
    s, t = dq.popleft()
    if t <= q:
        ans += t
        li.append(s + " " + str(ans))
    else:
        ans += q
        dq.append((s, t - q))
print(*li, sep="\n")

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque(f(*input().split()) for _ in range(n))
cnt = 0
ans = []
while dq:
    s, t = dq.popleft()
    if t <= q:
        cnt += t
        ans.append(f"{s} {cnt}")
    else:
        cnt += q
        dq.append((s, t - q))
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.readline
n, q = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque(f(*readline().split()) for _ in range(n))
cnt = 0
ans = []
while dq:
    s, t = dq.popleft()
    if t <= q:
        cnt += t
        ans.append(f"{s} {cnt}")
    else:
        cnt += q
        dq.append((s, t - q))
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.readline
n, q = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque(f(*readline().split()) for _ in range(n))
cnt = 0
ans = []
while dq:
    s, t = dq.popleft()
    if t <= q:
        cnt += t
        ans.append(s + " " + str(cnt))
    else:
        cnt += q
        dq.append((s, t - q))
print(*ans, sep="\n")

Submission status: Accepted

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

class RingBuffer(list):
    def __init__(self, MAX):
        list.__init__(self)
        self.head = 0
        self.tail = 0
        self.MAX = MAX
        self[:] = [None]*MAX

    def isEmpty(self):
        return self.head == self.tail
    
    def enque(self,x):
        self[self.tail] = x
        if self.tail + 1 == self.MAX:
            self.tail = 0
        else:
            self.tail += 1

    def deque(self):
        x = self[self.head]
        if self.head + 1 == self.MAX:
            self.head = 0
        else:
            self.head += 1
        return x



if __name__ == '__main__':

    n, q = [int(c) for c in input().split(" ")]
    P = RingBuffer(n+1)
    
    for i in range(n):
        P_name, P_time = input().split(" ")
        P.enque([P_name, int(P_time)])

    time = 0
    while not P.isEmpty():
        P_name, P_time = P.deque()
        
        if P_time > q:
            time = time + q
            P_time = P_time - q
            P.enque([P_name, P_time])
        else:
            time = time + P_time
            print(P_name, time)

Submission status: Accepted

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

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

total = 0
while True:
    if q >= que[0][1]:
        total += que[0][1]
        print(que[0][0], total)
        que.pop(0)
    else:
        total += q
        que[0][1] -= q
        que.append(que[0])
        que.pop(0)
    if len(que) == 0:
        break

Submission status: Accepted

Solution code:
n, quantum = list(map(int, input().split()))
queue = []
sumOfTime = 0

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

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

Submission status: Accepted

Solution code:
from collections import deque;

count, time_slice = map(int, input().split(" "));

data = [];
for i in range(count):
    data += input().split(" ");

def round_robin(data, count, time_slice):
    t = 0;
    data = [[data[i], int(data[i + 1])] for i in range(0, len(data), 2)];
    que = deque(data);
    while len(que) > 0:
        current = que.popleft();
        if current[1] > time_slice:
            current[1] -= time_slice;
            t += time_slice;
            que.append(current);
        else:
            t += current[1];
            print(current[0], t);


round_robin(data, count, time_slice);

Submission status: Accepted

Solution code:
from collections import deque

n,q=[int(i) for i in input().split()]
name=[0 for i in range(n)]
time=[0 for i in range(n)]
answer_name=[]
answer_time=[]
total_time=0

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

d_name = deque(name)
d_time = deque(time)

while len(d_name)>0:

    a=d_name.popleft()
    b=int(d_time.popleft())

    if b>q:
        d_name.append(a)
        d_time.append(b-q)
        total_time=total_time+q

    else:
        total_time=total_time+b
        answer_name.append(a)
        answer_time.append(total_time)

for i in range(len(answer_time)):
    print(answer_name[i], answer_time[i])

Submission status: Accepted

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

t = 0
while len(qs) > 0:
    task = qs.pop(0)
    if task[1] <= q:
        t += task[1]
        print(task[0], t)
    else:
        t += q
        task[1] -= q
        qs.append(task)

Submission status: Accepted

Solution code:
#getting input
statements = []
while True:
    try:
        line = str(input())
    except EOFError:
        break
    statements.append(line)

pair = statements.pop(0).split()
amount = int(pair[0])
quantum = int(pair[1])

#process input
queue = []
for state in statements:
    pair = state.split()
    name = pair[0]
    time = int(pair[1])
    queue.append([name,time])

#processing queue

done_list = list()
time_spend = 0
while True:
    if not len(queue) > 0:
        break
    task = queue[0]


    if task[1] >= quantum: #time left
        time_spend += quantum
        task[1] = task[1] - quantum
    else:
        time_spend += task[1]
        task[1] = 0

    if task[1] <= 0: #finished
        task.append(time_spend)   #name,left_time, finished time
        done_list.append((task[0],task[2])) #name and finished time
        queue.remove(task)
    else:
        temp = task
        queue.remove(task)
        queue.append(temp)

for result in done_list:
    print(result[0],end=' ')
    print(result[1])

Submission status: Accepted

Solution code:
from collections import deque

header = input().split(" ")
N = int(header[0])
q = int(header[1])

queue = deque([])

for i in range(N):
    row = input().split(" ")
    queue.append((row[0], int(row[1])))

totalTime = 0

while queue:
    task = queue.popleft()
    name = task[0]
    time = task[1]
    leftTime = time - q
    if leftTime > 0:
        totalTime += q
        queue.append((name, leftTime))
    else:
        totalTime += time
        print(name + " " + str(totalTime))