#include <bits/stdc++.h>
#define int long long
using namespace std;
struct edge {
int u, v;
};
signed main() {
int n, m; cin >> n >> m;
vector<int> a(1+n); for (int i = 1; i <= n; ++i) cin >> a[i];
vector<edge> el(m); for (auto &[u, v] : el) cin >> u >> v;
vector<int> cvr(1+n, -1);
vector<int> cst, cmc{0};
auto wgt = [&](edge x) {
int wgt0 = (!~cvr[x.u])*a[x.u] + (!~cvr[x.v])*a[x.v];
return wgt0 ? wgt0 : 232323232323;
};
for (int i = 0; i < m; ++i) {
edge x = *min_element(el.begin(), el.end(), [&](const edge &y, const edge &z){
return wgt(y) < wgt(z);
});
if (!~cvr[x.u] && !~cvr[x.v]) {
int p = x.u, q = x.v;
if (a[p] > a[q]) swap(p, q);
cmc.push_back(cvr[p] = 0);
cmc.push_back(cvr[q] = 2);
cst.push_back(a[p]); cst.push_back(a[q]);
}
else if (!~cvr[x.u]) cmc.push_back(cvr[x.u] = 1), cst.push_back(a[x.u]);
else if (!~cvr[x.v]) cmc.push_back(cvr[x.v] = 1), cst.push_back(a[x.v]);
}
int z = cst.size();
for (int i = 1; i < z; ++i) cst[i] += cst[i-1];
for (int i = 1; i < z; ++i) cmc[i] += cmc[i-1];
int q; cin >> q; while (q--) {
int d; cin >> d;
int x = upper_bound(cst.begin(), cst.end(), d) - cst.begin();
cout << cmc[x] << '\n';
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |