사용자 도구

사이트 도구


ps:problems:boj:2322

아령

ps
링크acmicpc.net/…
출처BOJ
문제 번호2322
문제명아령
레벨플래티넘 1
분류

그리디

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python
제출기록30864KB / 72ms
최고기록72ms
해결날짜2022/03/02

풀이

코드

"""Solution code for "BOJ 2322. 아령".

- Problem link: https://www.acmicpc.net/problem/2322
- Solution link: http://www.teferi.net/ps/problems/boj/2322

Tags: [Greedy]
"""


def main():
    n = int(input())
    w = [int(x) for x in input().split()]

    min_w = min(w)
    orders = sorted(range(n), key=w.__getitem__)
    is_visited = [False] * n
    answer = 0

    for i in range(n):
        if is_visited[i]:
            continue
        weights = []
        cur = i
        while not is_visited[cur]:
            is_visited[cur] = True
            weights.append(w[cur])
            cur = orders[cur]
        answer += min(
            sum(weights) + min(weights) * (len(weights) - 2),
            sum(weights) + min(weights) + min_w * (len(weights) + 1))
    print(answer)


if __name__ == '__main__':
    main()

토론

댓글을 입력하세요:
K S A V D
 
ps/problems/boj/2322.txt · 마지막으로 수정됨: 2022/03/04 16:28 저자 teferi