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:
n,q = map(int,input().split())
p = [input().split() for _ in range(n)]

time = 0
while len(p) > 0:
    left = int(p[0][1]) - q
    if left <= 0:
        time = time + int(p[0][1])
        print(p[0][0],end="")
        print(" ",end="")
        print(time)
        p.pop(0)
    else:
        time = time + q
        p[0][1] = left
        p.append(p[0])
        p.pop(0)

Submission status: Accepted

Solution code:
args = list(map(int, input().split()))
processes = []
for i in range(args[0]):
    processes.append(input().split())
    processes[i][1] = int(processes[i][1])
timer, p = 0, 0
while args[0]:
    p %= args[0]
    if processes[p][1] > args[1]:
        processes[p][1] -= args[1]
        timer += args[1]
    else:
        timer += processes[p][1]
        print(processes[p][0], timer)
        args[0] -= 1
        del processes[p]
        p -= 1
    p += 1

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp
class Process(object):
    def __init__(self, name, time):
        self.name = str(name)
        self.time = int(time)

from collections import deque

n,  q = map(int, input().split())
que = deque([Process(*(input().split())) for i in range(n)])

que_l = len(que)
time = 0
while que_l!=0:
    tmp = que.popleft()
    if tmp.time<=q:
        time += tmp.time
        que_l -= 1
        print(tmp.name, time)
    else:
        time += q
        tmp.time -= q
        que.append(tmp)

Submission status: Accepted

Solution code:
tmp_str = input().split(" ")
n = int(tmp_str[0])
q = int(tmp_str[1])
queue = []
time = 0

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

while queue:
    process_target = queue.pop(0)
    if process_target[1] <= q:
        time += process_target[1]
        print(process_target[0] + " " + str(time))
    else:
        time += q
        queue.append([process_target[0], process_target[1] - q])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
queue = []
for i in range(n):
    name, time = input().split()
    time = int(time)
    queue.append([name, time])
ans = []
cnt = 0
while queue !=  []:
    queue[0][1] = queue[0][1] - q
    if queue[0][1] > 0:
        cnt += q
        queue.append(queue.pop(0))
    else:
        cnt += q + queue[0][1]
        ans.append([queue[0][0], cnt])
        queue.pop(0)
for i in ans:
    print(' '.join(map(str, i)))

Submission status: Accepted

Solution code:
def readInts():
  return [int(i) for i in input().split()]

def readLine(n: int):
  return [input().split() for _ in range(n)]


n, q = readInts()
ps = readLine(n)

t = 0
while ps != []:
    p = ps.pop(0)
    pt = int(p[1])
    if pt <= q:
        t += pt
        print(p[0], t)
    else:
        t += q
        ps.append([p[0], pt - q])

Submission status: Accepted

Solution code:
from collections import deque

class Node():
    def __init__(self, name, cost):
        self.name = name
        self.cost = int(cost)

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

for i in range(n):
    tmp = list(input().split(" "))
    nodes.append(Node(tmp[0],tmp[1]))

time = 0
while not len(nodes) == 0:
    node = nodes.popleft()
    if node.cost > p:
        node.cost -= p
        nodes.append(node)
        time += p
    elif node.cost <= p:
        time += node.cost
        print(node.name + " " + str(time))

Submission status: Accepted

Solution code:
from collections import*
q = deque([])
a,b=map(int,input().split())
for i in range(a):
    c,d=input().split()
    q.append([c,int(d)])
t=0
while len(q)>0:
    tmp=q.popleft()
    if tmp[1]>b:
        tmp[1]-=b
        q.append(tmp)
        t+=b
    else:
        print(tmp[0],tmp[1]+t)
        t+=tmp[1]

Submission status: Accepted

