A Fundraising Problem

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 162M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

N people want to donate money to DMOJ. Each person has one person that they hate though, so they will not donate money if the person they hate donates to DMOJ.

Compute the maximum amount of money that DMOJ can receive.


1 \le N \le 10^6

a_i \le 10^6

Input Specification

The first line contains a single integer, N.

The next N lines contain two space-separated integers. The first is the amount of money person i will donate, a_i. The second is the index of the person they hate, which will be one-indexed.

Output Specification

Output the desired total.

Sample Input

10 2
20 3
30 1

Sample Output



  • 9
    discoverMe  commented on May 25, 2019, 6:12 p.m.

    unlike in real life you can't hate yourself

    • 3
      Darcy___Liu  commented on May 26, 2019, 12:03 a.m. edited


    • 1
      Roronoa_Zoro1540  commented on May 25, 2019, 7:21 p.m.

      yo nice one victor when will u send me some of that milk tea homie

      • 2
        subscriber  commented on May 25, 2019, 8:03 p.m.

        It suffices to Jacks has a severe of case Autismophotonicala