#include using namespace std; using ll = long long; using pll = pair; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; void solve(){ int n, m; cin >> n >> m; vector p(n); for(int i=0; i> p[i]; vector> T(n); for(int i=0; i> u >> v; u--; v--; T[u].insert(v); T[v].insert(u); } vector used(n, false); set que; set> st; for(int i=0; i> c; c--; used[c] = true; que.insert(c); st.erase({p[c], c}); } ll ans = 0LL; for(int t=1; ; t++){ if(st.empty()) break; auto [v, i] = *st.rbegin(); ans += v; used[i] = true; que.erase(i); st.erase({v, i}); int to = *T[i].begin(); if(!used[to]){ T[to].erase(i); if((int)T[to].size() == 1) st.insert({p[to], to}); } set nxt; for(auto v : que){ for(int u : T[v]){ if(used[u]) continue; used[u] = true; nxt.insert(u); st.erase({p[u], u}); } } que = nxt; } cout << ans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }