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 Process:
    def __init__(self, process_name, process_time):
        self.name = process_name
        self.time = process_time
        self.remaing_time = process_time
        self.complete_flg = False

    def get_name(self):
        return self.name

    def get_time(self):
        return self.time

    def get_remaining_time(self):
        return self.remaing_time

    def exec_process(self, quantum):
        if self.remaing_time <= quantum:
            self.complete_flg = True
            use_time = self.remaing_time
            self.remaing_time = 0
            return use_time
        else:
            self.remaing_time -= quantum
            return quantum

    def get_complete_flg(self):
        return self.complete_flg

if __name__ == '__main__':
    n, q = [int(x) for x in input().split()]
    all_time = 0
    procesies = []
    for _ in range(n):
        name, time = input().split()
        temp_process = Process(name, int(time))
        procesies.append(temp_process)

    while len(procesies) != 0:
        target_process = procesies.pop(0)
        all_time += target_process.exec_process(q)
        if target_process.get_complete_flg():
            print(target_process.get_name() + " " + str(all_time))
        else:
            procesies.append(target_process)

Submission status: Accepted

Solution code:
import sys
n, q = map(int, input().split())
queue = [[label, int(num)] for label, num in map(lambda line: line.strip().split(), sys.stdin.readlines())] 
i = 0
time = 0
while len(queue) > 0:
    i = i % len(queue)
    time += q
    queue[i][1] -= q
    if queue[i][1] <= 0:
        time += queue[i][1]
        print("{} {}".format(queue[i][0], time))
        queue.pop(i)
        i -= 1
    i += 1

Submission status: Accepted

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

    def process(self, quantum):
        if self.time <= quantum:
            time, self.time = self.time, 0
            return time

        self.time -= quantum
        return quantum

    def isProcessed(self):
        return self.time == 0

    def getName(self):
        return self.name

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

    queue = []
    for _ in range(n):
        queue.append(Process(input().split()))

    elapsed = 0
    while len(queue) > 0:
        p = queue.pop(0)
        elapsed += p.process(q)

        if p.isProcessed():
            print('{} {}'.format(p.getName(), elapsed))
        else:
            queue.append(p)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class a(object):
    def __init__(self, buf_size=10000000):
        self.m = [None for _ in range(buf_size)]
        self.ind = 0
        self.cur = 0

    def enqueue(self, elem):
        self.m[self.ind] = elem
        self.ind += 1

    def dequeue(self):
        if self.is_empty(): raise EOFError('this queue is empty')
        result = self.m[self.cur]
        self.cur += 1
        return result

    def is_empty(self):
        return self.ind <= self.cur

n, q = input().split()
qe = a()
cur_time = 0

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

while not qe.is_empty():
    name, time = qe.dequeue()
    if time > int(q):
        cur_time += int(q)
        time -= int(q)
        qe.enqueue((name, time))
    else:
        cur_time += int(time)
        print('{} {}'.format(name, cur_time))

Submission status: Accepted

Solution code:
n,ms = map(int, input().split())
a = [list(map(str, input().split())) for _ in range(n)]
total = 0
for i in a:
    tmp = int(i[1]) - ms
    if tmp <= 0:
        total += ms + tmp
        print("{} {}".format(i[0], total))
    else:
        total += ms
        a.append([i[0], tmp])

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
queue = deque()
for _ in range(n):
    name, time = input().split()
    queue.append((name, int(time)))

comp = []
t = 0
while queue:
    name, time = queue.popleft()
    if time <= q:
        t += time
        comp.append((name, t))
    else:
        t += q
        queue.append((name, time - q))
for s in comp:
    print(*s)

Submission status: Accepted

Solution code:

import Queue
n,q=map(int,raw_input().strip().split())
name=[]
time=[]
qu=Queue.Queue()
class task:
    def __init__(self,name,time):
        self.name=name
        self.time=time
        self.total=0
