PivotOJ

BnPC

시간 제한: 1000ms메모리 제한: 1024MB출처: BAPC 2021BOJ 23376

문제

You are playing your favorite game, Basements and Pigeonlike Creatures, for the umpteenth time. You know the game pretty well, but you have never spent enough time on it to figure out the best strategy. That is, until now. The game consists of a certain sequence of events, such as battling a monster or saving a cat from a tree, and you need to complete all events to win. Attached to each event is an attribute, such as strength, and a threshold, some positive integer. If your attribute score matches or exceeds the threshold, you successfully complete the event! If not, it is unfortunately game over and your total score will be zero.

If you complete all the events successfully, your score depends on how well you did during these events. If your attribute score matches the threshold of an event exactly, you get 0 points, barely scraping by that event. If you exceed the threshold, you get points equal to your attribute score that was used for that event.

You are now at the final part of the game, but first you have some attribute points to spend to increase your attribute scores. You know what events will happen during the final part of the game, so all that is left is to figure out what attributes to increase.

입력

The input consists of:

  • One line containing an integer nn (1n1051 \leq n \leq 10^5) and an integer kk (1k1091 \leq k \leq 10^9), the number of attributes and the number of attribute points you can still spend.
  • nn lines, each containing a distinct attribute name, and an integer ss (0s1090 \leq s \leq 10^9), the current score you have in that attribute.
  • One line containing an integer ll (1l1051 \leq l \leq 10^5), the number of events.
  • ll lines each describing one event, containing the name of the attribute that is used, and an integer tt (0t1090 \leq t \leq 10^9), the threshold for this event.

Attribute names consist of upper case English letters (A-Z), and have a length between 11 and 2020 characters inclusive.

출력

Output the maximum score you can get from the events.

예제

예제 1

입력
2 3
STR 15
CON 12
2
STR 17
CON 14
출력
0

예제 2

입력
3 7
JUMP 5
RUN 7
FLY 0
4
FLY 0
JUMP 6
RUN 10
RUN 8
출력
31
코드를 제출하려면 로그인하세요.