CodeForces1388D Captain Flint and Treasure

D. Captain Flint and Treasure
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure’s location or may not. That’s why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this…

There are two arrays a and b of length n. Initially, an ans is equal to 0 and the following operation is defined:

Choose position i (1≤i≤n);
Add ai to ans;
If bi≠−1 then add ai to abi.
What is the maximum ans you can get by performing the operation on each i (1≤i≤n) exactly once?

Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

Input
The first line contains the integer n (1≤n≤2⋅105) — the length of arrays a and b.

The second line contains n integers a1,a2,…,an (−106≤ai≤106).

The third line contains n integers b1,b2,…,bn (1≤bi≤n or bi=−1).

Additional constraint: it’s guaranteed that for any i (1≤i≤n) the sequence bi,bbi,bbbi,… is not cyclic, in other words it will always end with −1.

Output
In the first line, print the maximum ans you can get.

In the second line, print the order of operations: n different integers p1,p2,…,pn (1≤pi≤n). The pi is the position which should be chosen at the i-th step. If there are multiple orders, print any of them.

Examples
input
3
1 2 3
2 3 -1
output
10
1 2 3
input
2
-1 100
2 -1
output
99
2 1
input
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9
output
-9
3 5 6 1 9 4 10 7 8 2

int n;
	cin >> n;
	vector<ll> v(n),in(n);
	vector<int> fans;
	ll ans=0;
	for(int i=0;i<n;++i) cin >> v[i];
	vector<vector<int>> adj(n);
	for(int i=0;i<n;++i)
	{
		int t;
		cin >> t;
		if(t!=-1)
		{
			adj[i].push_back(t-1);
			in[t-1]++;
		}
	}
	vector<int> topsort;
	queue<int> q;
	vector<int> vis(n);
	for(int i=0;i<n;++i)
	{
		if(in[i]==0)
		{
			q.push(i);
			vis[i]=1;
		}
	}
	while(!q.empty())
	{
		int temp=q.front();
		topsort.push_back(temp);
		q.pop();
		for(int u:adj[temp])
		{
			in[u]--;
			if(!vis[u] && in[u]==0)
			{
				vis[u]=1;
				q.push(u);
			}
		}
	}
	for(int i=0;i<n;++i)
	{
		if(v[topsort[i]]>=0)
		{
			ans+=v[topsort[i]];
			for(int u:adj[topsort[i]])
			{
				v[u]+=v[topsort[i]];
			}
			fans.push_back(topsort[i]);
		}
		else
		ans+=v[topsort[i]];
	}
	for(int i=n-1;i>=0;--i)
	{
		if(v[topsort[i]]<0)
		fans.push_back(topsort[i]);
	}
	cout << ans << endl;
	for(int u:fans)
	cout << u+1 << " ";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值