


from collections import deque






class       _UpperCamelCase    :

				"""simple docstring"""







				def __init__(						self				,   a__				,   a__				,   a__   )	->				Any:
					A								=				process_name  # process name
					A								=				arrival_time  # arrival time of the process
					# completion time of finished process or last interrupted time
					A								=				arrival_time
					A								=				burst_time  # remaining burst time
					A								=				0  # total time of the process wait in ready queue
					A								=				0  # time from arrival time to completion time






class       _UpperCamelCase    :

				"""simple docstring"""







				def __init__(						self				,   a__				,   a__				,   a__				,   a__				,   )	->				List[str]:
					# total number of mlfq's queues
					A								=				number_of_queues
					# time slice of queues that round robin algorithm applied
					A								=				time_slices
					# unfinished process is in this ready_queue
					A								=				queue
					# current time
					A								=				current_time
					# finished process is in this sequence queue
					A								=				deque()

				def      _UpperCAmelCase					(						self   )	->				Dict:
					A								=				[]
					for i in range(len(self.finish_queue   )   ):
						sequence.append(self.finish_queue[i].process_name   )
					return sequence

				def      _UpperCAmelCase					(						self				,   a__   )	->				List[str]:
					A								=				[]
					for i in range(len(__snake_case   )   ):
						waiting_times.append(queue[i].waiting_time   )
					return waiting_times

				def      _UpperCAmelCase					(						self				,   a__   )	->				Optional[Any]:
					A								=				[]
					for i in range(len(__snake_case   )   ):
						turnaround_times.append(queue[i].turnaround_time   )
					return turnaround_times

				def      _UpperCAmelCase					(						self				,   a__   )	->				Tuple:
					A								=				[]
					for i in range(len(__snake_case   )   ):
						completion_times.append(queue[i].stop_time   )
					return completion_times

				def      _UpperCAmelCase					(						self				,   a__   )	->				Optional[Any]:
					return [q.burst_time for q in queue]

				def      _UpperCAmelCase					(						self				,   a__   )	->				Any:
					process.waiting_time += self.current_time - process.stop_time
					return process.waiting_time

				def      _UpperCAmelCase					(						self				,   a__   )	->				Union[str, Any]:
					A								=				deque()  # sequence deque of finished process
					while len(__snake_case   ) != 0:
						A								=				ready_queue.popleft()  # current process

						# if process's arrival time is later than current time, update current time
						if self.current_time < cp.arrival_time:
							self.current_time += cp.arrival_time

						# update waiting time of current process
						self.update_waiting_time(__snake_case   )
						# update current time
						self.current_time += cp.burst_time
						# finish the process and set the process's burst-time 0
						A								=				0
						# set the process's turnaround time because it is finished
						A								=				self.current_time - cp.arrival_time
						# set the completion time
						A								=				self.current_time
						# add the process to queue that has finished queue
						finished.append(__snake_case   )

					self.finish_queue.extend(__snake_case   )  # add finished process to finish queue
					# FCFS will finish all remaining processes
					return finished

				def      _UpperCAmelCase					(						self				,   a__				,   a__   )	->				int:
					A								=				deque()  # sequence deque of terminated process
					# just for 1 cycle and unfinished processes will go back to queue
					for _ in range(len(__snake_case   )   ):
						A								=				ready_queue.popleft()  # current process

						# if process's arrival time is later than current time, update current time
						if self.current_time < cp.arrival_time:
							self.current_time += cp.arrival_time

						# update waiting time of unfinished processes
						self.update_waiting_time(__snake_case   )
						# if the burst time of process is bigger than time-slice
						if cp.burst_time > time_slice:
							# use CPU for only time-slice
							self.current_time += time_slice
							# update remaining burst time
							cp.burst_time -= time_slice
							# update end point time
							A								=				self.current_time
							# locate the process behind the queue because it is not finished
							ready_queue.append(__snake_case   )
						else:
							# use CPU for remaining burst time
							self.current_time += cp.burst_time
							# set burst time 0 because the process is finished
							A								=				0
							# set the finish time
							A								=				self.current_time
							# update the process' turnaround time because it is finished
							A								=				self.current_time - cp.arrival_time
							# add the process to queue that has finished queue
							finished.append(__snake_case   )

					self.finish_queue.extend(__snake_case   )  # add finished process to finish queue
					# return finished processes queue and remaining processes queue
					return finished, ready_queue

				def      _UpperCAmelCase					(						self   )	->				List[str]:

					#  all queues except last one have round_robin algorithm
					for i in range(self.number_of_queues - 1   ):
						A								=				self.round_robin(
						    self.ready_queue				,   self.time_slices[i]   )
					#  the last queue has first_come_first_served algorithm
					self.first_come_first_served(self.ready_queue   )

					return self.finish_queue