for i in xrange(n):
    inp=raw_input().strip().split()
    qu.put(task(inp[0],int(inp[1])))
curtime=0

while not qu.empty():
    tmp=qu.get()
    if tmp.time>q:
        tmp.time-=q
        curtime+=q
        qu.put(tmp)
    else:

        curtime+=tmp.time
        ed='' if qu.qsize()==0 else '\n'
        print tmp.name,curtime
#    print curtime

Submission status: Accepted

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

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

total_time = 0

count = 0
while(a):
    current = a.pop(0)
    if(current[1] > q):
        current[1] -= q
        a.append(current)
        total_time += q
    else:
        total_time += current[1]
        print(current[0],total_time,sep = " ")

Submission status: Accepted

Solution code:
class queue():
    def __init__(self):
        self.head = 0
        self.tail = 0
        self.MAX = 100000
        self.Q = [[0] for i in range(self.MAX)]

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

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

    def enqueue(self, x):
        if self.is_full():
            raise ValueError("エラー（オーバーフロー）")
        self.Q[self.tail] = x
        if self.tail + 1 == self.MAX:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            raise ValueError("エラー（アンダーフロー）")
        x = self.Q[self.head]
        if self.head + 1 == self.MAX:
            self.head = 0
        else:
            self.head += 1
        return x

n, q = input().split(" ")
n = int(n)
q = int(q)
q_name = queue()
q_val = queue()
for i in range(n):
    name, t = input().split(" ")
    t = int(t)
    q_name.enqueue(name)
    q_val.enqueue(t)

results = []
end_time = 0
while True:
    process_val = q_val.dequeue()
    process_name = q_name.dequeue()
    if process_val <= q:
        end_time += process_val
        results.append([process_name, end_time])
    else:
        end_time += q
        rest_val = process_val - q
        q_name.enqueue(process_name)
        q_val.enqueue(rest_val)
    
    if q_val.head == q_val.tail:
        break

for v in results:
    print("{} {}".format(v[0],v[1]))

Submission status: Accepted

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

    elapsed_time = 0
    while len(queue) > 0:
        process, time = queue.pop(0)
        if time > q:
            queue.append((process, time - q))
            elapsed_time += q
        else:
            elapsed_time += time
            print('{} {}'.format(process, str(elapsed_time)))
            
    return


main()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
queue = []
for i in range(n):
    x = input().split()
    queue.append((x[0], int(x[1])))
time = 0
while queue != []:
    if queue[0][1] <= q:
        time += queue[0][1]
        print('{} {}'.format(queue.pop(0)[0], time))
    else:
        time += q
        x = queue.pop(0)
        queue.append((x[0], x[1]-q))

Submission status: Accepted

Solution code:
from collections import deque

n,limit = map(int,input().split())
total_time = 0
queue = deque()
for _ in range(n):
    p,t = input().split()
    queue.append([p,int(t)])

while len(queue) > 0:
    head = queue.popleft()
    if head[1] <= limit:
        total_time += head[1]
        print('{} {}'.format(head[0],total_time))
    else:
        head[1] -= limit
        total_time += limit
        queue.append(head)

Submission status: Accepted

Solution code:
from collections import deque

n,limit = map(int,input().split())
queue = deque()
total_time = 0

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

while len(queue) > 0:
    head = queue.popleft()
    if   head[1] <= limit:
        total_time += head[1]
        print('{} {}'.format(head[0],total_time))
    elif head[1] > limit:
        head[1] -= limit
        total_time += limit
        queue.append(head)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
A = [list(map(str,input().split())) for i in range(n)]
time = 0
flag = 1
while len(A) > 0:
    if int(A[0][1]) <= q: #プロセスが完了する場合
        time += int(A[0][1])
        print(A[0][0],time)
        del A[0]
    else: #プロセスが完了しない場合
        time += q
        A[0][1] = str(int(A[0][1])-q)
        A.append(A[0])
        del A[0]

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(n):
    name, t = map(str, input().split())
    p_que.append([name, int(t)])

