#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; int n,m,jdg[5001],cnt,ans; struct uio{ int from,to,que; }road[200001]; int find(int x) { if(x==jdg[x]) return x; return jdg[x]=find(jdg[x]); } bool cmp(uio x,uio y) { return x.que<y.que; } int main() { cin>>n>>m; for(int i=1;i<=n;i++) jdg[i]=i; for(int i=1;i<=m;i++) cin>>road[i].from>>road[i].to>>road[i].que; sort(road+1,road+1+m,cmp); for(int i=1;i<=m;i++) { int x=find(road[i].from); int y=find(road[i].to); if(x!=y) { ans+=road[i].que; cnt++; jdg[x]=y; } if(cnt==n-1) break; } cout<<ans<<endl; return 0; }