#585
Unrated
Visits
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Each of Bessie’s NN (2N1052\le N\le 10^5) bovine buddies (conveniently labeled 1N1\ldots N) owns her own farm. For each 1iN1\le i\le N, buddy ii wants to visit buddy aia_i (aiia_i\neq i).

Given a permutation (p1,p2,,pN)(p_1,p_2,\ldots, p_N) of 1N1\ldots N, the visits occur as follows.

For each ii from 11 up to NN:

If buddy apia_{p_i} has already departed her farm, then buddy pip_i remains at her own farm.Otherwise, buddy pip_i departs her farm to visit buddy apia_{p_i}’s farm. This visit results in a joyful "moo" being uttered vpiv_{p_i} times (0vpi1090\le v_{p_i}\le 10^9).

Compute the maximum possible number of moos after all visits, over all possible permutations pp.

입력

The first line contains NN.

For each 1iN1\le i\le N, the i+1i+1-st line contains two space-separated integers aia_i and viv_i.

출력

A single integer denoting the answer.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

예제 입력 1

4
2 10
3 20
4 30
1 40

예제 출력 1

90

점수

Test cases 2-3 satisfy aiaja_i\neq a_j for all iji\neq j.Test cases 4-7 satisfy N103N\le 10^3.Test cases 8-11 satisfy no additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.