#include<cstdio> #include<cctype> #include<vector> #include<algorithm> using namespace std; int n,m; long long ans; vector<pair<int,int> >edge[1000005]; vector<pair<int,int> >e; bool used[1000005]; inline int read(){ char ch=getchar();int k=0; while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){k=(k<<1)+(k<<3)+ch-‘0‘;ch=getchar();} return k; } bool cmp(pair<int,int>p1,pair<int ,int>p2){return p1.second>=p2.second;} void prim(){ used[1]=1; for(int i=0;i<edge[1].size();i++) e.push_back(edge[1][i]); make_heap(e.begin(),e.end(),cmp); for(int k=1;k<n;k++){ while(used[e[0].first]){ pop_heap(e.begin(),e.end(),cmp); e.pop_back(); } pair<int,int>minn=e[0]; ans+=minn.second; pop_heap(e.begin(),e.end(),cmp); e.pop_back(); used[minn.first]=1; for(int i=0;i<edge[minn.first].size();i++) if(!used[edge[minn.first][i].first]) {e.push_back(edge[minn.first][i]);push_heap(e.begin(),e.end(),cmp);} } } int main(){ n=read();m=read(); for(int t=1;t<=m;t++){ int u=read(),v=read(),w=read(); edge[u].push_back(make_pair(v,w)); edge[v].push_back(make_pair(u,w)); } prim(); printf("%d\n",ans); return 0; }