Halloween '14 P4 - Fox Girls

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

This morning, you woke up and realized that some foxen have turned into girls! In light of this exciting event, you have kindly invited Izuna, a fox-girl, over to your house (with only pure intentions, of course). You have some anime that you want to share with Izuna, but your list of anime is very long. Therefore, you will adopt the strategy of watching some anime with Izuna while she's at your house, and then leaving with her a list of recommendations.

For each anime in your list, you will recommend to Izuna another anime from the list, meaning that if she has watched one at your house, she should definitely watch the other sometime later. Being an obedient fox-girl, Izuna will always follow your recommendations.

Of course, you have to start watching some anime with Izuna at your house in order for her to follow all the recommendations and eventually watch them all, but since you are short on time, you need to determine the minimum number of minutes you spend watching anime at your house before Izuna can follow some of the recommendations and eventually watch all the anime in the list.

Input Specification

The first line of input will have N (2 \le N \le 100\,000), the number of anime in your list, numbered from 1 to N.

The second line of input will have N space-separated integers, the time it takes in minutes to watch the i^\text{th} anime (1 \le \text{time}_i \le 10^9).

The third line of input will have N space-separated integers, meaning that you recommend the i^\text{th} anime if Izuna watched the anime with the i^\text{th} number in this list at your house, and vice versa (1 \le \text{recommendation}_i \le N, \text{recommendation}_i \ne i).

Test cases worth 25% of the marks will have 2 \le N \le 20.

Test cases worth another 25% of the marks will have 2 \le N \le 5\,000.

Of the aforementioned cases, 50% of those will have \text{time}_i = 1 (1 \le i \le N).

Output Specification

The first and only line of output should be the minimal number of minutes spent watching anime so that Izuna will eventually finish watching all the anime on your list.

Sample Input 1

3
20 23 42
2 3 1

Sample Output 1

20

Explanation for Sample Output 1

By taking 20 minutes to watch the first anime, Izuna will definitely watch the other two anime due to the recommendations.

Sample Input 2

4
100 23 23 24
2 3 1 1

Sample Output 2

47

Explanation for Sample Output 2

Although watching just the first anime is enough for Izuna to watch the rest of the anime on her own, that will take 100 minutes. It is much more efficient to watch the fourth anime and either the second or third anime at your house for a total of 47 minutes.


Comments


  • 23
    Moopli  commented on Nov. 30, 2014, 5:02 a.m.

    It is good to know programming club hasn't changed one bit.


    • 1
      bobhob314  commented on Jan. 4, 2015, 10:13 p.m.

      lol xD What uni are you at now?