Graph

5

1 votes
Tree, Dynamic Programming, Algorithms, Medium
Problem

Given two sequences s1sn and w1wn. A graph is built with n vertices and n directed edges (i,si). Cost to change some si is  wi . 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, s1sn.

The third line contains n integers w1wn.

Output

An integer representing the answer.

Constraints

1n105

1sin

1wi109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Change s2 to 3 and s3 to 4.

Editor Image

?