You are given $n$ integers $wi (i = 0, 1, ..., n-1)$ to be sorted in ascending order.…
- User
You are given integers to be sorted in ascending order. You can swap two integers and . Each swap operation has a cost, which is the sum of the two integers . You can perform the operations any number of times.
Assistant
pythonn = int(input()) w = list(map(int, input().split())) sorted_w = sorted(w) g = sorted_w[0] pos = {v: i for i, v in enumerate(sorted_w)} visited = [False] * n total_cost = 0 for i in range(n): if not visited[i]: cycle = [] j = i while not visited[j]: visited[j] = True cycle.append(w[j]) j = pos[w[j]] k = len(cycle) if k == 1: continue sum_c = sum(cycle) m = min(cycle) if m == g: total_cost += sum_c + m * (k - 2) else: cost1 = sum_c + m * (k - 2) cost2 = sum_c + g * k total_cost += min(cost1, cost2) print(total_cost)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.