CF1027D.Mouse Hunt

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 44 (hopefully) years.

The dormitory consists of nn rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number ii costs cic_i burles. Rooms are numbered from 11 to nn.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room ii in second tt then it will run to room aia_i in second t+1t + 1 without visiting any other rooms inbetween (i=aii = a_i means that mouse won't leave room ii). It's second 00 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 11 to nn at second 00.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

伯兰国立大学医学院刚刚结束了本年度的招生工作。和往常一样,约 80%80\% 的申请者为女生,其中大多数将在接下来的 44 年(但愿如此)里住在大学宿舍中。

该宿舍共有 nn 个房间,以及一只老鼠!女生们决定在部分房间中安装捕鼠器,以消灭这个可怕的“怪物”。在第 ii 号房间安装捕鼠器的费用为 cic_i 卢布。房间编号从 11 到 nn。

老鼠并不会一直待在原地,而会持续奔跑:若它在第 tt 秒位于第 ii 号房间,则它将在第 t+1t+1 秒直接跑入第 aia_i 号房间(中间不经过任何其他房间)(若 i=aii = a_i,则表示老鼠将停留在第 ii 号房间不动)。初始时刻为第 00 秒。若老鼠在某一秒恰好位于一个装有捕鼠器的房间内,则它将被该捕鼠器捕获。

倘若女生们确切知道老鼠的初始位置,问题就简单多了。遗憾的是,事实并非如此——老鼠在第 00 秒可能位于任意一个从 11 到 nn 的房间中。

那么,女生们至少需要花费多少卢布来安装捕鼠器,才能确保无论老鼠从哪个房间出发,最终都必然被捕获?

输入格式

The first line contains as single integers nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of rooms in the dormitory.

The second line contains nn integers c1,c2,…,cnc_1, c_2, \dots, c_n (1≤ci≤1041 \le c_i \le 10^4) — cic_i is the cost of setting the trap in room number ii.

The third line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) — aia_i is the room the mouse will run to the next second after being in room ii.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示宿舍中的房间数量。

第二行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(1≤ci≤1041 \le c_i \le 10^4)—— 其中 cic_i 表示在第 ii 号房间设置陷阱的花费。

第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)—— 其中 aia_i 表示老鼠在第 ii 号房间停留一秒后,下一秒将跑向的房间编号。

输出格式

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

输出一个整数——女孩们设置陷阱所需花费的最少总金额(单位:burles),以确保无论老鼠从哪个房间出发,最终都会被抓住。

输入输出样例

  • 输入#1

    5
    1 2 3 2 10
    1 3 4 3 3

    输出#1

    3
  • 输入#2

    4
    1 10 2 10
    2 4 2 2

    输出#2

    10
  • 输入#3

    7
    1 1 1 1 1 1 1
    2 2 2 3 6 7 6

    输出#3

    2

说明/提示

In the first example it is enough to set mouse trap in rooms 11 and 44. If mouse starts in room 11 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 44.

In the second example it is enough to set mouse trap in room 22. If mouse starts in room 22 then it gets caught immideately. If mouse starts in any other room then it runs to room 22 in second 11.

Here are the paths of the mouse from different starts from the third example:

  • 1→2→2→…1 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 2→2→…2 \rightarrow 2 \rightarrow \dots;
  • 3→2→2→…3 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 4→3→2→2→…4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 5→6→7→6→…5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots;
  • 6→7→6→…6 \rightarrow 7 \rightarrow 6 \rightarrow \dots;
  • 7→6→7→…7 \rightarrow 6 \rightarrow 7 \rightarrow \dots;

So it's enough to set traps in rooms 22 and 66.

在第一个例子中,只需在房间 11 和 44 设置捕鼠器即可。若老鼠起始于房间 11,则它会立即被捕获;若老鼠起始于其余任意房间,则它最终都会到达房间 44。

在第二个例子中,只需在房间 22 设置捕鼠器即可。若老鼠起始于房间 22,则它会立即被捕获;若老鼠起始于其余任意房间,则它会在第 11 秒时跑到房间 22。

以下是第三个例子中老鼠从不同起点出发的路径:

  • 1→2→2→…1 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 2→2→…2 \rightarrow 2 \rightarrow \dots;
  • 3→2→2→…3 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 4→3→2→2→…4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots;
  • 5→6→7→6→…5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots;
  • 6→7→6→…6 \rightarrow 7 \rightarrow 6 \rightarrow \dots;
  • 7→6→7→…7 \rightarrow 6 \rightarrow 7 \rightarrow \dots;

因此,只需在房间 22 和 66 设置捕鼠器即可。

输入解题思路,AI测评打分。不知道怎么写?

首页