All Tracks Algorithms Dynamic Programming Problem

Graph
/

Algorithms, Dynamic Programming, Tree

Problem
Editorial
Analytics

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\)

SAMPLE INPUT
4
2 1 1 1
3 1 1 3
SAMPLE OUTPUT
2
Explanation

Change \(s_2\) to 3 and \(s_3\) to 4.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB

Best Submission

Similar Problems

Contributors

This Problem was Asked in

Initializing Code Editor...
通知
View All Notifications

?