#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> vIsland(100);
bool Cmp(vector<int> a, vector<int> b) { return a[2] < b[2]; }
int FindParent(int idx)
{
if (vIsland[idx] != idx)
vIsland[idx] = FindParent(vIsland[idx]);
return vIsland[idx];
}
int solution(int n, vector<vector<int>> costs)
{
int answer = 0;
sort(costs.begin(), costs.end(), Cmp);
for (int i = 0; i < n; i++)
vIsland[i] = i;
for (int i = 0; i < costs.size(); i++)
{
int a = FindParent(costs[i][0]), b = FindParent(costs[i][1]), cost = costs[i][2];
if (a != b)
{
answer += cost;
vIsland[b] = a;
}
}
return answer;
}