Serve all customers

2.5

31 votes
Scheduling algorithms, Queues, Basics of Queues, Operating Systems, Data Structures
Problem

There are N people entering a restaurant at different times. These people are numbered from 1 to N by the staffs of the restaurant. There are K chefs working in the restaurant. These chefs are also numbered from 1 to N by the management of the restaurant. A person has to order immediately after he or she enters the restaurant. The arrival time of a person is given in an array A so that the restaurant can track the timings of the customers. Also, the time that a chef takes to prepare the customer's orders is given in an array B.

The management of the restaurant also stores some data in array C. The element of this array, Ci, denotes the increased amount of the satisfaction level of the ith customer if he or she has to wait for 1 unit of time after giving the order.

The restaurant is open until 109 units of time and all the orders must be prepared by this time.

Your task is to minimize the sum of the total satisfaction level of all the customers who come to the restaurant.

Note: Only one chef works to prepare an order of one person. A chef can only prepare one order at a time and a person's order can only be prepared by one chef. Also, if a chef completes preparation of order at time t, then he or she can pick up another order and start preparing it at time t+1.

Input format

  • The first line contains two space-separated integers N and K.
  • The second line contains an array A of N integers where Ai denotes the arrival time of the ith person.
  • The third line contains an array B of N integers where Bi denotes the time it takes for preparing an order of a person i.
  • The fourth line contains an array C of N integers where Ci denotes the satisfaction level of the ith person for each unit of time that he or she has to wait. 

Output Format

Print N space-separated integers(say qi) denoting that the ith person's order is picked up for preparation at time qi by one of the free chefs. If no chef is available at that time, then your program must result in Wrong Answer verdict for that test. 

Refer to the verdict and scoring section for caveats and the sample explanation for better understanding.

Constraints

1KN1041Ai1051Bi,Ci104

Verdict and Scoring

Note that if your output states that a person's order starts getting processing at time t and all the K chefs are busy at time t, then it results in a Wrong Answer verdict. Since the restaurant closes at time t=109, all the orders must be ready by then. If there is any person whose order is still getting preparation or not yet given for preparation, then it results in a Wrong Answer verdict.

The scoring for this problem is relative. The participant with the best answer (in this case minimum satisfactionreceives the highest score and your score is relative to that. 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Here there are 2 chefs available(lets call them chef1 and chef2) at the restaurant and 5 people enter. The best possible way to prepare orders is:

  • Prepare the order of 1st person as soon as he enters. So, without loss of generality, chef1 will pick up their order. Hence, chef1 will only be available to prepare another order at time = 16 units.
  • When the 2nd person arrives, chef2 will be free and starts preparing their order. So, now chef2 will only be available to prepare another order at time = 21 units.
  • When the 3rd person arrives, both chef1 and chef2 are busy. chef1 gets free at time = 16 units. As soon as the chef1 gets free, he starts preparing their order. He will be available at time = 21 units.
  • The 4th person arrives at time 20 units. chef1 and chef2 will be available at time 21 units only. Hence, he will have to wait until then and then his order will start being prepared by chef1 (or even chef2 for that matter). 
  • The last person arrives and the other chef will immediately start preparing his order. Hence, he does not have to wait longer.
Editor Image

?