import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; alias Edge = Tuple!(int, "a", int, "b", int, "cost"); void main() { int n, m, k; scan(n, m, k); auto es = new Edge[](m); int ans; foreach (i ; 0 .. m) { int ai, bi, ci; scan(ai, bi, ci); es[i] = Edge(ai-1, bi-1, ci); ans += ci; } auto uf = UnionFind(n); foreach (i ; 0 .. k) { int pi; scan(pi); pi--; ans -= es[pi].cost; uf.merge(es[pi].a, es[pi].b); } es.sort!"a.cost < b.cost"(); foreach (i ; 0 .. m) { if (uf.numcc == 1) { break; } if (!uf.same(es[i].a, es[i].b)) { uf.merge(es[i].a, es[i].b); ans -= es[i].cost; } } writeln(ans); } struct UnionFind { private { int _size; int[] _parent; int _numcc; } this(int N) in { assert(N > 0); } body { _size = N; _numcc = N; _parent = new int[](_size); foreach (i ; 0 .. _size) { _parent[i] = i; } } int findRoot(int x) in { assert(0 <= x && x < _size); } body { if (_parent[x] != x) { _parent[x] = findRoot(_parent[x]); } return _parent[x]; } bool same(int x, int y) in { assert(0 <= x && x < _size); assert(0 <= y && y < _size); } body { return findRoot(x) == findRoot(y); } void merge(int x, int y) in { assert(0 <= x && x < _size); assert(0 <= y && y < _size); } body { int u = findRoot(x); int v = findRoot(y); if (u == v) return; _parent[v] = u; _numcc--; } int numcc() { return _numcc; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }