#P11782. Maximum Score from Card Pairing

    ID: 13878 Type: Default 1000ms 256MiB

Maximum Score from Card Pairing

Maximum Score from Card Pairing

You are given n cards. Each card has a color \(c_i\) and a number \(a_i\). Initially, your score is \(0\). You may perform the following operation any number of times:

  • Select two cards \(i\) and \(j\) such that they have different colors (i.e. \(c_i \neq c_j\)). Add \(a_i + a_j\) to your score, and then discard one of these two cards.

The process stops when no valid operation can be performed (i.e. all remaining cards have the same color, or fewer than two cards remain). Determine the maximum score you can obtain.

Note: Any formulas appearing in the statement are in \(\LaTeX\) format.

inputFormat

The input begins with a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of cards.

Then \(n\) lines follow. Each of these lines contains two integers \(c_i\) and \(a_i\), representing the color and the number of the \(i\)-th card. It is guaranteed that the values of \(a_i\) are non-negative.

For example:

4
1 5
2 3
1 2
3 10

outputFormat

Output a single integer, the maximum score that can be obtained by performing the operations optimally.

sample

1
1 10
0

</p>