TLE '17 Contest 1 P2 - Willson and Food

View as PDF

Submit solution


Points: 5 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type
Pizza is one of Willson's favourite foods.

Willson the Canada Goose is like any other Canada goose - he likes to eat grass. But, as anyone knows, there are different types of grass (e.g. green, blue, white, corn). Additionally, some humans like to feed geese with other foodstuffs (e.g. bread, pizza, hot dogs).

Each food type can provide energy to Willson or drain it away if it's unhealthy. Willson's energy can become negative. Willson must use up one unit of energy in order to move one meter. Willson cannot move if he has zero or negative energy, but he can still eat. You know the energy values of F food types - the ith food type is named si and has an energy value of ei.

Willson sees N food items in front of him. The food type of jth item is tj and the item is located dj meters away from Willson's initial location. Willson will eat any food item that he encounters.

How many food items can Willson eat?

Input Specification

The first line of input will contain F (1F103).

F lines of input will follow. The ith line will contain a string si and an integer ei. All si are pairwise unique.

The next line of input will contain N (1N103).

N lines of input will follow. The jth line will contain a string tj and an integer dj (0dj109). It is guaranteed that tj is equal to some si.

All strings will be at most 10 characters long and will only contain lowercase English letters.

It is guaranteed that N×max|ei|109.

Output Specification

Output a single integer, the number of food items that Willson can eat.

Sample Input

Copy
3
grass 5
bread 10
metal -100
5
bread 5
grass 0
grass 6
bread 10
metal 9

Sample Output

Copy
4

Explanation for Sample Output

Willson first eats the 2nd food item, providing him 5 energy. Just as he runs out of energy, he encounters the 1st food item, which provides him 10 energy. He passes the 3rd food item, providing him 5 more energy. However, he eats the 5th food item, which completely drains all of his energy and leaves him unable to move.


Comments

There are no comments at the moment.