#P7701. [CCC 2014] 提前交卷
[CCC 2014] 提前交卷
Description
You are taking an exam in a narrow and long exam hall. The hall has rows, numbered from to from front to back. Each row has seats: on the left and on the right, with an aisle in the middle. Each seat is labeled with a letter from A to F: the leftmost seat is A, the rightmost seat is F, and the aisle is between seats C and D. The hall also has two waiting rooms: one at the very front (in front of row ) and one at the very back (behind row ).
Initially, every seat in the hall is occupied by exactly one student. However, during the exam, different students decide to leave the hall one by one after finishing all the problems they can do. Student sits at seat , where is one of the letters A to F. When a student leaves the hall, they must wait in one of the waiting rooms until the exam ends. Fortunately, the waiting rooms can hold any number of students.
Students not only care about the exam itself, but also about how to take the exam as comfortably as possible. Therefore, they cooperate to minimize the sum of their dissatisfaction. A student’s dissatisfaction is calculated as , where are constants, is the number of students they pass on the way to the waiting room (defined in detail below), and is the number of people already in that waiting room before the student enters it. Note that if a student does not leave their seat, then their dissatisfaction is .
When a student walks from their seat to a waiting room, they must first pass the students in the same row while moving toward the aisle, and then pass the students in the adjacent aisle seats from that row up to row or row (depending on which waiting room they choose). Note that passing empty seats does not affect the value of .
Can you help them minimize the sum of their dissatisfaction?
Input Format
The first line contains four integers , separated by spaces, representing the number of rows in the hall, the number of students who leave early, and the parameters in the dissatisfaction formula.
The next lines each contain an integer and a letter , where .
Output Format
Output one integer, the minimum possible total dissatisfaction.
5 5 3 4
3E
1D
5C
1E
4A
55
Hint
One optimal strategy is as follows. The first student who leaves early goes to the front waiting room, passing students (3D, 3C, 2D, 2C, 1D, 1C), so the dissatisfaction is . The second student also goes to the front waiting room, passing only student, 1C, and then finds that there is student in the waiting room, so the dissatisfaction is . The third student goes to the back waiting room, passing student, so the dissatisfaction is . The fourth student goes to the front waiting room, passing student (because seat 1D is empty), so the dissatisfaction is . Finally, the fifth student goes to the back waiting room, passes students, finds that there is person in the waiting room, so the dissatisfaction is . The total dissatisfaction of all students is .
Constraints: for of the testdata, .
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号