记录编号 49075 评测结果 AWWWWWWWWW
题目名称 [河南省队2012] 电脑幽魂 最终得分 10
用户昵称 Gravatarskyfisherman 是否通过 未通过
代码语言 C++ 运行时间 3.687 s
提交时间 2012-11-07 12:14:03 内存使用 11.73 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

struct node{
	int value, next;
}a[1000002];
bool cmp(const node&a,const node&b)
{
	return a.value<b.value;
}
bool flag[1000002];

int n,cnt;
long long ans;
int main()
{
	freopen("ghost.in","r",stdin);
	freopen("ghost.out","w",stdout);
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i].value);
	for(int i=1;i<=n;i++)scanf("%d",&a[i].next);
	for(int i=1;i<=n;i++)
		if(a[i].value!=a[i].next)
			ans+=a[i].value;
		else
			flag[i]=true;
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++)if(!flag[i])
	{
		cnt=0;
		for(int k=i;!flag[k];k=a[k].next)
			cnt++, flag[k]=true; 
		ans+=(long long)(cnt-2)*i;
	}
	cout<<ans<<endl;
//	system("pause");
	return 0;
}