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 namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        if self.isFull():
            raise IndexError("queue is full")
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail = 0

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        e = self._l[self._head]
        self._l[self._head] = None
        self._head += 1
        if self._head > self.n:
            self._head = 0
        return e

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

    def isFull(self):
        return self._head == (self._tail + 1) % (self.n + 1)

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))

Submission status: Accepted

Solution code:
from collections import deque

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

que = deque(ps)
time = 0

while que:

    p, rest = que.popleft()

    elapsed = min(q, int(rest))
    time += elapsed
    rest = str(int(rest) - elapsed)

    if int(rest) > 0:
        que.append([p, rest])
    else:
        print(p, time)

Submission status: Accepted

Solution code:
n, q = map(int, input().split(' '))
p = []
time = 0
counter = 0
for i in range(n):
    tmp_n, tmp_p = input().split(' ')
    tmp_p = int(tmp_p)
    p.append((tmp_n, tmp_p))

while len(p) > 0:
    task = p.pop(0)
    tmp_p = task[1] - q
    if( tmp_p > 0):
        time += q
        p.append((task[0], tmp_p))
    elif( tmp_p <= 0):
        time += task[1]
        print('{} {}'.format(task[0], time))

Submission status: Accepted

Solution code:
from collections import deque

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

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

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
queue = deque()
for i in range(n):
    name, time = input().split()
    queue.append({"name": name, "time": int(time)})
t = 0
while len(queue) != 0:
    #print([queue[i]["time"] for i in range(len(queue))])
    head = queue.popleft()
    if head["time"] <= q:
        t += head["time"]
        name = head["name"]
        print("{} {}".format(name, t))
    else:
        queue.append({"name": head["name"], "time": head["time"] - q})
        t += q

Submission status: Accepted

Solution code:
from collections import deque

n, q = [int(_) for _ in input().split()]
processes = deque([tuple(input().split()) for i in range(n)])

time = 0
while processes:
    process = processes.popleft()

    if int(process[1]) <= q:
        time += int(process[1])
        print(process[0], time)

    else:
        time += q
        processes.append((process[0], int(process[1]) - q))

Submission status: Accepted

Solution code:
# ALDS1_3_B.
# ラウンドロビンスケジューリング。

def main():
    data = input().split()
    n = int(data[0]); q = int(data[1])
    list = [] # ここに順に格納していく感じ。
    for i in range(n):
        list.append(input().split())
        list[i][1] = int(list[i][1])
    t = 0
    while len(list) > 0:
        if list[0][1] > q:
            name = list[0][0]; time = list[0][1]
            del list[0]
            list.append([name, time - q])
            t += q
        else:
            t += list[0][1]
            print(list[0][0] + " " + str(t))
            del list[0]

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding:utf-8
n, t_u = map(int,input().split())
name_pro_dict = {}
for i in range(n):
    name, pro = input().split()
    name_pro_dict[name] = int(pro)

t = 0
while name_pro_dict:
    keys = list(name_pro_dict.keys())
    for key in keys:
        if name_pro_dict[key] <= t_u:
            t += name_pro_dict[key]
            print(key, t)
            del name_pro_dict[key]
        else:
            name_pro_dict[key] -= t_u
            t += t_u

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]
que = []
ans_count = 0 #出力数
time = 0 #経過時間
# data読み込み
for i in range(n):
    que.append([i for i in input().split()])

while(ans_count!=n):
    pop_process = que.pop(0)
    pop_process[1] = int(pop_process[1])
    # 末尾から追加、先頭から取り出す
    if pop_process[1] <= q:
        time += pop_process[1]
        print('{0} {1}'.format(pop_process[0], time))
        ans_count += 1
    else:
        time += q
        pop_process[1] -= q
        que.append(pop_process)

Submission status: Accepted

Solution code:
import queue


nq = input().split()
n = int(nq[0])
q = int(nq[1])

qu = queue.Queue()
for _ in range(n):
    nt = input().split()
    qu.put({"name": nt[0], "time": int(nt[1])})

