#P3651. 展翅翱翔之时 (はばたきのとき)

    ID: 2662 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心枚举,暴力环套树,基环树洛谷月赛

展翅翱翔之时 (はばたきのとき)

Description

However, it seems there is some trouble with the relay satellites…

We have a total of NN relay satellites, numbered 11 to NN. Each relay satellite can and only can receive data unidirectionally from exactly one other relay satellite.

The ii-th satellite is currently set to receive data from the AiA_i-th satellite (called the source).

The source of these relay satellites can be modified, but each modification costs some money.

It is said that to achieve relaying, these satellites must be able to communicate with each other pairwise (directly or indirectly).

Although Mari is very rich, expenses this large still need to be planned in advance.

So, can you help us calculate the minimum total amount of money needed?

Input Format

The first line contains an integer NN.

Then NN lines follow. Each line contains 22 integers Ai,CiA_i, C_i, indicating that initially the ii-th relay satellite receives data from the AiA_i-th satellite, and the cost required to adjust the source of this satellite.

Output Format

Output a single integer, which is the minimum cost that Mari needs to prepare.

4
2 2
1 6
1 3
3 1
5

Hint

Constraints and Notes

  • For 10%10\% of the testdata, N10N \le 10.
  • For 40%40\% of the testdata, N15N \le 15.
  • For 70%70\% of the testdata, N3000N \le 3000.
  • For 100%100\% of the testdata, 2N1000002 \le N \le 100000, 1Ci1091 \le C_i \le 10^9.

Easter egg

In fact, LoveLive’s live satellite relay uses only one satellite, and it is never encrypted.

As a result, as long as you have a satellite dish, you can secretly watch the live broadcast at home — the legendary satellite source.

lin_toto: Bandai Namco even bought out Shallow Water Bay, yet there was only a replay, so I had to go to Hong Kong’s the sky to watch (wry smile).

As for why watch a relay screening, eplus says for LoveLive series concerts, everyone can enter lotteries and try to buy; if you manage to buy a ticket, I lose.

So last year during μ's Final LoveLive, lin_toto took down eplus with a botnet and then managed to buy tickets.

This year, eplus didn’t even allow rush purchases; it was all lotteries. If you get selected, I lose.

So lin_toto went to watch the relay screening.

Translated by ChatGPT 5