This page is still under construction.

Parts of this page are still being built. What you see may change.

Information Merchant Hoseok

Interview

Time limit2sMemory limit512 MB

Summary
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 C1C_1, C2C_2, ..., CkC_k.

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 QQ pieces in total. Each piece of information is one of the following two kinds.

  • 1 Name kk C1,C2,...,CkC_1, C_2, ..., C_k : the gorilla named [Name] obtained kk pieces of information, with values from C1C_1 to CkC_k.
  • 2 Name bb : Hoseok buys bb pieces of information from the gorilla named [Name]. He buys the bb most valuable pieces among the information the gorilla has, and if the gorilla has bb 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 QQ.

Then QQ 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 kk, followed by the kk information values C1,...,CkC_1, ..., C_k as natural numbers. Every CiC_i 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 bb he wants to buy.

Output

Print the total value of the information Hoseok obtains after all queries have finished.

Constraints

  • 1 ≤ QQ ≤ 100,000, QQ 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 ≤ kk ≤ 100,000, kk is a natural number
  • 1 ≤ CC ≤ 100,000, CC is a natural number
  • 1 ≤ bb ≤ 100,000, bb is a natural number
  • The sum of kk over all queries does not exceed 1,000,000.

Examples1

  1. Example 1

    Input
    7
    1 Cpp 5 10 4 2 8 4
    1 Java 2 8 2
    2 Cpp 2
    1 Cpp 2 10 3
    2 Cpp 3
    2 Java 1
    2 Python 10
    
    Expected output
    44