Problem: Notes

Template in C

Constraints

* 1 ≤ n ≤ 100000
* 1 ≤ q ≤ 1000
* 1 ≤ timei ≤ 50000
* 1 ≤ length of namei ≤ 10
* 1 ≤ Sum of timei ≤ 1000000

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.

Example

Input

5 100
p1 150
p2 80
p3 200
p4 350
p5 20


Output

p2 180
p5 400
p1 450
p3 550
p4 800

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{




	public static void main(String[] args){

		String array[];

		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

		try{
		String str=reader.readLine();
		array=str.split(" ");
		int num=Integer.parseInt(array[0]);
		int q=Integer.parseInt(array[1]);
		int sum=0;
		Queue queue=new Queue(num);

		String x;
		int y;



		for(int i=0;i<num;i++){
			str=reader.readLine();
			queue.enqueue(str);
		}

		while(queue.num!=0){
			str=queue.dequeue();
			 array=str.split(" ");
			    x=array[0];
				y=Integer.parseInt(array[1]);

			    if(y>q){
			    	y=y-q;
			    	sum+=q;
			    	queue.enqueue(x+" "+y);
			    	} else{

			    	sum+=y;
			          System.out.println(x+" "+sum);
			    }
		}






		}catch(IOException e){

		}

	}//main
}

class Queue{
	int max;
	int num;
	String[] array;
	int front, rear;

	Queue(int capacity){//?????????????????????
		max=capacity;
		num=rear=front=0;
		array=new String[max];
	}
	public void enqueue(String str){
		array[rear++]=str;
		num++;
		if(rear==max)rear=0;
	}

	public String dequeue(){
		String str=array[front++];
		num--;
		if(front==max)front=0;

		return str;
	}

}//stack?????????