P1844 Reading Room

Description

A reading room receives a large number of readers every day. The opening time is $O$, and the closing time is $T$. Each reader arrives at a different time and wants to read at most $5$ publications. Each reader has an internal ranking of the publications they want to read. Upon arrival, the reader first looks for their most desired publication; if it is unavailable, they will look for the next one in their preference order. If none of their desired publications are available, they will start waiting until at least one of their desired publications is returned to its place. Of course, among the available ones, the reader will first take the publication they want the most. After finishing reading a publication, the reader returns it to its place and then looks for the most desired publication they have not yet read. This continues until they have finished all the publications they want to read. Conflicts arise when two people want to take the same publication at the same time. To avoid disputes, the reading room has a rule: each time a reader begins waiting, they must first register once at the service desk. If two people want the same publication at the same time, the reader who registered earlier gets the publication. If both registered at the same time, the reader who arrived at the reading room earlier gets it. The one who does not get it can only go look for other publications to read. When the reading room closes, all readers are forced to leave and are no longer allowed to continue reading. The reading room wants to conduct a statistical survey. You are required to write a program to simulate this process and compute the total number of times all publications were read. Whenever a reader starts reading a publication, the count for that publication increases by $1$, regardless of whether it is eventually finished.

Input Format

The input contains multiple test cases. Each test case begins with two integers $T$ and $n$ ($1 \le n \le 100$), representing the closing time of the reading room and the total number of readers, respectively. Next, the detailed descriptions of all readers are given in the order of their arrival times. Each reader’s description begins with an integer $t$ ($0 \le t < T$), indicating the reader’s arrival time. The next line begins with an integer $k$ ($1 \le k \le 5$), indicating the number of publications the reader wants to read. It is followed by $2k$ integers given in the order of the reader’s preferences. For each $i$, the $(2i-1)$-th integer is the publication ID $s$ ($0 \le s < 1000$), and the $2i$-th integer is the time required for the reader to finish that publication.

Output Format

For each test case, output on a separate line the total number of times all publications were read.

Explanation/Hint

Translated by ChatGPT 5