Solution code:
class Queue:
    def __init__(self, size):
        self.data = [None for i in range(size)]
        self.front = 0
        self.tail = 0 
    
    def initialize(self):
        self.front = self.tail = 0
    
    def isempty(self):
        return self.front == self.tail
    
    def isfull(self):
        return (self.tail + 1)%len(self.data) == self.front
    
    def enqueue(self, x):
        if self.isfull():
            raise Exception('overflow')
        else:
            self.data[self.tail] = x
            self.tail = (self.tail + 1)%len(self.data)
    
    def dequeue(self):
        if self.isempty():
            raise Exception('underflow')
        else:
            x = self.data[self.front]
            self.front = (self.front + 1) %len(self.data)
            return x

T = 0
size, time = list(map(int, input().split()))
# print(isinstance(time, int))
queue = Queue(size+2)
for i in range(size):
    ele = tuple(input().split())
    queue.enqueue(ele)

while not queue.isempty():
    task, t = queue.dequeue()
    t = int(t)
    if t <= time:
        T += t
        print(task, T)
    else:
        t -= time
        T += time
        queue.enqueue((task,t))

Submission status: Accepted

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

ans = []
t = 0
while len(l) > 0:
    v = l.pop(0)
    v[1] -= q
    if v[1] <= 0:
        t += q+v[1]
        ans.append([v[0], str(t)])
    else:
        t += q
        l.append(v)

for v in ans:
    print(" ".join(v))

Submission status: Accepted

Solution code:
class P:
    def __init__(self, name, t):
        self.name  = name
        self.t = t

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

LEN = 100005  # 100005は十分大きければなんでも良い(n以上になるように)

head = 0
tail = n
Q = [P('', 0) for _ in range(LEN)]
for i in range(n):
    Q[i].name, num = input().split()
    Q[i].t = int(num)

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

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

elaps = 0

while head != tail:
    u, head = dequeue(Q, head)
    c = min(q, u.t)
    u.t -= c
    elaps += c
    if u.t > 0:
        tail = enqueue(Q, u, tail)
    else:
        print(u.name + ' ' + str(elaps))

Submission status: Accepted

Solution code:
class RoundRobin(object):
    def __init__(self, n, q, names, times):
        self.n = n
        self.q = q
        self.size = 2 * n

        self.queue = [0] * self.size
        self.queue[:n] = [ ( names[i], int(times[i]) ) for i in range(n) ]
        self.head = 0
        self.tail = n

    def enqueue(self, x):
        self.queue[self.tail] = x
        self.tail = ( self.tail + 1 ) % self.size
        return

    def dequeue(self):
        x = self.queue[self.head]
        self.queue[self.head] = 0
        self.head = ( self.head + 1 ) % self.size
        return x

    def execute(self):
        t = 0
        while 1:
            u = self.dequeue()
            if u[1] > self.q:
                t += self.q
                self.enqueue( (u[0], u[1] - self.q ) )
            else:
                t += u[1]
                print(u[0], t)
            if self.head == self.tail:
                break
        return

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

names = [0]*n
times = [0]*n
for i in range(n):
    names[i], times[i] = input().split()

model = RoundRobin(n, q, names, times)
model.execute()

Submission status: Accepted

Solution code:
#qeueu
from collections import deque
n,p = map(int,input().split());cnt = 0
r = deque(input().split() for _ in range(n))
while r:
    name, val = r.popleft()
    val_af = max(int(val)-p, 0)
    if val_af != 0:r.append([name,val_af]);cnt += p
    else:cnt += int(val);print(name, cnt)

Submission status: Accepted

Solution code:
from collections import deque


nq = list(map(int, input().split()))
n = nq[0]
q = nq[1]
t = 0
queue = deque()
for i in range(n):
    p = input().split()
    queue.append(p)
while queue:
    proc = queue.popleft()
    if int(proc[1]) <= q:
        t += int(proc[1])
        print(proc[0], t)
    else:
        proc[1] = int(proc[1]) - q
        t += q
        queue.append(proc)

Submission status: Accepted

Solution code:
# coding: utf-8
class Task:
    def __init__(self, name, time):
        self.name = name
        self.time = time
    
    def getTime(self):
        return self.time
    
    def dispatch(self,qtm):
        self.time -= qtm

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

p = 0
while que:
    t = que.pop(0)
    if t.getTime() > q:
        p += q
        t.dispatch(q)
        que.append(t)
    else:
        p += t.getTime()
        print(t.name + " " + str(p))

