import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)



def par(a):

    if P[a] < 0: return a

    t = par(P[a])

    P[a] = t

    return t

def unite(a, b):

    if par(a) == par(b): return 0

    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)]

    if P[par(b)] == P[par(a)]:

        P[par(b)] = par(a)

        P[par(a)] -= 1

    elif P[par(b)] > P[par(a)]:

        P[par(b)] = par(a)

    else:

        P[par(a)] = par(b)

N, H, W = list(map(int, input().split()))

P = [-1 for i in range(H+W)]

C = [1] * (H+W)

X = []

for _ in range(N):

    r, c, a = list(map(int, input().split()))

    X.append((a, r-1, c+H-1))

X = sorted(X)

ans = 0

while X:

    a, r, c = X.pop()

    unite(r, c)

    if C[par(r)]:

        ans += a

        C[par(r)] -= 1

print(ans)
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)

N, H, W = list(map(int, input().split()))

P = [-1 for i in range(H+W)]

C = [1] * (H+W)

arr = []

for _ in range(N):

    r, c, a = list(map(int, input().split()))

    arr.append((a, r-1, c+H-1))

arr = sorted(arr)



ans = 0

def find(a):

    if P[a] < 0: return a

    t = find(P[a])

    P[a] = t

    return t



def unite(r, c):

    fr, fc = find(r), find(c)

    if fr == fc:  return 0

    C[fr] = C[fc] = C[fr] + C[fc]

    if P[fr] == P[fc]:

        P[fc] = fr

        P[fr] -= 1

    elif P[fr] > P[fc]:

        P[fc] = fr

    else:

        P[fr] = fc

    

while arr:

    a, r, c = arr.pop()

    unite(r, c)

    fr = find(r)

    if C[fr]:

        ans += a

        C[fr] -= 1

print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def par(a):
    if P[a] < 0:
        return a
    t = par(P[a])
    P[a] = t
    return t


def unite(a, b):
    if par(a) == par(b):
        return 0
    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)]
    if P[par(b)] == P[par(a)]:
        P[par(b)] = par(a)
        P[par(a)] -= 1
    elif P[par(b)] > P[par(a)]:
        P[par(b)] = par(a)
    else:
        P[par(a)] = par(b)


N, H, W = list(map(int, input().split()))
P = [-1 for i in range(H + W)]
C = [1] * (H + W)
X = []
for _ in range(N):
    r, c, a = list(map(int, input().split()))
    X.append((a, r - 1, c + H - 1))
X = sorted(X)
ans = 0
while X:
    a, r, c = X.pop()
    unite(r, c)
    if C[par(r)]:
        ans += a
        C[par(r)] -= 1
print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N, H, W = list(map(int, input().split()))
P = [-1 for i in range(H + W)]
C = [1] * (H + W)
arr = []
for _ in range(N):
    r, c, a = list(map(int, input().split()))
    arr.append((a, r - 1, c + H - 1))
arr = sorted(arr)
ans = 0


def find(a):
    if P[a] < 0:
        return a
    t = find(P[a])
    P[a] = t
    return t


def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:
        return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc


while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

+N, H, W = list(map(int, input().split())) +P = [-1 for i in range(H + W)] +C = [1] * (H + W) +arr = [] +for _ in range(N): +    r, c, a = list(map(int, input().split())) +    arr.append((a, r - 1, c + H - 1)) +arr = sorted(arr) +ans = 0 -def par(a): +def find(a): -    t = par(P[a]) +    t = find(P[a]) -def unite(a, b): -    if par(a) == par(b): +def unite(r, c): +    fr, fc = find(r), find(c) +    if fr == fc: -    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)] -    if P[par(b)] == P[par(a)]: -        P[par(b)] = par(a) -        P[par(a)] -= 1 -    elif P[par(b)] > P[par(a)]: -        P[par(b)] = par(a) +    C[fr] = C[fc] = C[fr] + C[fc] +    if P[fr] == P[fc]: +        P[fc] = fr +        P[fr] -= 1 +    elif P[fr] > P[fc]: +        P[fc] = fr -        P[par(a)] = par(b) +        P[fr] = fc -N, H, W = list(map(int, input().split())) -P = [-1 for i in range(H + W)] -C = [1] * (H + W) -X = [] -for _ in range(N): -    r, c, a = list(map(int, input().split())) -    X.append((a, r - 1, c + H - 1)) -X = sorted(X) -ans = 0 -while X: -    a, r, c = X.pop() +while arr: +    a, r, c = arr.pop() -    if C[par(r)]: +    fr = find(r) +    if C[fr]: -        C[par(r)] -= 1 +        C[fr] -= 1