Gotta Nudge 'Em All
Time limit3sMemory limit512 MB
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:

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 (in seconds since the start of the game), any action taken at time earns double XP if and only if .
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 (), the number of Nudgémon families;
- lines, each describing a family of Nudgémon, where each line consists of the following elements:
- an integer (), the number of Nudgémon in this family;
- times the name of a Nudgémon followed by an integer (), 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 (), the number of Nudgémon your friend caught;
- lines, each containing an integer () and a string : the time at which the Nudgémon was caught and the name of the caught Nudgémon.
There are at most kinds of Nudgémon (). The Nudgémon in each family are given in order of increasing rank, so the values of within one family are non-decreasing. Every Nudgémon name is a string of 1 to 20 lowercase letters. The times 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 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.