This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5;
vector<int> S;
vector<int> g[nmax];
int occ[nmax];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
S.resize(n);
for(auto &x : S) cin >> x;
vector<int> dir_ord(n), reidx(n + 1);
iota(all(dir_ord), 0);
sort(all(dir_ord), [&](int a, int b) { return S[a] < S[b]; });
sort(all(S));
for(int i = 0; i < n; i++)
reidx[dir_ord[i] + 1] = i;
//for(auto x : reidx) cerr << x << ' '; cerr << '\n';
vector<pii> edg;
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[reidx[a]].emplace_back(reidx[b]);
g[reidx[b]].emplace_back(reidx[a]);
edg.emplace_back(reidx[a], reidx[b]);
}
for(int i = 0; i < n; i++)
sort(all(g[i]), greater<int>());
occ[0] = 1;
priority_queue<int, vector<int>, greater<int>> heap;
heap.emplace(0);
vector<pii> ntedg;
//exit(0);
while(!heap.empty()) {
int node = heap.top();
heap.pop();
//cerr << node << '\n';
while(!g[node].empty() && occ[g[node].back()] == 1) g[node].pop_back();
if(g[node].empty()) continue;
ntedg.emplace_back(node, g[node].back());
heap.emplace(g[node].back());
occ[g[node].back()] = 1;
g[node].pop_back();
if(!g[node].empty())
heap.emplace(node);
}
//exit(0);
ll sum = 0;
for(auto [a, b] : ntedg)
sum += S[a] + S[b];
//exit(0);
cout << sum - accumulate(all(S), 0LL) + (*max_element(all(S))) << '\n';
}
/**
Anul asta nu se da centroid
-- Rugaciunile mele
*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |