성우리뷰

친구비)python

두원공대88학번뚜뚜 2020. 7. 12. 21:32

import sys

input= sys.stdin.readline

 


def find(target):
    if network[target]==target:
        return target

    network[target]=find(network[target])
    return network[target]

def union(a,b):
    findA= find(a)
    findB= find(b)

    if findA==findB:
        return

    if findA<findB:
        network[findB]=findA
    else:
        network[findA]=findB

N, M, K=map(int, input().split())
costs=[0] + list(map(int, input().split()))
#첫 항에 0을 넣고, 다음으로 숫자를 집어넣는 리스트

network=list(range(N+1))
#n+1 크기의 리스트 생성

for _ in range(M):      # for _ in range() : 인덱스 무시. 즉 걍 m번 실행
    a, b= map(int, input().split())

    union(a,b)

for i in range(1, N+1):
    find(i)

INF=2147483647
cheapList=[INF]*(N+1)  # (N+1)개짜리, 요소가 전부 INF인 배열 생성

for i in range(1, N+1):
    cheapList[network[i]]= min(cheapList[network[i]], costs[i])
#i를 1~N+1 범위로 놓고, CHEAPLIST의 NETWORK[i]번째 요소를, network[i]와 costs[i]중
#작은 거로

friendFee=0

for fee in cheapList:
    if fee!=INF:
        friendFee += fee

if friendFee<=K:
    print(friendFee)
else:
    print("Oh no")