sum_time = 0
while True:
    p = p_que.popleft()
    if p[1] <= q:
        sum_time += p[1]
        print(p[0], sum_time)
    else:
        sum_time += q
        p[1] -= q
        p_que.append(p)
    if len(p_que) == 0:
        break

Submission status: Accepted

Solution code:
from queue import Queue

n, q_time = map(int, input().split())
qu = Queue()
for i in range(n):
    p_name, p_time = input().split()
    p_time = int(p_time)
    qu.put((p_name, p_time))

t = 0
while True:
    if qu.empty():
        break
    p_name, p_time = qu.get()
    if p_time <= q_time:
        t += p_time
        print(f'{p_name} {t}')
    else:
        t += q_time
        qu.put((p_name, p_time - q_time))

Submission status: Accepted

Solution code:
import queue

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

t = 0
while not queue.empty():
    name, time = queue.get()

    if time <= q:
        t += time
        print(name, t)
    else:
        queue.put((name, time - q))
        t += q

Submission status: Accepted

Solution code:
class Queue:
    queue_list = []
    def enqueue(self, a):
        self.queue_list.append(a)
    def dequeue(self):
        return self.queue_list.pop(0)
    def isEmpty(self):
        return len(self.queue_list) == 0

n, q = list(map(lambda x: int(x), input().strip().split()))

queue = Queue()

for i in range(n):
    queue.enqueue(input().strip().split())


sum_time = 0
while not queue.isEmpty():
    name, time = queue.dequeue()
    if int(time) <= q:
        sum_time += int(time)
        print(name + ' ' + str(sum_time))
    else:
        queue.enqueue([name, str(int(time) - q)])
        sum_time += q

Submission status: Accepted

Solution code:
class Queue:
    queue_list = []
    start = 0
    def enqueue(self, a):
        self.queue_list.append(a)
    def dequeue(self):
        assert self.start != len(self.queue_list), "オーバーフローが発生しました。"
        self.start += 1
        return self.queue_list[self.start - 1]
    def isEmpty(self):
        return self.start == len(self.queue_list)

n, q = list(map(lambda x: int(x), input().strip().split()))

queue = Queue()

for i in range(n):
    queue.enqueue(input().strip().split())


sum_time = 0
while not queue.isEmpty():
    name, time = queue.dequeue()
    if int(time) <= q:
        sum_time += int(time)
        print(name + ' ' + str(sum_time))
    else:
        queue.enqueue([name, str(int(time) - q)])
        sum_time += q

Submission status: Accepted

Solution code:
MAX_ARRAY_SIZE = 100000

class Queue:
    array = [None] * MAX_ARRAY_SIZE
    start = end = 0
    def enqueue(self, a):
        assert not self.isFull(), "オーバーフローが発生しました。"
        self.array[self.end] = a
        if self.end + 1 == MAX_ARRAY_SIZE:
            self.end = 0
        else:
            self.end += 1
    def dequeue(self):
        assert not self.isEmpty(), "アンダーフローが発生しました。"
        if (self.start + 1) == MAX_ARRAY_SIZE:
            self.start = 0
            return self.array[MAX_ARRAY_SIZE - 1]
        else:
            self.start += 1
            return self.array[self.start - 1]
    def isEmpty(self):
        return self.start == self.end
    def isFull(self):
        return self.start == (self.end + 1) % MAX_ARRAY_SIZE

n, q = list(map(lambda x: int(x), input().strip().split()))

queue = Queue()

for i in range(n):
    queue.enqueue(input().strip().split())


sum_time = 0
while not queue.isEmpty():
    name, time = queue.dequeue()
    if int(time) <= q:
        sum_time += int(time)
        print(name + ' ' + str(sum_time))
    else:
        queue.enqueue([name, str(int(time) - q)])
        sum_time += q

Submission status: Accepted

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

tasks = []
for _ in range(n):
    ni, ti = input().split()
    tasks.append((ni, int(ti)))

t = 0
while len(tasks) > 0:
    ni, ti = tasks.pop(0)
    if ti <= q:
        t += ti
        print(f'{ni} {t}')
    else:
        tasks.append((ni, ti - q))
        t += q

Submission status: Accepted

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

t = 0
while queue:
	name,time = queue.pop(0)
	t += min(q, time)
	if time > q:
		queue.append((name, time-q))
	else:
		print(name,t)

Submission status: Accepted

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

t = 0
i = 0
while queue:
	i %= len(queue)
	p = queue[i]
	t += min(q, p[1])
	if p[1] > q:
		p[1] -= q
		i += 1
	else:
		print(p[0], t)
		queue.pop(i)

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, int(time)))

t = 0
while queue:
	name,time = queue.popleft()
	t += min(q, time)
	if time > q:
		queue.append((name, time-q))
	else:
		print(name,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque

n,q = map(int,input().split())
f = lambda n,t: (n,int(t))
queue = deque(f(*l.split()) for l in sys.stdin)

t = 0
while queue:
	name,time = queue.popleft()
	t += min(q, time)
	if time > q:
		queue.append((name, time-q))
	else:
		print(name,t)

Submission status: Accepted

Solution code:
# -*- coding: utf 8 -*-
# Queue
# 2019. 4/25

LEN = 100005
Q = [0] * LEN
head = 0
tail = 0

class Queue:
    def __init__(self):
        self.name = 0
        self.t = 0

def enqueue(x):
    global tail
    Q[tail] = x
    tail = (tail + 1) % LEN

def dequeue():
    global head
    x = Q[head]
    head = (head + 1) % LEN
    return x

if __name__ == '__main__':
    elaps = 0
    n, q = map(int, input().split())
    for i in range(1, n + 1):
        u = Queue()
        name , time = input().split()
        u.name = name
        u.t = int(time)
        Q[i] = u

    head = 1
    tail = n + 1

    while(head != tail):
        u = dequeue()
        c = min(q, u.t)
        u.t -= c
        elaps += c
        if( u.t > 0):
            enqueue(u)
        else:
            print("{} {}".format(u.name, elaps))

Submission status: Accepted

Solution code:
from collections import deque

n, m = map(int, input().split())
name = ['']*n
time = ['']*n
for i in range(n):
	name[i], time[i] = input().split()
time = list(map(int, time))

Q = deque([i for i in range(n)])
t = 0
while Q!=deque([]):
	q = Q.popleft()
	if time[q]<=m:
		t += time[q]
		print(name[q] + ' ' + str(t))
	else:
		t += m
		time[q] -= m
		Q.append(q)

Submission status: Accepted

Solution code:
from collections import deque
def solve(n,t):
  q=deque([])
  for i in range(n):
    l=list(input().split())
    q.append(l)
  time=0
  while not len(q)==0:
    x=q.popleft()
    if int(x[1])-t>0:
      x[1]=str(int(x[1])-t)
      time+=t
      q.append(x)
    else:
      print(x[0]+" "+str(time+int(x[1])))
      time+=int(x[1])
n,t=map(int,input().split())
solve(n,t)

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
p = deque()
for i in range(n):
    name, t = input().split()
    time = int(t)
    p.append((name, time))
elapsed = 0
while len(p) > 0:
    name, time = p.popleft()
    elapsed += min(q, time)
    if time > q:
        p.append((name, time - q))
    else:
        print(name, elapsed)

Submission status: Accepted

Solution code:

pnum,mtime = map(int,input().split(" "))

total = [list(input().split(" ")) for _ in range(pnum)]

cnt=0
tcnt=0
while len(total) > 0:
	ztime = int(total[0][1]) - mtime
	if ztime <= 0:
		tcnt += int(total[0][1])
		print(total[0][0],int(tcnt))
		total.pop(0)
	else:
		total.append([total[0][0],ztime])
		total.pop(0)
		tcnt += mtime
	cnt += 1

Submission status: Accepted

Solution code:
#queueを使った方法
from collections import deque

pnum,mtime = map(int,input().split(" "))

q = deque([])
[q.append(input().split(" ")) for u in range(pnum)]

tcnt=0
while len(q) > 0:
	qq = q.popleft()
	ztime = int(qq[1]) - mtime
	if ztime <= 0:
		tcnt += int(qq[1])
		print(qq[0],int(tcnt))
	else:
		q.append([qq[0],ztime])
		tcnt += mtime

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == "__main__":
    n, q = input().split()
    n, q = int(n), int(q)
    s = 0
    a = deque([list(map(str,input().split())) for _ in range(n)])

    while len(a):
        x = a.popleft()
        if int(x[1]) - q <= 0:
            s += int(x[1])
            print(x[0], s)
        else:
            x[1] = str(int(x[1]) - q)
            s += q
            a.append(x)

Submission status: Accepted

Solution code:
# Queue

class MyQueue:
    def __init__(self):
        self.queue = [] # list
        self.top = 0
    
    def isEmpty(self):
        if self.top==0:
            return True
        return False
    
    def isFull(self):
        pass

    def enqueue(self, x):
        self.queue.append(x)
        self.top += 1
    
    def dequeue(self):
        if self.top>0:
            self.top -= 1            
            return self.queue.pop(0)
        else:
            print("there aren't enough values in Queue")

def main():
    n, q = map(int, input().split())
    A = [
        list(map(lambda x: int(x) if x.isdigit() else x, input().split()))
        for i in range(n)
    ]
    A = [{'name': name, 'time': time} for name, time in A]
    queue = MyQueue()
    for a in A:
        queue.enqueue(a)
    elapsed = 0
    while not queue.isEmpty():        
        job = queue.dequeue()        
        if job['time']>q:
            elapsed += q
            job['time'] = job['time'] - q
            queue.enqueue(job)
        else:
            elapsed += job['time']
            print(job['name']+ ' ' + str(elapsed))

main()

Submission status: Accepted

Solution code:
def enqueue(x, queue):
    queue.append(x)
    return queue
def dequeue(queue):
    val = queue.pop(0)
    return val, queue

def isEmpty(queue):
    return queue!=[]
        
def work(q, process):
    process[1] = process[1] - q
    return process
    
n, q = map(int, input().split())
process = [input().split() for _ in range(n)]
process = [[one[0], int(one[1])] for one in process]
queue = process.copy()

time = 0
while isEmpty(queue):
    process, queue = dequeue(queue)
    process = work(q, process)
    if process[1]<=0:
        worktime = q+process[1]
        print(process[0]+ ' ' +str(time+worktime))
        time += worktime
    else:
        queue = enqueue(process, queue)
        time += q

Submission status: Accepted

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

queue = []

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

time = 0

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
process = []
for _ in range(n):
    name, time = input().split()
    process.append([name, int(time)])
process.append([])

def reduce(index):
    left_time = process[index][1] - q
    return [process[index][0], left_time]

head = 0
tail = n
quene_len = len(process)
elapsed_time = 0

while head != tail:
    if process[head][1] > q:
        process[tail] = reduce(head)
        elapsed_time += q
        head = (head+1)%quene_len
        tail = (tail+1)%quene_len
    else:
        elapsed_time += process[head][1]
        print(process[head][0], elapsed_time)
        head = (head+1)%quene_len

Submission status: Accepted

Solution code:
from queue import Queue
n,q = map(int,input().split())
names = []
times = []
for _ in range(n):
  data = list(map(str,input().split()))
  names.append(data[0])
  times.append(int(data[1]))

names_queue = Queue()
times_queue = Queue()
for i in range(n):
  names_queue.put(names[i])
  times_queue.put(times[i])
#print("---------")
time = 0
i = 0
ret = {}
while True:
  if times_queue.empty():
    break
  d = times_queue.get()
  n = names_queue.get()
# print("%s %d" %(n,d))
# print(d)
  if d-q <= 0:
    time += d
    ret[n] = time
  else:
    times_queue.put(d-q)
    names_queue.put(n)
    time += q
#print("--------")
for key,value in ret.items():
  print("%s %d" %(key,value))

Submission status: Accepted

Solution code:
from collections import deque

n, q = (int(s) for s in input().split())

que = deque()
for i in range(n):
    name, ts = input().split()
    que.append((name, int(ts)))

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

Submission status: Accepted

Solution code:
class process:
    def __init__(self, name, time):
        self.name = name
        self.time = time

def main():
    n, q = map(int, raw_input().split())
    p = list()
    for i in range(n):
        name, time = raw_input().split()
        p.append(process(name, int(time)))

    s = 0
    while len(p) > 0:
        e = p.pop(0)
        if e.time - q <= 0:
            s += e.time
            print(e.name + " " + str(s))
        else:
            s += q
            e.time -= q
            p.append(e)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,q = map(int,raw_input().split(" "))
task = [raw_input().split(" ") for i in range(n)]
c= 0
while task:
	name,time = task.pop(0)
	e = int(time) - q
	if e > 0:
		c+= q
		task.append([name,e])
	else:
		c+= int(time)
		print name+" "+str(c)

Submission status: Accepted

Solution code:
import sys
n, q = map(int, sys.stdin.readline().split())
processes = []
for i in range(n):
    name, time =sys.stdin.readline().split()
    processes += [[name, int(time)]]

progress = 0

while len(processes):
    new_processes = []
    for target in processes:
        if target[1] > q:
            target[1] -= q
            progress += q
            new_processes += [target]
        else:
            progress += target[1]
            print(target[0], progress)
    processes = new_processes

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,raw_input().split())
que = deque()
for i in xrange(n):
    namei, timei = raw_input().split()
    que.append([namei,int(timei)])

