We need to interpret the problem: There is a graph of n vertices. Each vertex i has a cost ci to bribe (i.e., to start spreading rumor). Once a vertex is bribed, the rumor spreads automatically through edges: if any vertex knows rumor, it tells all its friends, who then also know rumor and spread further. So essentially, if we bribe some set of vertices, the rumor will reach all vertices in the same connected component as any bribed vertex. Our goal: ensure every vertex eventually knows rumor. Minimize total bribe cost.
Thus the problem reduces to: For each connected component of the graph, we need to select at least one vertex to bribe, and we should pick the one with minimum cost in that component. Since bribing one vertex in a component will eventually inform all vertices in that component. However, is there any reason to bribe more than one vertex in the same component? No, because if we bribe one, it spreads to all others, so bribing extra would waste gold. So the optimal is to bribe exactly the minimum cost vertex in each connected component. However, careful: If there are isolated vertices (degree 0), they form components of size 1; we have to bribe them individually. So answer = sum over components of min ci in that component.
Thus the problem is simply: find connected components (using DSU or DFS/BFS) and compute min cost for each component, sum them.
Constraints: n up to 100k, m up to 100k, costs up to 1e9. So DSU or DFS both fine.
But note: The input: "some characters are friends to each other, and they share information they got." It suggests that if A and B are friends, then if A knows rumor, B will get it for free. And B then spreads to its friends, etc. So yes, rumor propagates across entire connected component.
Thus answer: For each connected component, minimum cost.
Check examples:
Example 1: n=5, edges: 1-4, 4-5. So components: {1,4,5} and {2} and {3}. Costs: 2,5,3,4,8. min in {1,4,5} = min(2,4,8)=2; min in {2}=5; min in {3}=3; sum=2+5+3=10. Correct.
Example 2: n=10, m=0, all isolated; each component = single vertex; min cost each = its cost; sum = sum of all = 55. Correct.
Example 3: n=10, edges: 1-2,3-4,5-6,7-8,9-10. So components: {1,2},{3,4},{5,6},{7,8},{9,10}. Costs: 1,6,2,7,3,8,4,9,5,10. Min in each: {1,2} min=1, {3,4} min=2, {5,6} min=3, {7,8} min=4, {9,10} min=5; sum=1+2+3+4+5=15. Correct.
So solution is straightforward.
Implementation: Use DSU: initialize each vertex as separate set; for each edge (x,y) union sets. Then maintain for each set the minimum cost. Or using adjacency list + visited boolean for DFS to find components.
Let's choose DSU: For each node, parent[i]=i, size[i]=1 (or rank), minCost[i] = ci initially. When union, combine sets and set minCost[newRoot] = min(minCost[rootA], minCost[rootB]). After processing all edges, iterate over nodes; if node is root of its set, add minCost[root] to answer.
Complexities: O(n + m * α(n)). Works.
Edge cases: m may be 0; DSU works.
Need to watch for large costs: sum may be up to 1e14? n up to 100k, each cost up to 1e9, so sum up to 1e14, fits in 64-bit integer (use long long in C++). Python int unlimited.
Implementation in Python: read input, DSU with path compression and union by size. At the end sum minCosts for roots.
Potential pitfalls: Input lines may be many; use sys.stdin.read for speed.
Write code accordingly.
Now I'll produce final answer with explanation and code.
But the user asked: "Vova promised himself that he would never play computer games... But recently ..." They want solution to compute minimum gold. So answer should be code? Probably they want algorithm explanation? The user is ChatGPT and they're asking for solution. The typical response is to provide code and explanation. We'll include explanation and code in Python.
But note the guidelines: "You are ChatGPT, a large language model ... Provide answers in a natural, helpful manner." The problem is from Codeforces (maybe). We'll produce solution.
Thus final answer:
Approach:
- The rumor spreads through the friendship network: any connected component will be fully informed once at least one person in it is bribed.
- Therefore, we must bribe at least one person in each connected component, and the cheapest such person is the minimum cost vertex in that component.
- So we need to compute all connected components of the graph and sum the minimum cost per component.
Implementation Details:
- Use Disjoint Set Union (DSU) to merge vertices based on edges.
- Maintain for each set the minimum cost among its members.
- After processing all edges, iterate over all vertices, identify root of each set, and sum the minimum cost stored in that root.
Complexity: O((n + m) * α(n)) time, O(n) space.
Now code.
But careful: DSU union: typical union by size or rank. Ensure we update min cost after union.
We'll implement:
import sys
def main():
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
costs = [0] + [int(next(it)) for _ in range(n)] # 1-indexed
parent = list(range(n+1))
size = [1] * (n+1)
minCost = costs[:] # copy
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a, b):
ra = find(a)
rb = find(b)
if ra == rb:
return
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
if minCost[rb] < minCost[ra]:
minCost[ra] = minCost[rb]
for _ in range(m):
x = int(next(it))
y = int(next(it))
union(x, y)
total = 0
for i in range(1, n+1):
if parent[i] == i: # root
total += minCost[i]
print(total)
if __name__ == "__main__":
main()
This solves the problem.
But one nuance: In the union function, we only update minCost for the new root. However, after path compression, some nodes may have parent that is not the root; but that's fine because we only use roots.
Alternatively, use visited set of roots. The code works.
Let's test with examples:
Example 1: n=5, costs [2,5,3,4,8]; edges:1-4,4-5. After unions: component roots: root of 1,4,5 might be 1 (if union ordering). minCost[1] becomes min of all 2,4,8 =2. For 2 and 3, they are roots. sum=2+5+3=10.
Example 2: n=10, m=0, each is root, sum all costs =55.
Example 3: n=10, edges... union pairs, each pair's min becomes the smaller one; sum=1+2+3+4+5=15.
Works.
Thus final answer with code and explanation.
I'll include comments in code.
Now produce final answer.
```python
import sys
def main() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
# costs are 1-indexed for convenience
cost = [0] + [int(next(it)) for _ in range(n)]
parent = list(range(n + 1))
size = [1] * (n + 1) # for union by size
min_in_set = cost[:] # per-set minimum cost
# ---------- DSU ----------
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression
x = parent[x]
return x
def union(a: int, b: int) -> None:
ra = find(a)
rb = find(b)
if ra == rb:
return
# union by size: attach smaller tree to larger
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
# keep the smallest cost in the new root
if min_in_set[rb] < min_in_set[ra]:
min_in_set[ra] = min_in_set[rb]
# ---------- read edges ----------
for _ in range(m):
x = int(next(it))
y = int(next(it))
union(x, y)
# ---------- compute answer ----------
total = 0
for v in range(1, n + 1):
if parent[v] == v: # v is a root of its set
total += min_in_set[v]
print(total)
if name == "main":
main()