#P7701. [CCC 2014] 提前交卷

[CCC 2014] 提前交卷

Description

You are taking an exam in a narrow and long exam hall. The hall has nn rows, numbered from 11 to nn from front to back. Each row has 66 seats: 33 on the left and 33 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 11) and one at the very back (behind row nn).

Initially, every seat in the hall is occupied by exactly one student. However, during the exam, mm different students decide to leave the hall one by one after finishing all the problems they can do. Student ii sits at seat ricir_i c_i, where cic_i 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 Ax+ByAx+By, where A,BA,B are constants, xx is the number of students they pass on the way to the waiting room (defined in detail below), and yy 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 00.

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 11 or row nn (depending on which waiting room they choose). Note that passing empty seats does not affect the value of xx.

Can you help them minimize the sum of their dissatisfaction?

Input Format

The first line contains four integers n,m,A,Bn,m,A,B, 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 mm lines each contain an integer rir_i and a letter cic_i, where 1in1\le i\le n.

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 66 students (3D, 3C, 2D, 2C, 1D, 1C), so the dissatisfaction is 3×6+4×0=183\times6+4\times0=18. The second student also goes to the front waiting room, passing only 11 student, 1C, and then finds that there is 11 student in the waiting room, so the dissatisfaction is 77. The third student goes to the back waiting room, passing 11 student, so the dissatisfaction is 33. The fourth student goes to the front waiting room, passing 11 student (because seat 1D is empty), so the dissatisfaction is 1111. Finally, the fifth student goes to the back waiting room, passes 44 students, finds that there is 11 person in the waiting room, so the dissatisfaction is 1616. The total dissatisfaction of all students is 5555.

Constraints: for 60%60\% of the testdata, 1m50001\le m\le5000.

For 100%100\% of the testdata, 1n1051\le n\le 10^5, 1m6n1\le m\le6n, 1A,B1091\le A,B\le 10^9.

Translated by ChatGPT 5