#P15887. [ICPC 2026 NAC] Maki Conveyor Belt

[ICPC 2026 NAC] Maki Conveyor Belt

Description

Alice and Bob are eating at a conveyor belt maki restaurant. (Maki is a kind of sushi). Diners in the restaurant sit around a circular conveyor belt with NN positions numbered from 11 to NN in clockwise order. Alice sits at position pAp_A and Bob sits at position pBp_B.

The restaurant serves MM different kinds of maki. There are KK different plates set out on the conveyor belt. The ithi^\text{th} plate consists of xix_i pieces of a single kind of maki and each piece costs cic_i coins. The same kind of maki may appear on multiple plates, and have different costs on different plates. No more plates will be added beyond what is already on the conveyor belt and no other customers are present at the restaurant (perhaps they picked a poorly rated maki restaurant\ldots).

There is at most one plate per position. Every second, the conveyor belt rotates clockwise. Formally, if there is a plate at position NN, it moves to position 11; and all other plates move from position ii to position i+1i+1. When a plate is in front of a diner, they may immediately grab any number of pieces from it, or choose not to grab any. For example, if there is a plate with 55 pieces of maki in front of Alice, she can choose to only grab 22. The diners may grab maki from plates in front of them before the belt first rotates.

Alice and Bob want to return home as quickly as possible to watch their favorite sushi documentary. They know the full layout of the restaurant, and each have a desired number of pieces of each maki type they want to eat in order to be satisfied. Help them determine the minimum time (in seconds) they need to spend in the restaurant and the minimum cost (in coins) for them to become satisfied within that time.

:::align{center}

Initial position of Alice, Bob, and the plates in Sample Input 1. :::

Input Format

The first line of input contains five space-separated integers NN, MM, KK, pAp_A, and pBp_B, where NN (2N109)(2 \leq N \leq 10^{9}) is the number of conveyor belt positions, MM (1M105)(1 \leq M \leq 10^{5}) is the number of maki types, KK (1Kmin(2105,N))(1 \leq K \leq \min(2 \cdot 10^{5}, N)) is the number of plates, and pAp_A and pBp_B (1pA,pBN,pApB)(1 \leq p_A, p_B \leq N, p_A \not= p_B) are Alice's and Bob's positions respectively.

The second line contains MM space-separated integers aia_i (0ai106)(0 \leq a_i \leq 10^6), where aia_i is the number of pieces of maki of type ii that Alice wants to eat.

The third line contains MM space-separated integers bib_i (0bi106)(0 \leq b_i \leq 10^6), where bib_i is the number of pieces of maki of type ii that Bob wants to eat.

Each of the next KK lines describe a plate. The jthj^\text{th} line contains four space-separated integers sjs_j, tjt_j, xjx_j, and cjc_j, where sjs_j (1sjN)(1 \leq s_j \leq N) is the starting position of the plate, tjt_j (1tjM)(1 \leq t_j \leq M) is the type of maki on the plate, xjx_j (1xj106)(1 \leq x_j \leq 10^6) is the number of pieces on the plate, and cjc_j (1cj106)(1 \leq c_j \leq 10^6) is the cost per piece. All plates have different starting positions (all sjs_j are distinct).

Output Format

Print two integers: the minimum time in seconds that Alice and Bob will need to spend in the restaurant and the minimum cost in coins for them to become satisfied within that minimum time.

If it is impossible for both of them to ever be satisfied, print impossible\texttt{impossible}.

10 2 3 5 7
3 1
4 1
5 1 9 2
6 2 5 3
8 1 9 7
9 20
5 1 1 2 3
2
2
5 1 3 3
impossible