CCCGuide

Problem S2: Jerseys

2015 · S2 · Original problem · DMOJ

Problem Description

A school team is trying to assign jerseys numbered 1,2,3,…,J1,2,3,\ldots,J to student athletes. The size of each jersey is either small (S), medium (M) or large (L).

Each athlete has requested a specific jersey number and a preferred size. The athletes will not be satisfied with a jersey that is the wrong number or that is smaller than their preferred size. They will be satisfied with a jersey that is their preferred size or larger as long as it is the right number. Two students cannot be given the same jersey.

Your task is to determine the maximum number of requests that can be satisfied.

Input Specification

The first line of input is the integer JJ which is the number of jerseys.

The second line of input is the integer AA which is the number of athletes.

The next JJ lines are each the character S, M or L. Line jj gives the size of jersey jj (1≤j≤J)(1 \leq j \leq J).

The last AA lines are each the character S, M or L followed by a space followed by an integer. Line aa (1≤a≤A(1 \leq a \leq A) gives the requested size and jersey number for athlete aa where the athletes are numbered 1,2,3,…,A1,2,3,\ldots,A.

For 50% of the test cases, 1≤J≤1031 \leq J \leq 10^3 and 1≤A≤1031 \leq A \leq 10^3.

For the remaining 50% of the test cases, 1≤J≤1061 \leq J \leq 10^6 and 1≤A≤1061 \leq A \leq 10^6.

Output Specification

The output will consist of a single integer which is the maximum number of requests that can be satisfied.

Sample Input

4
3
M
S
S
L
L 3
S 3
L 1

Output for Sample Input

1

Explanation Sample Output

Jersey 1 cannot be assigned because it is medium and athlete 3 requested large. No athlete requested jersey 2 or 4. Jersey 3 (small) can be assigned athlete 2 (small) but not athlete 1 (large).