Logo
(追記) (追記ここまで)

24977번 - Visits 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 1024 MB41117115843.288%

문제

Each of Bessie’s $N$ (2ドル\le N\le 10^5$) bovine buddies (conveniently labeled 1ドル\ldots N$) owns her own farm. For each 1ドル\le i\le N,ドル buddy $i$ wants to visit buddy $a_i$ ($a_i\neq i$).

Given a permutation $(p_1,p_2,\ldots, p_N)$ of 1ドル\ldots N,ドル the visits occur as follows.

For each $i$ from 1ドル$ up to $N$:

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

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

입력

The first line contains $N$.

For each 1ドル\le i\le N,ドル the $i+1$-st line contains two space-separated integers $a_i$ and $v_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

힌트

If $p=(1,4,3,2)$ then

  • Buddy 1ドル$ visits buddy 2ドル$'s farm, resulting in 10ドル$ moos.
  • Buddy 4ドル$ sees that buddy 1ドル$ has already departed, so nothing happens.
  • Buddy 3ドル$ visits buddy 4ドル$'s farm, adding 30ドル$ moos.
  • Buddy 2ドル$ sees that buddy 3ドル$ has already departed, so nothing happens.

This gives a total of 10ドル+30=40$ moos.

On the other hand, if $p=(2,3,4,1)$ then

  • Buddy 2ドル$ visits buddy 3ドル$'s farm, causing 20ドル$ moos.
  • Buddy 3ドル$ visits buddy 4ドル$'s farm, causing 30ドル$ moos.
  • Buddy 4ドル$ visits buddy 1ドル$'s farm, causing 40ドル$ moos.
  • Buddy 1ドル$ sees that buddy 2ドル$ has already departed, so nothing happens.

This gives 20ドル+30+40=90$ total moos. It can be shown that this is the maximum possible amount after all visits, over all permutations $p$.

출처

Olympiad > USA Computing Olympiad > 2021-2022 Season > USACO 2022 US Open Contest > Silver 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /