Visits
시간 제한2초메모리 제한1024 MB
각 소가 방문하려는 대상 a[i]가 주어진 함수 그래프에서, 방문 순서를 정해 자기 대상이 아직 떠나지 않은 상태로 출발하는 소들의 가치 합을 최대로 만든다.
문제
Each of Bessie’s () bovine buddies (conveniently labeled ) owns her own farm. For each , buddy wants to visit buddy ().
Given a permutation of , the visits occur as follows.
For each from up to :
- If buddy has already departed her farm, then buddy remains at her own farm.
- Otherwise, buddy departs her farm to visit buddy ’s farm. This visit results in a joyful "moo" being uttered times ().
Compute the maximum possible number of moos after all visits, over all possible permutations .
입력
The first line contains .
For each , the -st line contains two space-separated integers and .
출력
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++).
힌트
If then
- Buddy visits buddy 's farm, resulting in moos.
- Buddy sees that buddy has already departed, so nothing happens.
- Buddy visits buddy 's farm, adding moos.
- Buddy sees that buddy has already departed, so nothing happens.
This gives a total of moos.
On the other hand, if then
- Buddy visits buddy 's farm, causing moos.
- Buddy visits buddy 's farm, causing moos.
- Buddy visits buddy 's farm, causing moos.
- Buddy sees that buddy has already departed, so nothing happens.
This gives total moos. It can be shown that this is the maximum possible amount after all visits, over all permutations .