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\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
Dynamic ProgrammingTreeMediumAlgorithmsKadaneDepth-first search
Points:30
Tags:
Dynamic ProgrammingAlgorithmsTrees
Points:30
3 votes
Tags:
dynamic programmingTreebitmaskMediumAlgorithmsDynamic Programming
Editorial