t = 0
while not(qu.empty()):
    process = qu.get()
    if process["time"] > q:
        process["time"] -= q
        t += q
        qu.put(process)
    else:
        t += process["time"]
        print(process["name"], t)

Submission status: Accepted

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


class Queue:
    def __init__(self):
        self.l = []

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

    def dequeue(self):
        x = self.l[0]
        del self.l[0]
        return x

    def is_empty(self):
        return len(self.l) == 0


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


if __name__ == '__main__':
    n, q = map(int, raw_input().split())
    Q = Queue()
    i = 0
    while i != n:
        tmp = raw_input().split()
        Q.enqueue(Process(tmp[0], int(tmp[1])))
        i += 1
    cnt = 0
    while not Q.is_empty():
        p = Q.dequeue()
        if p.time > q:
            p.time -= q
            cnt += q
            Q.enqueue(p)
        else:
            cnt += p.time
            print p.name, cnt

Submission status: Accepted

Solution code:
from collections import deque, namedtuple

Process = namedtuple('Process', 'name, time')

def main():
    n, q = map(int, input().split())
    dq = deque()
    for _ in range(n):
        name, time = input().split()
        dq.append(Process(name, int(time)))
    cur_time = 0
    while len(dq) > 0:
        x = dq.popleft()
        if x.time > q:
            dq.append(Process(x.name, x.time-q))
            cur_time += q
        else:
            cur_time += x.time
            print(x.name, cur_time)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import deque

def main():
    n ,q = map(int, stdinput().split())
    ps = deque()
    for _ in range(n):
        p, t = stdinput().split()
        t = int(t)
        ps.append([p, t])

    ctime = 0
    while len(ps) > 0:
        p = ps.popleft()
        if p[1] <= q:
            ctime += p[1]
            print(f'{p[0]} {ctime}')
        else:
            ctime += q
            p[1] -= q
            ps.append(p)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

queue = []

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

time = 0
ans = []

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

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

Submission status: Accepted

Solution code:
from queue import Queue

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

	def set_time(self, time):
		self.time = time

n, q = map(int, input().split())
queue = Queue()
for i in range(n):
	name, time = map(str, input().split())
	queue.put(Process(name, time))

time_elapsed = 0
while queue.empty() != True:
	process = queue.get()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		queue.put(process)
	else:
		time_elapsed += min(time, q)
		print(process.name + ' ' + str(time_elapsed))

Submission status: Accepted

Solution code:
from queue import Queue

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

	def set_time(self, time):
		self.time = time

n, q = map(int, input().split())
queue = Queue()
for i in range(n):
	name, time = map(str, input().split())
	queue.put(Process(name, time))

time_elapsed = 0
while queue.empty() != True:
	process = queue.get()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		queue.put(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

Submission status: Accepted

Solution code:

class Node:
	def __init__(self, value):
		self.value = value
		self.next = None

class SimpleQeueue:
	def __init__(self):
		self.size = 0
		self.head = None
		self.tail = None
		return

	def enqueue(self, value):
		node = Node(value)
		if self.head == None:
			self.head = node
			self.tail = node
		else:
			self.tail.next = node
			self.tail = node
		self.size += 1

	def dequeue(self):
		if self.is_empty() == True:
			return None
		node = self.head
		self.head = self.head.next
		self.size -= 1
		if self.is_empty() == True:
			self.head = None
			self.tail = None
		return node.value

	def peek(self):
		if self.is_empty() == True:
			return None
		return self.head.value

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def __str__(self):
		str_ = '['
		current = self.head
		while current != None:
			str_ += str(current.value)
			current = current.next
			if current != None:
				str_ += ', '
		str_ += ']['
		if self.tail != None:
			str_ += str(self.tail.value)
		str_ += ']'
		return str_

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

	def set_time(self, time):
		self.time = time

n, q = map(int, input().split())
myqueue = SimpleQeueue()
for i in range(n):
	name, time = map(str, input().split())
	myqueue.enqueue(Process(name, time))

time_elapsed = 0
while myqueue.is_empty() != True:
	process = myqueue.dequeue()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		myqueue.enqueue(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

Submission status: Accepted

Solution code:

class Node:
	def __init__(self, value):
		self.value = value
		self.next = None

class SimpleQueue:
	def __init__(self):
		self.size = 0
		self.head = None
		self.tail = None
		return

	def enqueue(self, value):
		node = Node(value)
		if self.head == None:
			self.head = node
			self.tail = node
		else:
			self.tail.next = node
			self.tail = node
		self.size += 1

	def dequeue(self):
		if self.is_empty() == True:
			return None
		node = self.head
		self.head = self.head.next
		if self.head == None:
			self.tail = None
		self.size -= 1
		return node.value

	def peek(self):
		if self.is_empty() == True:
			return None
		return self.head.value

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def __str__(self):
		str_ = '['
		current = self.head
		while current != None:
			str_ += str(current.value)
			current = current.next
			if current != None:
				str_ += ', '
		str_ += ']['
		if self.tail != None:
			str_ += str(self.tail.value)
		str_ += ']'
		return str_

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

	def set_time(self, time):
		self.time = time

n, q = map(int, input().split())
myqueue = SimpleQueue()
for i in range(n):
	name, time = map(str, input().split())
	myqueue.enqueue(Process(name, time))

time_elapsed = 0
while myqueue.is_empty() != True:
	process = myqueue.dequeue()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		myqueue.enqueue(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

Submission status: Accepted

Solution code:
class Process(object):
    def __init__(self, name, s):
        self.name = name
        self.s = s

    def exec(self, q):
        if self.s <= q:
            time = self.s
            self.s = 0
            return time
        else:
            time = q
            self.s = self.s - q
            return time

def execute(processes, q):
    time = 0
    completed = []
    while processes:
        p = processes.pop()
        time += p.exec(q)
        if p.s == 0:
            completed.append((p, time))
        else:
            processes.insert(0, p)
    return completed

def run():
    n, q = list(map(int, input().split()))
    processes = []
    for _ in range(n):
        name, time = input().split()
        processes.append(Process(name, int(time)))
    processes.reverse()
    completed = execute(processes, q)
    for c in completed:
        print(c[0].name, c[1])

if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
class ProcessQueue:
    def __init__(self, N):
        self.MAX = N + 1
        self.Q = [(None, None)] * self.MAX
        self.head = 0
        self.tail = 0

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

    def enqueue(self, p):
        self.Q[self.tail] = p
        self.tail += 1
        if self.tail == self.MAX:
            self.tail = 0

    def dequeue(self):
        self.head += 1
        if self.head == self.MAX:
            self.head = 0

        return self.Q[self.head - 1]


N, Q = (int(n) for n in input().split())
processes = ProcessQueue(N)
for i in range(N):
    process = input().split()
    processes.enqueue((process[0], int(process[1])))

time = 0
while not processes.isEmpty():
    process = processes.dequeue()
    if process[1] - Q <= 0:
        time += process[1]
        print(process[0], time)
    else:
        time += Q
        processes.enqueue((process[0], process[1] - Q))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    N, Q = [int(i) for i in input().split()]
    Ts = deque([])

    for _ in range(N):
        name, time = [i for i in input().split()]
        Ts.append({
            "name": name,
            "time": int(time)
        })

    cur_t = 0

    while len(Ts):
        task = Ts.popleft()

        # 終わらない
        if task["time"] > Q:
            task["time"] -= Q
            cur_t += Q
            Ts.append(task)
        else:
            cur_t += task["time"]
            print(task["name"], cur_t)


main()

Submission status: Accepted

Solution code:
from collections import deque
 
n,p=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],p)
    atop[1]-=spend
    t+=spend
    if(atop[1]==0):
        print(atop[0],t)
    else:
        que.append(atop)

Submission status: Accepted

Solution code:
process_num, qms = map(int, input().split())
raw_procs = [input() for i in range(process_num)]


if __name__ == '__main__':
    procs = []
    for row in raw_procs:
        name, time = row.split()
        procs.append({
            "name": name,
            "time": int(time),
        })

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

Submission status: Accepted

Solution code:
def fn(x):
    return [x[0], int(x[1])]

N, q = map(int, input().split())
name_time = list(map(fn, [ input().split() for n in range(N) ]))
sec = 0; finish =[];

while True:
    if len(name_time) == 0:
        break
    elif name_time[0][1] > q:
        sec += q
        name_time[0][1] -= q
        name_time.append(name_time.pop(0))
    else:
        sec += name_time[0][1]
        finish.append([name_time.pop(0)[0],sec])
for f in finish:
    print(*f)

Submission status: Accepted

Solution code:
from collections import deque
 
process_num, qms = map(int ,input().split())
que = deque({})
 
for i in range(process_num):
    name, time = input().split()
    time = int(time)
    que.append({"name":name, "time":time})
 
if __name__ == '__main__':
    total_time = 0
    while len(que)>0:
        atop = que.popleft()
        spend = min(atop["time"], qms)
        atop["time"] -= spend
        total_time += spend
        if(atop["time"] == 0):
            print("{} {}".format(atop["name"], total_time))
        else:
            que.append(atop)

Submission status: Accepted

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

raw_proc = [input() for i in range(N)]
queue = [{} for i in range(N+1)]
for i in range(N):
    name, time = raw_proc[i].split()
    time = int(time)
    queue[i] = {"name": name, "time": time}

def enque(tail, p):
    queue[tail] = p
    tail = (tail+1) % (N+1)
    return tail

def deque(head):
    proc = queue[head]
    head = (head+1) % (N+1)
    return proc, head


if __name__ == '__main__':
    head = 0
    tail = N
    total_time = 0
    while head != tail:
        p, head = deque(head)
        t = min(qms, p["time"])
        p["time"] = p["time"] - t
        total_time += t
        if p["time"] > 0:
            tail = enque(tail, p)
        else:
            print("{} {}".format(p["name"], total_time))

Submission status: Accepted

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

raw_proc = [input() for i in range(N)]
queue = [{} for i in range(N+1)]
limit = N+1
for i in range(N):
    name, time = raw_proc[i].split()
    time = int(time)
    queue[i] = {"name": name, "time": time}

def enque(tail, p):
    queue[tail] = p
    tail = (tail+1) % limit
    return tail

def deque(head):
    proc = queue[head]
    head = (head+1) % limit
    return proc, head


if __name__ == '__main__':
    head = 0
    tail = N
    total_time = 0
    while head != tail:
        p, head = deque(head)
        t = min(qms, p["time"])
        p["time"] = p["time"] - t
        total_time += t
        if p["time"] > 0:
            tail = enque(tail, p)
        else:
            print("{} {}".format(p["name"], total_time))

Submission status: Accepted

Solution code:
def main():
    # データ入力
    n, q = input().split()
    n = int( n )
    q = int( q )
    name = []
    time = []
    for i in range(n):
        tmp_n, tmp_t = input().split()
        name.append( tmp_n )
        time.append( int( tmp_t ) )

    # 処理
    count = 0
    while n > 0:
        if time[0] > q:
            time[0] -= q
            count += q
            time.append( time[0] )
            time.pop( 0 )
            name.append( name[0] )
            name.pop( 0 )
        else :
            count += time[0]
            print( name[0], end=" " )
            print( str( count ) )
            time.pop( 0 )
            name.pop( 0 )
            n -= 1

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
que = []
for i in range(n):
    p, t = map(str, input().split())
    que.append([p, int(t)])
total = 0
while que != []:
    pt = que.pop(0)
    if pt[1] <= q:
        total += pt[1]
        print(pt[0], total)
    else:
        total += q
        que.append([pt[0], pt[1]-q])

Submission status: Accepted

Solution code:
n ,q = map(int, input().split())
ntlist = []
for i in range(n):
    nt = list(map(str, input().split()))
    ntlist.append(nt)
tp = 0
while (len(ntlist)):
    nt = ntlist.pop(0)
    if (int(nt[1])> q):
        nt[1] = int(nt[1]) - q
        tp += q
        ntlist.append(nt)
    else:
        tp += int(nt[1])
        nt[1] = 0
        print(nt[0], tp)

Submission status: Accepted

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


def simu(pro, q):
    end_time = 0

    while bool(pro):
        if pro[1] > q:
            end_time += q
            pro.append(pro[0])
            pro.append(pro[1] - q)
            pro.pop(0)
            pro.pop(0)

        elif pro[1] <= q:
            end_time += pro[1]
            print('{0} {1}'.format(pro[0], end_time))
            pro.pop(0)
            pro.pop(0)


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

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

simu(pro, q)

Submission status: Accepted

Solution code:
N,q = list(map(int,input().split(' ')))
que=[]
time=0
for _ in range(N):
    t1,t2 = input().split(' ')
    que.append((t1,int(t2)))
while len(que)!=0:
    task = que.pop(0)
    if task[1] > q:
        ntask = (task[0],task[1] -q)
        que.append(ntask)
        time +=q
    else:
        print(task[0],time+task[1])
        time +=task[1]

Submission status: Accepted

Solution code:
n, q = [int(t) for t in input().split()]
procs = []
for i in range(n):
    name, tm = input().split()
    procs.append((name, int(tm)))

curtime = 0
while len(procs) > 0:
    queue = []
    for name, tm in procs:
        if tm <= q:
            curtime += tm
            print(name, curtime)
        else:
            curtime += q
            queue.append((name, tm - q))
    procs = queue

Submission status: Accepted

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

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

stime = 0
while que:
    job = que.pop(0)
    if job[1]<=q:
        stime += job[1]
        print job[0], stime
    else:
        stime += q
        job[1] -= q
        que.append(job)

Submission status: Accepted

Solution code:
import sys

class Queue:
    class Cell:
        def __init__(self, x, y = None):
            self.data = x
            self.next = y

    def __init__(self):
        self.size = 0
        self.rear = None

    def enqueue(self, x):
        if self.size == 0:
            self.rear = Queue.Cell(x)
            self.rear.next = self.rear
        else:
            new_cell = Queue.Cell(x, self.rear.next)
            self.rear.next = new_cell
            self.rear = new_cell
        self.size += 1

    def dequeue(self):
        if self.size == 0: raise IndexError
        front = self.rear.next
        self.rear.next = front.next
        self.size -= 1
        if self.size == 0: self.rear = None
        return front.data

    def isEmpty(self):
        return self.size == 0

def generator(n1, n2, step=1):
    n = n1
    while n<n2:
        yield n
        n+= step

n, q = map(int, raw_input().split())
 
A = Queue()
for i in generator(0,n):
    name, time = raw_input().split()
    A.enqueue([name, int(time)])

stime = 0
while not A.isEmpty():
    job = A.dequeue()
    if q>=job[1]:
        stime += job[1]
        print job[0], stime
    else:
        stime += q
        job[1] -= q
        A.enqueue(job)

Submission status: Accepted

Solution code:
import sys

class Queue:
    class Cell:
        def __init__(self, x, y = None):
            self.data = x
            self.next = y

    def __init__(self):
        self.size = 0
        self.rear = None

    def enqueue(self, x):
        if self.size == 0:
            self.rear = Queue.Cell(x)
            self.rear.next = self.rear
        else:
            new_cell = Queue.Cell(x, self.rear.next)
            self.rear.next = new_cell
            self.rear = new_cell
        self.size += 1

    def dequeue(self):
        if self.size == 0: raise IndexError
        front = self.rear.next
        self.rear.next = front.next
        self.size -= 1
        if self.size == 0: self.rear = None
        return front.data

    def isEmpty(self):
        return self.size == 0

def generator(n1, n2, step=1):
    n = n1
    while n<n2:
        yield n
        n+= step

n, q = map(int, raw_input().split())
TASK=[]
A = Queue()
for i in generator(0,n):
    name, time = raw_input().split()
    TASK.append([name, int(time)])
    A.enqueue(i)

stime = 0
while not A.isEmpty():
    p = A.dequeue()
    if q>= TASK[p][1]:
        stime += TASK[p][1]
        print TASK[p][0], stime
    else:
        stime += q
        TASK[p][1] -= q
        A.enqueue(p)

Submission status: Accepted

Solution code:
import collections

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

datalist = collections.deque(datalist)

time = 0
while len(datalist):
	data = datalist.popleft()
	data[1] = int(data[1])
	
	if data[1] <= q:
		time += data[1]
		print data[0], time
		
	else:
		time += q
		data[1] = data[1] - q
		datalist.append(data)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin

num, qt = (int(s) for s in stdin.readline().split())
rrs = []
for _ in range(num):
    name, time = stdin.readline().split()
    rrs.append([name, int(time)])

process = 0
while rrs:
    rrs[0][1] -= qt
    if rrs[0][1] <= 0:
        process += qt + rrs[0][1]
        print(rrs.pop(0)[0], process)
    else:
        process += qt
        p = rrs.pop(0)
        rrs.append(p)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
from Queue import Queue
import collections
start = time.clock()
i = 0
(n,q)=map(int, raw_input().split())
datalist=[raw_input().split() for _ in range(n)]
datalist=collections.deque(datalist)
#print(datalist)
while len(datalist):
    data=datalist.popleft()
#    print data,datalist
    data[1]=int(data[1])
    if data[1]<=q:
        i+=data[1]
        print data[0],i
    else:
        i+=q
        data[1]-=q
        datalist.append(data)

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
t = 0
while que:
	a = que.pop(0)
	if int(a[1]) <= q:
		t += int(a[1])
		print a[0],t				
	else:
		t += q
		que.append([a[0],int(a[1])-q])

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	a = que[i]
	if 0 < a[1] <= q:
		t += a[1]
		que.pop(i)
		n -= 1
		print a[0],t				
	elif q < a[1]:
		t += q
		que[i][1] = a[1] - q
		i += 1

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	a,c = que[i][0],que[i][1]
	if c <= q:
		t += c
		que.pop(i)
		n -= 1
		print a,t				
	else:
		t += q
		que[i][1] = c - q
		i += 1

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if que[i][1] <= q:
		t += que[i][1]
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] -= q
		i += 1

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = []
for i in range(n):
	inp = map(str, raw_input().split())
	que.append([inp[0],int(inp[1])])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if que[i][1] <= q:
		t += que[i][1]
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] -= q
		i += 1

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
mxt = sum([int(y) for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if int(que[i][1]) <= q:
		t += int(que[i][1])
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] = int(que[i][1]) - q
		i += 1

Submission status: Accepted

Solution code:
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
    if i >= n: i %= n
    if que[i][1] <= q:
        t += que[i][1]
        print que[i][0],t
        que.pop(i)
        n -= 1
    else:
        t += q
        que[i][1] -= q
        i += 1

Submission status: Accepted

Solution code:
from collections import deque

n, qt = map(int, input().strip().split())
q = deque()
for i in range(n):
    name, time = input().strip().split()
    q.append((name, int(time)))

t = 0
while q:
    name, time = q.popleft()
    if time <= qt:
        t += time
        print(name, t)
    else:
        t += qt
        time -= qt
        q.append((name, time))

Submission status: Accepted

Solution code:
sum=0
s=0
A=input().split()
n=(int)(A[0])
m=(int)(A[1])
x=[0 for i in range(2*n)]
for i in range(n):
    B=input().split()
    k=2*i
    x[k]=(B[0])
    x[k+1]=(int)(B[1])
while len(x)>0:
    s=x[1]-m
    if s>0:
        sum+=m
        x.append(x[0])
        x.append(s)
        del x[0]
        del x[0]
    if s<=0:
        sum+=x[1]
        print( x[0],sum)
        del x[0]
        del x[0]