if __name__ == "__main__":
			import doctest

			_lowercase       :      Any	=	Process("P1", 0, 53)
			_lowercase       :      Dict	=	Process("P2", 0, 17)
			_lowercase       :      Tuple	=	Process("P3", 0, 68)
			_lowercase       :      Optional[int]	=	Process("P4", 0, 24)
			_lowercase       :      Optional[int]	=	3
			_lowercase       :      int	=	[17, 25]
			_lowercase       :      Optional[Any]	=	deque([Pa, Pa, Pa, Pa])

			if len(time_slices) != number_of_queues - 1:
						raise SystemExit(0)

			doctest.testmod(extraglobs={"queue": deque([Pa, Pa, Pa, Pa])})

			_lowercase       :      str	=	Process("P1", 0, 53)
			_lowercase       :      Tuple	=	Process("P2", 0, 17)
			_lowercase       :      int	=	Process("P3", 0, 68)
			_lowercase       :      Any	=	Process("P4", 0, 24)
			_lowercase       :      Tuple	=	3
			_lowercase       :      List[Any]	=	[17, 25]
			_lowercase       :      int	=	deque([Pa, Pa, Pa, Pa])
			_lowercase       :      int	=	MLFQ(number_of_queues, time_slices, queue, 0)
			_lowercase       :      str	=	mlfq.multi_level_feedback_queue()

			# print total waiting times of processes(P1, P2, P3, P4)
			print(
			    f'''waiting time:\
        \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}'''
			)
			# print completion times of processes(P1, P2, P3, P4)
			print(
			    f'''completion time:\
        \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}'''
			)
			# print total turnaround times of processes(P1, P2, P3, P4)
			print(
			    f'''turnaround time:\
        \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}'''
			)
			# print sequence of finished processes
			print(
			    f'''sequence of finished processes:\
        {mlfq.calculate_sequence_of_finish_queue()}'''
			)







import sys
from collections import defaultdict






class       _UpperCamelCase    :

				"""simple docstring"""







				def __init__(						self   )	->				Any:
					A								=				[]

				def      _UpperCAmelCase					(						self				,   a__   )	->				List[str]:
					return self.node_position[vertex]

				def      _UpperCAmelCase					(						self				,   a__				,   a__   )	->				Optional[int]:
					A								=				pos

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__   )	->				Any:
					if start > size // 2 - 1:
						return
					else:
						if 2 * start + 2 >= size:
							A								=				2 * start + 1
						else:
							if heap[2 * start + 1] < heap[2 * start + 2]:
								A								=				2 * start + 1
							else:
								A								=				2 * start + 2
						if heap[smallest_child] < heap[start]:
							A					,			A								=				heap[smallest_child], positions[smallest_child]
							A					,			A								=				(
							    heap[start],
							    positions[start],
							)
							A					,			A								=				temp, tempa

							A								=				self.get_position(positions[smallest_child]   )
							self.set_position(
							    positions[smallest_child]				,   self.get_position(positions[start]   )   )
							self.set_position(positions[start]				,   a__   )

							self.top_to_bottom(a__				,   a__				,   a__				,   a__   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__   )	->				Dict:
					A								=				position[index]

					while index != 0:
						A								=				int((index - 2) / 2   ) if index % 2 == 0 else int((index - 1) / 2   )

						if val < heap[parent]:
							A								=				heap[parent]
							A								=				position[parent]
							self.set_position(position[parent]				,   a__   )
						else:
							A								=				val
							A								=				temp
							self.set_position(a__				,   a__   )
							break
						A								=				parent
					else:
						A								=				val
						A								=				temp
						self.set_position(a__				,   0   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__   )	->				Optional[int]:
					A								=				len(a__   ) // 2 - 1
					for i in range(a__				,   -1				,   -1   ):
						self.top_to_bottom(a__				,   a__				,   len(a__   )				,   a__   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__   )	->				Any:
					A								=				positions[0]
					A								=				sys.maxsize
					self.top_to_bottom(a__				,   0				,   len(a__   )				,   a__   )
					return temp





def   _lowerCAmelCase				(   UpperCamelCase__:     List[str]						) ->   List[str]:





	"""simple docstring"""







	A								=				Heap()

	A								=				[0] * len(UpperCamelCase__						)
	A								=				[-1] * len(UpperCamelCase__						)  # Neighboring Tree Vertex of selected vertex
	# Minimum Distance of explored vertex with neighboring vertex of partial tree
	# formed in graph
	A								=				[]  # Heap of Distance of vertices from their neighboring vertex
	A								=				[]

	for vertex in range(len(UpperCamelCase__						)						):
		distance_tv.append(sys.maxsize						)
		positions.append(UpperCamelCase__						)
		heap.node_position.append(UpperCamelCase__						)

	A								=				[]
	A								=				1
	A								=				sys.maxsize
	for neighbor, distance in adjacency_list[0]:
		A								=				0
		A								=				distance
	heap.heapify(UpperCamelCase__							,					UpperCamelCase__						)

	for _ in range(1							,					len(UpperCamelCase__						)						):
		A								=				heap.delete_minimum(UpperCamelCase__							,					UpperCamelCase__						)
		if visited[vertex] == 0:
			tree_edges.append((nbr_tv[vertex], vertex)						)
			A								=				1
			for neighbor, distance in adjacency_list[vertex]:
				if (
				    visited[neighbor] == 0
				    and distance < distance_tv[heap.get_position(UpperCamelCase__						)]
				):
					A								=				distance
					heap.bottom_to_top(
					    UpperCamelCase__							,					heap.get_position(UpperCamelCase__						)							,					UpperCamelCase__							,					UpperCamelCase__						)
					A								=				vertex
	return tree_edges


if __name__ == "__main__":  # pragma: no cover
				# < --------- Prims Algorithm --------- >
				_lowercase       :      Tuple	=	int(input("Enter number of edges: ").strip())
				_lowercase       :      int	=	defaultdict(list)
				for _ in range(edges_number):
								_lowercase       :      Optional[Any]	=	[int(x) for x in input().strip().split()]
								adjacency_list[edge[0]].append([edge[1], edge[2]])
								adjacency_list[edge[1]].append([edge[0], edge[2]])
				print(prisms_algorithm(adjacency_list))



