Graph
Practice
5 (1 votes)
Tree
Dynamic programming
Algorithms
Medium
Problem
75% Success 649 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Given two sequences \(s_1{\dots}s_n\) and \(w_1{\dots}w_n\). A graph is built with n vertices and n directed edges \((i,s_i)\). Cost to change some \(s_i\) is \(w_i\) . Your goal is to make the graph strongly connected with minimum cost.
Input
The first line contains an integer n.
The second line contains n intergers, \(s_1{\dots}s_n\).
The third line contains n integers \(w_1{\dots}w_n\).
Output
An integer representing the answer.
Constraints
\(1 \le n \le 10^5\)
\(1 \le s_i \le n\)
\(1 \le w_i \le 10^9\)
Explanation
Change \(s_2\) to 3 and \(s_3\) to 4.
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
23 votes
Tags:
EasyNumber Theory
Points:30
Tags:
Medium
Points:30
12 votes
Tags:
AlgorithmsDepth-first searchTreesDynamic Programming
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output