以下是使用 Python 实现的解题代码: ```python import heapq def max_sum(n, m, queues): max_heap = [] for i in range(n): # 将队列头部和尾部的元素及其所属队列的索引加入最大堆 if queues[i]: heapq.heappush(max_heap, (-queues[i][0], i, 0)) if len(queues[i]) > 1: heapq.heappush(max_heap, (-queues[i][-1], i, -1)) total_sum = 0 for _ in range(m): # 取出最大堆的最大值 val, queue_index, pos = heapq.heappop(max_heap) total_sum += -val # 根据位置更新队列 if pos == 0: queues[queue_index].pop(0) if queues[queue_index]: heapq.heappush(max_heap, (-queues[queue_index][0], queue_index, 0)) else: queues[queue_index].pop() if queues[queue_index]: heapq.heappush(max_heap, (-queues[queue_index][-1], queue_index, -1)) return total_sum n, m = map(int, input().split()) queues = [] for _ in range(n): line = list(map(int, input().split())) queues.append(line[1:]) print(max_sum(n, m, queues)) ``` [2024-10-05 19:55:52 | AI写代码神器 | 345点数解答]