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) pos = {val: idx for idx, val in enumerate(sorted_w)} global_min = sorted_w[0] visited = [False] * n total = 0 for i in range(n): if not visited[i]: j = i sum_s = 0 k = 0 s_min = float('inf') while not visited[j]: visited[j] = True sum_s += w[j] k += 1 if w[j] < s_min: s_min = w[j] j = pos[w[j]] if k > 1: option1 = sum_s + (k - 2) * s_min option2 = sum_s + k * global_min total += min(option1, option2) print(total)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.