tot = 0
while len(que)>0:                     
    if que[0][1] <= q:
        tot += que[0][1]
        print '%s %d'% (que.popleft()[0],tot)
    else:
        que[0][1] -= q
        tot += q
        que.rotate(-1)

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:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B
# Queue
# Result:
import sys

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

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


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

    def __str__(self):
        """From head to tail"""
        str_val = ''
        for var in self.store:
            str_val += str(var) + '\n'
        return str_val.rstrip()

    def enqueue(self, v):
        self.store.append(v)

    def dequeue(self):
        if self.is_empty():
            return None
        return self.store.pop(0)

    def is_empty(self):
        if len(self.store) == 0:
            return True
        else:
            return False


### main
vals = sys.stdin.readline().rstrip().split(' ')
n, q = map(int, vals)

queue = Queue()
for i in range(0, n):
    pname, ptime = sys.stdin.readline().rstrip().split(' ')
    queue.enqueue(Process(pname, int(ptime)))

time = 0
while not queue.is_empty():
    p = queue.dequeue()
    if p.time > q:
        p.time -= q
        queue.enqueue(p)
        time += q
    else:
        time += p.time
        print '%s %d' % (p.name, time)

Submission status: Accepted

Solution code:
n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
ps = [0]*n
t = [0]*n
for i in range( n ):
	ps[i], t[i] = raw_input( ).split( " " )


