欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1027D.Mouse Hunt
Mouse Hunt
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about of applicants are girls and majority of them are going to live in the university dormitory for the next (hopefully) years.
The dormitory consists of 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 costs burles. Rooms are numbered from to .
Mouse doesn't sit in place all the time, it constantly runs. If it is in room in second then it will run to room in second without visiting any other rooms inbetween ( means that mouse won't leave room ). It's second 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 to at second .
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?
Input
The first line contains as single integers () — the number of rooms in the dormitory.
The second line contains integers () — is the cost of setting the trap in room number .
The third line contains integers () — is the room the mouse will run to the next second after being in room .
Output
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.
Note
In the first example it is enough to set mouse trap in rooms and . If mouse starts in room then it gets caught immideately. If mouse starts in any other room then it eventually comes to room .
In the second example it is enough to set mouse trap in room . If mouse starts in room then it gets caught immideately. If mouse starts in any other room then it runs to room in second .
Here are the paths of the mouse from different starts from the third example:
- ;
- ;
- ;
- $4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots$;
- $5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots$;
- ;
- ;
So it's enough to set traps in rooms and .
Samples
5
1 2 3 2 10
1 3 4 3 3
3
4
1 10 2 10
2 4 2 2
10
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
2
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |