Submission

Status:
[PPPPPPPPPP][PPPPP][PPPPPPPP][PPPPPPPPPP]

Score: 100

User: njoop

Problemset: D.Drunk

Language: cpp

Time: 0.655 second

Submitted On: 2025-01-05 17:14:22

#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
vector<pair<ll, ll>> g[1000010];
ll d[1000010], x, n, cd, cn, nd, nn, ans;
vector<int> st;
queue<pair<ll, ll>> pq;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for(int i=1; i<=n; i++) {
        cin >> d[i];
    }
    for(int i=1; i<=n; i++) {
        cin >> x;
        if(i == x) st.push_back(i); 
        else g[x].push_back({d[i], i});
    }
    for(int i: st) {
        pq.push({d[i], i});
    }
    while(!pq.empty()) {
        cd = pq.front().first;
        cn = pq.front().second;
        ans = max(ans, cd);
        pq.pop();
        for(auto i: g[cn]) {
            nd = cd+i.first;
            nn = i.second;
            pq.push({nd, nn});
        }
    }
    cout << ans;
    return 0;
}