Submission status: Accepted

Solution code:
class Queue:
    def __init__(self):
        self.list = []
    
    def enqueue(self, element):
        self.list.append(element)

    def dequeue(self):
        item = self.list[0]
        del self.list[0]
        return item
    
    def size(self):
        return len(self.list)

if __name__ == "__main__":
    queue = Queue()
    n, qtm = input().split()
    n, qtm = int(n), int(qtm)
    for i in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue([name, time])
    
    result = []
    time = 0
    while queue.size() > 0:
        proc = queue.dequeue()
        proc_name = proc[0]
        proc_t = proc[1]
        if proc_t > qtm:
            proc = [proc_name, proc_t - qtm]
            queue.enqueue(proc)
            time += qtm
        else:
            time += proc_t
            print(proc_name, time)

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
deq = deque()
for i in range(n):
    name, time = input().split();
    time = int(time)
    deq.append((time, name))
ans = []
cur = 0
while deq:
    time, name = deq.popleft()
    if time <= q:
        cur += time
        ans.append("%s %d" % (name, cur))
    else:
        cur += q
        deq.append((time - q, name))
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys,collections
p=[s.split()for s in sys.stdin]
p=collections.deque([[s[0],int(s[1])]for s in p])
q=int(p.popleft()[1])
t=0
while len(p)>0:
 if p[0][1]>q:
  p[0][1]-=q
  p+=[p.popleft()]
  t+=q
 else:
  t+=p[0][1]
  print(p.popleft()[0],t)

Submission status: Accepted

Solution code:
condition = input().split(' ')
num_of_values = int(condition[0])
quontum = int(condition[1])
input_lines = []
time_passed = 0

for n in range(num_of_values):
    input_lines.append(input())
    
while len(input_lines) > 0:
# for i in range(8):
    # 配列の1つ目を取り出す
    value = input_lines[0]
    # print(value)
    splitted_value = value.split(' ')
    # 値を記録する
    elem_name = splitted_value[0]
    elem_value = int(splitted_value[1])
    # print(elem_name)
    # print(elem_value)
    # 値を変更する
    calc_value = elem_value - quontum
    # print(calc_value)
    if calc_value > 0:
        time_passed += quontum
        input_lines.remove(value) # 値を削除する
        join_value = [elem_name, str(calc_value)]
        new_value = ' '.join(join_value)
        input_lines.append(new_value) # 値を追加する
    else :
        time_passed += elem_value
        input_lines.remove(value) # 値を削除する
        print(elem_name, time_passed)

Submission status: Accepted

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

for i in range(n):
    name, t = map(str, input().split())
    arr.append([name, t, 0])

for i in range(n):
    arr[i][1] = int(arr[i][1])

time = 0

while arr != []:
    if arr[0][1] <= 0:
        tmp = arr.pop(0)
        print("{} {}".format(tmp[0],time))
    elif arr[0][1] <= q:
        tmp = arr.pop(0)
        time += tmp[1]
        print("{} {}".format(tmp[0],time))
    elif arr[0][1] > q:
        arr[0][1] -= q
        time += q
        last = arr.pop(0)
        arr.append(last)
    else:
        print("その他")

Submission status: Accepted

Solution code:
from collections import deque

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

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

queue = deque([]) 

for i in range(n):
        temp = input().split()
        queue.append(Process(temp[0], int(temp[1])))

totalTime = 0
while len(queue) != 0:
        if queue[0].time > q:
                queue[0].time -= q
                queue.append(queue.popleft())
                totalTime += q
        else:
                totalTime += queue[0].time
                print(queue[0].id, totalTime)
                queue.popleft()

Submission status: Accepted

Solution code:
from collections import deque
BIG_NUM = 2000000000

class Process:
    def __init__(self, arg_name, arg_value):
        self.name = arg_name
        self.value = arg_value
        self.finish_time = BIG_NUM

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

for loop in range(n):
    input_str = list(map(str, input().split()))
    tmp_Process = Process(input_str[0], int(input_str[1]))
    Q.append(tmp_Process)

numFinish = 0
nowTime = 0

while True:
    tmp_Process = Q.popleft()

    if tmp_Process.value <= limit:
        nowTime += tmp_Process.value
        print(tmp_Process.name + ' ' + str(nowTime))
        numFinish += 1
    else:
        tmp_Process.value -= limit
        nowTime += limit
        Q.append(tmp_Process)

    if numFinish == n:
        break

Submission status: Accepted

Solution code:
from collections import deque

N,q = map(int,input().split())
A = [list(map(str,input().split())) for i in range(N)]
A = [[A[i][0],int(A[i][1])] for i in range(N)]

a = deque(A)
time = 0
while len(a) > 0:
    temp = a.popleft()
    if temp[1] - q <= 0:
        time += temp[1]
        print(temp[0],time)
    else:
        time += q
        temp[1] -= q
        a.append(temp)

Submission status: Accepted

Solution code:
class Queue:
    def __init__(self, size=1e6):
        self.max_size = size
        self.data = []
    
    def enqueue(self, x):
        self.data.append(x)
    
    def dequeue(self):
        return self.data.pop(0)
    
    def isEmpty(self):
        return len(self.data) == 0 
    
    def isFull(self):
        return len(self.data) == self.max_size



N, Q = tuple(map(int, input().split()))
queue = Queue()
processes = [(p[0], int(p[1])) for p in [ input().split() for _ in range(N)]]
[queue.enqueue(p) for p in processes]
current_q = 0

while True:
    current_q += Q
    name, time = queue.dequeue()
    time -= Q
    if (time > 0):
        queue.enqueue((name, time))
        continue
    else:
        current_q += time
        print('{} {}'.format(name, current_q))
    if queue.isEmpty(): 
        break

Submission status: Accepted

Solution code:
A=list(map(int,input().split()))
B=[]
q=A[1]
from collections import deque
D=deque()
for i in range(A[0]):
    
    L=list(map(str,input().split()))
    L[1]=int(L[1])
    D.append(L[:])
    
    
time=0
while len(D):
    a=D.popleft()
    if a[1]<=q:
        time+=a[1]
        print(f'{a[0]} {time}')
    else:
        time+=q
        D.append([a[0],a[1]-q])

Submission status: Accepted

Solution code:
N, q_time = map(int, input().split())
b = []

for i in range(N):
    a = input().split()
    b.append(a)

cnt = 0
score = 0
while cnt < N:
    if int(b[cnt][1]) > q_time:
        b[cnt][1] = int(b[cnt][1]) - q_time
        delete = b.pop(cnt)
        b.append(delete)
        score += q_time
        #print(b)
    else:
        score += int(b[cnt][1])
        b[cnt][1] = score
        cnt += 1
        #print(b)

for i in b:
    print(*i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class queue:
  def __init__(self):
    self.my_list = []
  def enqueue(self,x):
    self.my_list.append(x)
    
  def dequeue(self):
    x = self.my_list[0]
    del self.my_list[0]
    return x
  def isEmpty(self):
    if len(self.my_list)==0:
      return True;
    else:
      return False;
    
# input接收一行输入，返回字符串
n,q = map(int,input().split())
my_queue = queue()
#for name,time in input().split():
#  my_queue.enqueue([name,int(time)])

for i in range(n):
  processes = input().split()
  my_queue.enqueue([processes[0],int(processes[1])])
  
cnt_time = 0

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

Submission status: Accepted

Solution code:
n, q = list(map(int, input().rstrip().split()))
ps = []
for i in range(n):
    name, time = input().rstrip().split()
    ps.append({'name': name, 'time': int(time)})

current_time = 0    
while ps:
    p = ps.pop(0)
    name = p['name']
    time = p['time']
    if time > q:
        ps.append({'name': name, 'time': time - q})
        current_time += q
    else:
        current_time += time
        print('{0} {1}'.format(name, current_time))

Submission status: Accepted

Solution code:
from sys import stdin
from queue import Queue

n, q = [int(x) for x in stdin.readline().rstrip().split()]
proc = Queue()
for _ in range(n):
    pin = stdin.readline().rstrip().split()
    proc.put((pin[0], int(pin[1])))

time = 0
while not proc.empty():
    p = proc.get()
    if p[1] > q:
        proc.put((p[0], p[1]-q))
        time += q
    else:
        time += p[1]
        print(p[0], time)

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split()))
ps = []
for i in range(n):
    pi, qi = input().split()
    ps.append([pi, int(qi), 0])
