luogu#P1977. 出租车拼车

出租车拼车

Background

Once, little x went to a contest. Although the school was not far from the venue, little x still wanted to take a taxi. Taxis in the university town are a bit quirky: they do “carpooling.” That is, whether you ride alone or as a group, the total fare is the same per taxi ride (each taxi can seat at most 44 passengers excluding the driver). Coincidentally, a group of OIers from the same school were gathered at the school gate that day, and everyone decisively decided to carpool to the venue.

Here comes the problem: taxis kept passing by, but they were either full or had only one or two seats left. The OIers felt that getting on those would be a bad deal, and little x thought so too.

Problem Description

Suppose NN OIers plan to carpool. Time 00 is the current moment. The fare from the school gate to the destination is DD yuan, charged per taxi ride regardless of how many OIers are inside. Assume the contest starts exactly at SS minutes, so any taxi that passes the gate after SS minutes can be ignored. You are given the arrival times TiT_i and remaining seats ZiZ_i of all KK taxis that pass the gate within these SS minutes. For each taxi, the OIers may choose to let up to ZiZ_i people board (possibly 00), or skip this taxi entirely.

Time is money: each OIer’s waiting time in minutes at the gate is counted as spending the same amount of money. For example, if little x waits 2020 minutes, that means he spends an extra 2020 yuan.

Under the requirement that all OIers can reach the venue before the contest starts, compute the minimum total amount of money they need to spend.

Input Format

Each test case starts with four integers NN, KK, DD, SS, as described above.

Then follow KK lines. The ii-th line gives that the ii-th taxi arrives at minute TiT_i, and its number of remaining seats is ZiZ_i.

Times are given in chronological order.

Output Format

For each test case, output one line. If all of them can reach the venue before the contest starts, output a single integer representing the minimum amount of money (in yuan). Otherwise, output impossible.

2 2 10 5
1 1
2 2

14

Hint

Constraints: For 100%100\% of the testdata, N,K,D,S100N, K, D, S \le 100, 1Zi41 \le Z_i \le 4, 1TiTi+1S1 \le T_i \le T_{i+1} \le S.

Translated by ChatGPT 5