#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge {
int a, b, c;
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<Edge> original_edges(m);
for(int i=0; i<m; ++i){
cin >> original_edges[i].a >> original_edges[i].b >> original_edges[i].c;
}
vector<pair<int, int>> extra_edges(k);
for(int i=0; i<k; ++i){
cin >> extra_edges[i].a >> extra_edges[i].b;
}
vector<int> p(n+1);
for(int i=1; i<=n; ++i){
cin >> p[i];
}
// Sort original edges by weight ascending
sort(original_edges.begin(), original_edges.end(), [&](const Edge &a, const Edge &b) -> bool{
return a.c < b.c;
});
// DSU
vector<int> parent_dsu(n+1);
for(int i=1; i<=n; ++i) parent_dsu[i]=i;
function<int(int)> find_set = [&](int x) -> int{
return parent_dsu[x] ==x ? x : parent_dsu[x] = find_set(parent_dsu[x]);
};
// Build MST
// Assign unique edge indices to MST edges
vector<tuple<int, int, int>> MST_edges;
// To build the tree
vector<vector<pair<int, int>>> adj(n+1);
// Mapping weight to A_e
// Max weight up to 1e6
const int MAX_C = 1000000;
vector<ll> weight_to_A(MAX_C+2, 0);
for(auto &e: original_edges){
int a = e.a;
int b = e.b;
int c = e.c;
if(find_set(a) != find_set(b)){
parent_dsu[find_set(a)] = find_set(b);
MST_edges.emplace_back(a, b, c);
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
}
// DFS to compute A_e, root at 1
int LOG = 20;
vector<vector<int>> parent(LOG, vector<int>(n+1, -1));
vector<vector<int>> max_weight(LOG, vector<int>(n+1, 0));
vector<ll> A_e(n+1, 0); // Not used, use weight_to_A
// Initialize
// parent[0][1] = -1
// A_e will be filled during the DFS
ll sum_p_total = 0;
for(int i=1; i<=n; ++i){
sum_p_total += p[i];
}
// To map weight to A_e, assuming weights are unique in MST
// Implement DFS
// To decide parent and A_e
// We need to know for each node, the parent and the edge weight
// Also, track which edge is connected to which child
// Implement a recursive DFS
// Also compute A_e during DFS
// To store A_e in weight_to_A
// Use a lambda for DFS
ll total_sum =0;
function<ll(int, int)> dfs = [&](int u, int par_u) -> ll{
ll subtotal = p[u];
for(auto &[v, w]: adj[u]){
if(v != par_u){
parent[0][v]=u;
max_weight[0][v]=w;
ll child_sum = dfs(v, u);
subtotal += child_sum;
weight_to_A[w]=child_sum;
}
}
return subtotal;
};
dfs(1, -1);
// Precompute Binary Lifting tables
for(int j=1; j<LOG; ++j){
for(int u=1; u<=n; ++u){
if(parent[j-1][u] != -1){
parent[j][u] = parent[j-1][parent[j-1][u]];
max_weight[j][u] = max(max_weight[j-1][u], max_weight[j-1][parent[j-1][u]]);
}
else{
parent[j][u] = -1;
max_weight[j][u] = max_weight[j-1][u];
}
}
}
// Function to get max weight on path from u to v
auto get_max_weight = [&](int u, int v) -> int{
if(u == v) return 0;
int max_w = 0;
if(u == v){
return 0;
}
// Bring u and v to the same level
if(u < 1 || u > n || v <1 || v>n){
return 0;
}
// Assume levels are handled by lifting
// Implement LCA-like lifting
// First, find the maximum weight from u to lca and v to lca
// Without storing levels, we simulate by lifting until they meet
// But better to compute levels first
// To implement levels, perform another DFS
// To simplify, assume the root is 1 and levels are correctly assigned
// Implement level assignment
static vector<int> level(n+1, -1);
static bool levels_computed = false;
if(!levels_computed){
// Compute levels
function<void(int, int, int)> compute_levels = [&](int node, int par, int lev) -> void{
level[node] = lev;
for(auto &[neighbor, w]: adj[node]){
if(neighbor != par){
compute_levels(neighbor, node, lev+1);
}
}
};
compute_levels(1, -1, 0);
levels_computed = true;
}
if(level[u] < level[v]){
swap(u, v);
}
// Lift u up to the level of v
for(int j=LOG-1; j>=0; --j){
if(parent[j][u] != -1 && level[parent[j][u]] >= level[v]){
max_w = max(max_w, max_weight[j][u]);
u = parent[j][u];
}
}
if(u == v){
return max_w;
}
// Lift both u and v up until their parents are the same
for(int j=LOG-1; j>=0; --j){
if(parent[j][u] != -1 && parent[j][u] != parent[j][v]){
max_w = max(max_w, max_weight[j][u]);
max_w = max(max_w, max_weight[j][v]);
u = parent[j][u];
v = parent[j][v];
}
}
// Finally, one more step to the LCA
max_w = max(max_w, max_weight[0][u]);
max_w = max(max_w, max_weight[0][v]);
return max_w;
};
// Now, process k edges
// Collect profits, ensuring unique f_e
// Use an array to mark replaced f_e by weight
vector<bool> replaced(MAX_C+2, false);
vector<ll> profits;
for(int i=0; i<k; ++i){
int u = extra_edges[i].first;
int v = extra_edges[i].second;
int max_w = get_max_weight(u, v);
if(max_w ==0){
continue; // No replacement possible
}
if(!replaced[max_w]){
if(max_w >=1 && max_w <= MAX_C){
ll A_e = weight_to_A[max_w];
ll profit = (ll)max_w * A_e;
profits.push_back(profit);
replaced[max_w] = true;
}
}
}
// Sort profits descendingly
sort(profits.begin(), profits.end(), [&](const ll &a, const ll &b) -> bool{
return a > b;
});
// Take top k profits
ll answer =0;
for(int i=0; i<min((int)profits.size(), k); ++i){
answer += profits[i];
}
cout << answer;
}