time = 0
num = 0
while num != n:
    if ps[num][1] == 0:
        ps.append([ps[num][0], ps[num][1], ps[num][2]])
        del(ps[num])
    elif ps[num][1] <= q:
        time += ps[num][1]
        ps[num][2] = time
        ps[num][1] = 0
        num += 1
    else:
        time += q
        ps[num][1] -= q
        ps.append([ps[num][0], ps[num][1], ps[num][2]])
        del (ps[num])
ps = sorted(ps, key=lambda x: x[2], reverse=False)
for i in range(n):
    print(ps[i][0], ps[i][2])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
queue
"""
from collections import deque

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

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

cnt = 0
while len(que) != 0:
    cur = que.popleft()
    # もう1回
    if cur[1] - Q > 0:
        cnt += Q
        que.append([cur[0], cur[1] - Q])
    # やりきった
    else:
        cnt += cur[1]
        print(cur[0], cnt)

Submission status: Accepted

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

time = 0
tmp = []
answer = []

while True:
    tmp = data.pop(0)
    if int(tmp[1]) <= q:
        time += int(tmp[1])
        answer.append([tmp[0], time])
        if len(data) == 0:
            break
    else:
        tmp[1] = int(tmp[1]) - q
        time += q
        data.append(tmp)
for i in answer:
    print(i[0], i[1])

Submission status: Accepted

Solution code:
queue = []
head = 0
tail = 0
cnt = 0
MAX = 1000000
def enqueue(x):
    global tail
    global MAX
    queue[tail] = x
    tail += 1
    if tail == MAX:
        tail = 0


def dequeue():
    global head
    global MAX
    x = queue[head]
    head += 1
    if head == MAX:
        head = 0
    return x


queue.extend(range(0,MAX+1,1))
n, q = list(map(int,input().split()))
for i in range(0,n,1):
    idata, value = input().split()
    value = int(value)
    enqueue((idata,value))

while head!=tail:
    #print(queue)
    idata, value = dequeue()
    value -= q
    cnt += min(q,value+q)
    if value<=0:
        print(idata+" "+str(cnt)) 
        continue   
    enqueue((idata,value))

Submission status: Accepted

Solution code:
import sys
from collections import namedtuple, deque

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

queue = deque()

n, q = map(int, sys.stdin.readline().split())
for _ in range(n):
    name, time = sys.stdin.readline().split()
    queue.append(Process(name, int(time)))


def ok():
    pass


current_time = 0

while len(queue):
    process = queue.popleft()
    if process.time <= q:
        current_time += process.time
        print(f'{process.name} {current_time}')
    else:
        current_time += q
        queue.append(Process(process.name, process.time - q))

Submission status: Accepted

Solution code:
from collections import deque


def main():
    queue = deque()
    process = {}
    current_time = 0
    n, q = map(lambda x: int(x), input().split())
    for _ in range(n):
        name, time = input().split()
        process[name] = int(time)
        queue.append(name)
    while True:
        name = queue.popleft()
        process[name] -= q
        if process[name] > 0:
            queue.append(name)
            current_time += q
        else:
            current_time += q + process[name]
            print(name, current_time)
        if len(queue) == 0:
            break


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# In progress
class Queue(object):
    def __init__(self, queue_size):
        self.queue_size = queue_size
        self.Q = [None] * self.queue_size
        self.head = 0
        self.tail = 0

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

    def dequeue(self):
        x = self.Q[self.head]
        self.Q[self.head] = None
        self.head = (self.head + 1) % self.queue_size
        return x


if __name__ == "__main__":
    # Input
    n, q = list(map(int, input().split()))
    pt = list([input().split() for _ in range(n)])
    p = [val[0] for val in pt]
    t = list(map(int, [val[1] for val in pt]))
    # Enqueue all data
    q_size = 1000000  # Assumed max_size
    p_queue = Queue(q_size)
    t_queue = Queue(q_size)
    for i in range(n):
        p_queue.enqueue(p[i])
        t_queue.enqueue(t[i])
    # print(t_queue.Q)
    # Do round-robin
    n_finished = 0
    elapsed_time = 0
    while n_finished < n:
        tmp_t, tmp_p = t_queue.dequeue(), p_queue.dequeue()
        tmp_lefttime = tmp_t - q
        if tmp_lefttime <= 0:
            # 処理が終わった場合
            elapsed_time += tmp_t
            print(tmp_p, elapsed_time)
            n_finished += 1
        else:
            # 処理が残っている場合
            elapsed_time += q
            t_queue.enqueue(tmp_lefttime)
            p_queue.enqueue(tmp_p)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_B&lang=jp
#  Queue : python3
#  2018.11.26 yonezawa

from collections import deque
import sys
input = sys.stdin.readline

def calc(t,q):
    tq = deque()
    counter = 0
    while len(q) > 0:
#        print ("count",counter,q)
        (i1,i2) = q.popleft()
        if (i2 > t):
            i2 = i2 - t
            q.append((i1,i2)) 
            counter = counter + t
        else:
            i2 = counter + i2
            tq.append((i1,i2))
            counter = i2
    return tq
def main():
    (n,t) = map(int,input().split())
    q = deque()
    for i in range(n):
        (a,b) = map(str,input().split())
        q.append((a,int(b)))
    for a,b in calc(t,q):
        print (a,b)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

deq = deque()
for i in range(n):
    name, time = input().split(); time = int(time)
    deq.append((time, name))
    # deque([(150, 'p1'), (80, 'p2'), (200, 'p3'), (350, 'p4'), (20, 'p5')])
ans = []
cur = 0
while deq:
    time, name = deq.popleft()
    if time <= q:
        cur += time
        ans.append("%s %d" % (name, cur))
    else:
        cur += q
        deq.append((time-q, name))
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/3/ALDS1_3_B


from queue import Queue


def solve():
    q = Queue()
    n, time = map(int, input().split())
    for _ in range(n):
        row = list(map(str, input().split()))
        q.put([row[0], int(row[1])])

    elapsed = 0
    while len(q.queue) > 0:
        elem = q.get()

        if elem[1] <= time:
            elapsed += elem[1]
            print(elem[0], elapsed)
        else:
            elapsed += time
            elem[1] -= time
            q.put([elem[0], elem[1]])


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import collections
q = collections.deque()
N,Q = map(int,input().strip().split(" "))
for _ in range(N):
    n,p = input().strip().split(" ")
    q.append([n,int(p)])
t = 0
while len(q) > 0:
    processing = q.popleft()
    lstt = processing.pop()
    if lstt <= Q:
        t += lstt
        name = processing.pop()
        print(name,t)
    else:
        t += Q
        lstt -= Q
        processing.append(lstt)
        q.append(processing)

Submission status: Accepted

Solution code:
from collections import deque

# input
condition=list(map(int, input().split()))
queue=deque([input().split() for i in range(condition[0])])
timer=0

# process
while len(queue) > 0:
    task=queue.popleft()
    name=task[0]
    cost=int(task[1])
    if cost <= condition[1]:
        timer+=cost
        print(name + ' ' + str(timer))
    else:
        timer+=condition[1]
        queue.append([name, cost-condition[1]])

Submission status: Accepted

Solution code:
n, qtime = map(int, input().split(" "))
Name, Time,  = [], []
for i in range(n):
    name, time = map(str, input().split(" "))
    time = int(time)
    Name.append(name)
    Time.append(time)

cu_time = 0
while len(Time) > 0:
    #print(Name, Time)
    name, time = Name.pop(0), Time.pop(0)
    if time <= qtime:
        cu_time += time
        print(name, cu_time)
    else:
        time -= qtime
        cu_time += qtime
        Name.append(name)
        Time.append(time)

Submission status: Accepted

Solution code:
from collections import deque

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

p = deque([])

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

time = 0

while len(p) > 0:
    top = p.popleft()
    time_use = min(q,top[1])
    top[1] -= time_use
    time += time_use
    if top[1] == 0:
        print(top[0], time)
    else:
        p.append(top)

Submission status: Accepted

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

plist = []
tlist = []

for _ in range(n):
    p, t = map(str, input().split())
    plist.append(p)
    tlist.append(int(t))

time = 0

ans_p = []
ans_t = []

while(1):
    if plist == []:
        break

    p = plist[0]
    t = tlist[0]
    plist.pop(0)
    tlist.pop(0)

    if t <= q:
        time += t
        ans_p.append(p)
        ans_t.append(time)
    else:
        time += q
        t -= q
        plist.append(p)
        tlist.append(t)

for p, t in zip(ans_p, ans_t):
    print(p, t)

Submission status: Accepted

Solution code:
n=[]
t=[]
nq=list(map(int,input().split()))
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	t.append(inp[0])
cnt1=0
while len(n)!=0:	
	n[0]=n[0]-nq[1]
	if n[0]<=0:
		cnt1=cnt1+nq[1]+n[0]
		print(t[0],cnt1)
		n.pop(0)
		t.pop(0)	
		
	else:
		n.append(n[0])
		t.append(t[0])
		n.pop(0)
		t.pop(0)
		cnt1+=nq[1]

Submission status: Accepted

Solution code:
from collections import deque
n, time = list(map(int, input().split()))
que = deque()
spend = 0
for _ in range(n):
    process, p_time = input().split()
    que.append([process, int(p_time)])

while len(que) > 0:
    top = que.popleft()
    spend += min(top[1], time)
    top[1] = max(0, top[1]-time)
    if top[1] > 0:
        que.append(top)
    else:
        print(f'{top[0]} {spend}')

Submission status: Accepted

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

time=0
while(len(data)!=0):
    if(data[0][1] <=q):
        time+=data[0][1]
        print(f"{data[0][0]} {time}")
        data.pop(0)
    else:
        time+=q
        data[0][1]-=q
        data.append(data.pop(0))

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
n += 1
list_queue = []
def initialize(head,tail):
    head = 0
    tail = 0
    return (head,tail)
def enqueue(Q,MAX,head,tail,x):
    Q[tail] = x
    if tail + 1 == MAX:
        tail = 0
    else:
        tail += 1
    return (head, tail)
def dequeue(Q,MAX,head,tail):
    x = Q[head]
    if head + 1 == MAX:
        head = 0
    else:
        head += 1
    return (x,head,tail)

head = 0
tail = 0
list_queue = [0] * n
for i in range(0,n-1):
    head, tail = enqueue(list_queue,n,head,tail,list((map(str, input().split()))))
ans = 0

list_zero = []
for i in range(0,n):
    list_zero.append(0)
#while True:
while (head != tail):
    x,head,tail = dequeue(list_queue,n,head,tail)
    if int(x[1]) <= q:
        ans += int(x[1])
        x[1] = 0
        print(x[0], ans)
    else:
        ans += q
        x[1] = int(x[1]) - q
        head, tail = enqueue(list_queue,n,head,tail,x)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
tasknums=[]
times=[]
finish_task=[]
finish_time=[]
take=0
for i in range(0,n):
    a,b=input().split()
    tasknums.append(a)
    times.append(int(b))
while tasknums:
    tasknum=tasknums.pop(0)
    time=times.pop(0)
    if time>q:
        time-=q
        tasknums.append(tasknum)
        times.append(time)
        take+=q
    else:
        finish_task.append(tasknum)
        finish_time.append(take+time)
        take+=time
for i in range(0,n):
    print(finish_task[i]+" "+str(finish_time[i]))