Information Merchant Hoseok
InterviewTime limit2sMemory limit512 MB
Process queries where gorillas gain information values and Hoseok buys the b most valuable pieces from a named gorilla, and report the total value bought.
- Level
Medium6 of 10
- Topics
- Heap, Hash map, Simulation, Greedy
- Solved
- No attempts yet
Problem
Power in the underworld comes from fists and information. A fist is strong against one person, while information can make a plaything of the world, so Hoseok wants to become an "information merchant" who gathers all the information in the world. An information merchant is someone who buys and sells information.
Hoseok is still a newcomer in the merchant world, so he plans to gather information from several "information gorillas" through an initial investment. An information gorilla is someone who collects information from here and there. To scrape together information, Hoseok tries to buy information from several information gorillas.

The underworld's network is dense, so rumors about who obtained what information spread all the time. What a rumor tells is that a gorilla with a certain name obtained k pieces of information worth , , ..., .
Based on this, Hoseok can decide, at any moment, how many pieces of information to buy from a particular gorilla. He buys the most valuable information first. For example, if a gorilla has 10 pieces of information and Hoseok wants to buy 4 pieces, the gorilla sells the 4 most valuable pieces out of the 10. Once a piece of information is traded, it no longer has value for Hoseok, so the gorilla discards it as well.
You are a fist of the underworld, watching Hoseok, who may become one of the two great powers. The information you obtain from watching amounts to pieces in total. Each piece of information is one of the following two kinds.
- 1 Name : the gorilla named [Name] obtained pieces of information, with values from to .
- 2 Name : Hoseok buys pieces of information from the gorilla named [Name]. He buys the most valuable pieces among the information the gorilla has, and if the gorilla has pieces or fewer, he buys all of it.
To keep him in check, find the total value of the information Hoseok has, that is, the total amount of money Hoseok spent buying information.
Input
The events in which gorillas obtain information and the trades Hoseok makes are given in chronological order. The first line gives the number of queries .
Then lines follow, each giving one query. A query starts with 1 or 2. A query starting with 1 gives the name of the information gorilla that obtained information and , followed by the information values as natural numbers. Every is between 1 and 100,000 inclusive. A query starting with 2 gives the name of the information gorilla Hoseok wants to trade with and the number of pieces of information he wants to buy.
Output
Print the total value of the information Hoseok obtains after all queries have finished.
Constraints
- 1 ≤ ≤ 100,000, is a natural number
- Every Name consists of lowercase or uppercase English letters, contains no spaces, and has a length between 1 and 15 inclusive.
- 1 ≤ ≤ 100,000, is a natural number
- 1 ≤ ≤ 100,000, is a natural number
- 1 ≤ ≤ 100,000, is a natural number
- The sum of over all queries does not exceed 1,000,000.