# Kyoto University Programming Contest 2017: E - Treasure Hunt

,

これ好き。

## implementation

#include <climits>
#include <cstdio>
#include <functional>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }

int main() {
// input
int n, m; scanf("%d%d", &n, &m);
vector<ll> v(n); repeat (i, n) scanf("%lld", &v[i]);
vector<vector<int> > g(n);
repeat (i, m) {
int x, y; scanf("%d%d", &x, &y); -- x; -- y;
g[x].push_back(y);
g[y].push_back(x);
}

// solve
vector<bool> used(n);
ll acc, ignored; int vertex_count, degree_count;
function<void (int)> go = [&](int i) {
used[i] = true;
acc += v[i];
setmin(ignored, v[i]);
vertex_count += 1;
degree_count += g[i].size();
for (int j : g[i]) if (not used[j]) {
go(j);
}
};
ll result = 0;
repeat (i, n) if (not used[i]) {
acc = 0;
ignored = LLONG_MAX;
vertex_count = 0;
degree_count = 0;
go(i);
if (degree_count == 0) continue;
result += acc;
if (vertex_count > degree_count / 2) result -= ignored;
}

// output
printf("%lld\n", result);
return 0;
}