output = []
qsum = 0
while t:
	psi = ps.pop( 0 )
	ti = int( t.pop( 0 ) )
	if ti <= q:
		qsum += ti	
		output.append( psi+" "+str( qsum ) )
	else:
		t.append( ti - q )
		ps.append( psi )
		qsum += q

print( "\n".join( output ) )

Submission status: Accepted

Solution code:
n, q = [ int( val ) for val in input( ).split( " " ) ]
ps = [0]*n
t = [0]*n
for i in range( n ):
	ps[i], t[i] = input( ).split( " " )


output = []
qsum = 0
while t:
	psi = ps.pop( 0 )
	ti = int( t.pop( 0 ) )
	if ti <= q:
		qsum += ti	
		output.append( psi+" "+str( qsum ) )
	else:
		t.append( ti - q )
		ps.append( psi )
		qsum += q

print( "\n".join( output ) )

Submission status: Accepted

Solution code:
n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
ps = [0]*n
t = [0]*n
for i in range( n ):
    ps[i], t[i] = raw_input( ).split( " " )
 
 
output = []
qsum = 0
while t:
    psi = ps.pop( 0 )
    ti = int( t.pop( 0 ) )
    if ti <= q:
        qsum += ti
        output.append( "".join( ( psi, " ", str( qsum ) ) ) )
    else:
        t.append( ti - q )
        ps.append( psi )
        qsum += q
 
