QOJ.ac

QOJ

Límite de tiempo: 3 s Límite de memoria: 512 MB Puntuación total: 100

#17182. Huffman Tree I

Estadísticas

Given $n$ pairs $(a_i, b_i)$, representing that the multiset $S$ contains $b_i$ occurrences of $a_i$.

An encoding tree is defined as a full binary tree $T$ with exactly $|S|$ leaf nodes, where each leaf node is assigned a weight, and the $|S|$ weights correspond one-to-one with the elements in $S$.

The total weight of an encoding tree is defined as $\sum\limits_{u\in\text{leaf}} dep_u\cdot w_u$, where $\text{leaf}$ is the set of all leaf nodes, $dep_u$ is the depth of $u$ (the depth of the root is $0$), and $w_u$ is the weight of $u$.

A Huffman tree is defined as an encoding tree with the minimum total weight.

Calculate the total weight of the Huffman tree.

Input

The first line contains an integer $n$.

The next $n$ lines each contain two integers $a_i$ and $b_i$.

Output

A single integer representing the answer.

Examples

Input 1

3
1 3
2 2
3 1

Output 1

25

Constraints

For $100\%$ of the data, $1\le n, a_i, b_i \le 2\times 10^5$, and all $a_i$ are distinct.

Subtasks:

  • Subtask 1 ($30\%$): $\sum b_i \le 1000$.
  • Subtask 2 ($30\%$): $\sum b_i \le 2\times 10^5$.
  • Subtask 3 ($20\%$): $n \le 1000$.
  • Subtask 4 ($20\%$): No special restrictions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.