All Tracks Algorithms Dynamic Programming Problem

Graph
Tag(s):

Algorithms, Dynamic Programming, Medium, 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
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

April Circuits '18

OTHER PROBLEMS OF THIS CHALLENGE
通知
View All Notifications

?