print( "\n".join( output ) )

Submission status: Accepted

Solution code:
n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
ps = [0]*n
t = [0]*n
for i in range( n ):
    ps[i], t[i] = raw_input( ).split( " " )

qsum = 0
while t:
    psi = ps.pop( 0 )
    ti = int( t.pop( 0 ) )
    if ti <= q:
        qsum += ti
        print( "{:s} {:d}".format( psi, qsum ) )        
    else:
        t.append( ti - q )
        ps.append( psi )
        qsum += q

Submission status: Accepted

Solution code:
from Queue import Queue

n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
names = Queue( )
times = Queue( )
for i in range( n ):
	name, time = raw_input( ).split( " " )
	names.put( name )
	times.put( int( time ) )
 
qsum = 0
output = []
while not times.empty( ):
	name = names.get( )
	time = times.get( )
	if time <= q:
		qsum += time
		output.append( "{:s} {:d}".format( name, qsum ) )
	else:
		times.put( time - q )
		names.put( name )
		qsum += q

print( "\n".join( output ) )

Submission status: Accepted

Solution code:
from Queue import Queue
 
n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
processes = Queue( )
for i in range( n ):
    name, time = raw_input( ).split( " " )
    processes.put( ( name, int( time ) ) )
  
qsum = 0
output = []
while not processes.empty( ):
    process = processes.get( )
    if process[1] <= q:
        qsum += process[1]
        output.append( "{:s} {:d}".format( process[0], qsum ) )
    else:
        processes.put( ( process[0], process[1]- q ) )
        qsum += q
 
print( "\n".join( output ) )

Submission status: Accepted

Solution code:
from queue import Queue
 
n, q = [ int( val ) for val in input( ).split( " " ) ]
processes = Queue( )
for i in range( n ):
    name, time = input( ).split( " " )
    processes.put( ( name, int( time ) ) )
  
qsum = 0
output = []
while not processes.empty( ):
    process = processes.get( )
    if process[1] <= q:
        qsum += process[1]
        output.append( "{:s} {:d}".format( process[0], qsum ) )
    else:
        processes.put( ( process[0], process[1]- q ) )
        qsum += q
 
print( "\n".join( output ) )

Submission status: Accepted

Solution code:
from collections import deque

n, q = [ int( val ) for val in raw_input( ).split( " " ) ]
processes = deque( )
for i in range( n ):
    name, time = raw_input( ).split( " " )
    processes.append( ( name, int( time ) ) )
  
qsum = 0
output = []
while len( processes ):
    process = processes.popleft( )
    if process[1] <= q:
        qsum += process[1]
        output.append( "{:s} {:d}".format( process[0], qsum ) )
    else:
        processes.append( ( process[0], process[1]- q ) )
        qsum += q
 
print( "\n".join( output ) )