Under construction

Still being built. Things may change

Gotta Nudge 'Em All

Time limit3sMemory limit512 MB

Summary
Choose one 30-minute window for a double-XP bonus, catch every Nudgemon already logged, and maximize XP from evolutions inside that window.
Level

Hard8 of 10

Topics
Greedy, Sliding window, Sorting
Solved
No attempts yet

Problem

Nudgémon GO is a game in which players try to earn as many experience points (XP) as possible by catching and evolving Nudgémon. You gain 100 XP for catching a Nudgémon and 500 XP for evolving a Nudgémon. Your friend has been playing this game a lot recently, but you believe that his strategy is not optimal.

All Nudgémon are split into families, and each family has its own unique type of candy. The Nudgémon in a family are ranked from weakest to strongest and form a chain. Any Nudgémon that is not the strongest in its family can be evolved into the next ranked Nudgémon of the same family.

Candies are the basic currency in the Nudgémon universe:

  • When you catch a Nudgémon, you earn 3 candies of that Nudgémon's family.
  • When you irreversibly transfer a Nudgémon away from your possession, you earn 1 candy of that Nudgémon's family.

Every evolution of a Nudgémon consumes a specific amount of its family's candy. The costs of evolutions along the family chain are non-decreasing: a higher-ranked evolution in a family costs the same as or more than a lower-ranked one.

Here is an example of possible Nudgémon evolutions:

Example of Nudgémon evolutions

Apart from making the developers money and nudging 'em all, the goal of this game is to earn as much XP as possible to level up the player's character and encounter stronger Nudgémon in the wild. In line with the first goal, you can buy a Blessed Egg with real money in the game. This item doubles your earned XP for the next 30 minutes after activation. That is, when the Egg is activated at time ee (in seconds since the start of the game), any action taken at time tt earns double XP if and only if e≤t<e+1800e \le t < e + 1800.

At the start of the game your friend received a single Blessed Egg. Unfortunately, he completely wasted it. You believe that it is better to evolve Nudgémon only while the Blessed Egg is active, since anything else is a huge waste of resources. To prove your point, you took a log of all Nudgémon your friend caught, with timestamps, and decided to calculate the maximum amount of XP he could have had right now if he had chosen the activation time of his Blessed Egg strategically and evolved Nudgémon only while it was active.

Input

The input consists of:

  • one line containing an integer ff (0≤f≤1050 \le f \le 10^5), the number of Nudgémon families;
  • ff lines, each describing a family of Nudgémon, where each line consists of the following elements:
    • an integer sis_i (1≤si≤1051 \le s_i \le 10^5), the number of Nudgémon in this family;
    • si−1s_i - 1 times the name of a Nudgémon followed by an integer cjc_j (1≤cj≤1051 \le c_j \le 10^5), the number of candies (of the matching type) consumed by evolving this Nudgémon;
    • the name of the strongest Nudgémon in this family;
  • one line containing an integer nn (0≤n≤4×1050 \le n \le 4 \times 10^5), the number of Nudgémon your friend caught;
  • nn lines, each containing an integer tit_i (0≤ti≤1090 \le t_i \le 10^9) and a string pip_i: the time at which the Nudgémon was caught and the name of the caught Nudgémon.

There are at most 10510^5 kinds of Nudgémon (∑isi≤105\sum_i s_i \le 10^5). The Nudgémon in each family are given in order of increasing rank, so the values of cc within one family are non-decreasing. Every Nudgémon name is a string of 1 to 20 lowercase letters. The times tit_i are non-decreasing (your friend is so quick that he can catch several Nudgémon in a single second). No Nudgémon name appears more than once within a family or in more than one family, and all nn caught Nudgémon belong to one of the families.

Output

Output the maximum amount of XP your friend could have had at the current time if he had activated his Blessed Egg at the optimal time and evolved Nudgémon only while it was active.

Examples2

  1. Example 1

    Input
    3
    3 caterpillar 3 pupa 7 butterfly
    3 dove 3 pigeon 7 aaabaaajss
    3 mouse 1 electromouse 5 rat
    7
    0 electromouse
    500 electromouse
    1000 electromouse
    1500 rat
    2000 aaabaaajss
    2500 pigeon
    3000 butterfly
    
    Expected output
    5100
    
  2. Example 2

    Input
    1
    1 slownudge
    2
    0 slownudge
    1800 slownudge
    
    Expected output
    300