In civilized countries k ticket offices are working at the train station, but the queue…
- User
In civilized countries k ticket offices are working at the train station, but the queue to them is just one. The service works as follows. Initially, when all the ticket offices are free, the first k people from the queue go to the offices. The others are waiting their turn. As soon as someone is served and the corresponding office becomes free, the next person in the queue comes to this office. This continues until all the people will be served.
Find the time to serve all the clients.
Input The first line contains two integers: the queue size n and the number of ticket offices k (1≤n≤10^5 ,1≤k≤10 ^4). n positive integers are given in the second line. The i-th number gives the time ti (1≤t i≤10 ^5) to serve the i-th client in the queue.
Output Print one integer — the time to serve all the people in the queue.
Examples Input #1 5 2 3 1 1 2 3 Answer #1 6 Input #2 7 3 1 2 3 4 5 3 1 Answer #2 7 Write code in python
- Assistantpython
import heapq def main(): import sys import threading def solve(): import sys n, k = map(int, sys.stdin.readline().split()) t = list(map(int, sys.stdin.readline().split())) # Initialize min-heap heap = [] finish_times = [] # Assign first min(n, k) clients to offices initial_clients = min(n, k) for i in range(initial_clients): finish_time = t[i] finish_times.append(finish_time) heapq.heappush(heap, finish_time) # Assign remaining clients for i in range(initial_clients, n): # Get the earliest available office (office that finishes earliest) earliest_finish_time = heapq.heappop(heap) # Next client starts service at earliest_finish_time finish_time = earliest_finish_time + t[i] finish_times.append(finish_time) heapq.heappush(heap, finish_time) total_time = max(finish_times) print(total_time) threading.Thread(target=solve).start() if __name__ == "__main__": main()