Company: Npci_1aug

Difficulty: medium

Problem Statement

Rogue Packet Filters There are N servers. Server i forwards every packet to server a[i] after one second. A packet may start at any server. Installing a filter on server i costs c[i] ; a packet is neutralized as soon as it visits a filtered server. Find the minimum total cost of filters that guarantees every possible packet is eventually neutralized. Input The first line contains N . The second line contains N costs c[1] ... c[N] . The third line contains N destinations a[1] ... a[N] . Output Print the minimum total cost. Constraints 1 <= N <= 200000 1 <= c[i] <= 10000 1 <= a[i] <= N Use a 64-bit integer for the answer. Examples Input: 5 1 2 3 2 10 1 3 4 3 3 Output: 3 Input: 4 1 10 2 10 2 4 2 2 Output: 10 Notes Arrays and server labels are 1-indexed. A self-loop is a cycle and must contain a filter.

More Npci_1aug OA questionsInterview experiences