## CCO '14 P5 - Early Exam Evacuation

View as PDF

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2014 Stage 2, Day 2, Problem 2

You find yourself writing an exam in a long, narrow auditorium – it has () rows, numbered from the front to the back, each of which has 6 seats, 3 on each side of the aisle running down the middle of the auditorium. Each seat is identified by its row number immediately followed by a letter from A to F, which indicates its position in that row (with seat A at the far left, and F at the far right). The aisle lies between seats C and D in each row. The auditorium also has two secure rooms – one at the front (in front of row 1), and one at the back (behind row ).

Every seat in the auditorium is initially occupied by exactly one exam-writer per seat. However, during the course of the exam, different exam writers decide that they have written all they can on the exam, and then would like to leave the auditorium, one after another. The -th exam writer is in seat ( is one of A F). When the exam writer leaves the auditorium, they must stay in one of the secure rooms until the end of the exam. Fortunately, both secure rooms can hold as many people as necessary.

Exam writers not only worry about writing exams, but they would like their lives to be as convenient as possible – as such, they're interested in working together to minimize the total inconvenience experienced by all of them. The inconvenience in a single exam-writer experiences is , where and are given constants (), is the number of different people passed on the way to the chosen secure room (explained below), and is the number of people already in that secure room before that exam-writer enters. Note that if an exam-writer does not leave their seat, their inconvenience is 0.

When walking from a seat to a secure room, one must first pass any other exam writers in the same row on the way to the aisle, and then any exam writers in seats adjacent to the aisle from that row to either row or (depending on which secure room is chosen), inclusive. Note that any vacant seats passed along the way don't count towards this value.

Can you help the poor exam writers make their lives as convenient as possible?

#### Input Specification

The first line contains four space-separated integers: (), the number of rows in the auditorium; (), the number of exam writers that leave early; , followed by (), the constants used in determining the inconvenience caused by an exam writer leaving early.

The next lines each contain one integer followed by one character, and , for in order, where and A F.

You can assume that for test cases worth 60% of the marks, .

#### Output Specification

Output the integer which is the minimum total inconvenience experience by all exam writers who leave early.

#### Sample Input

5 5 3 4
3E
1D
5C
1E
4A

#### Output for Sample Input

55

#### Explanation of Output for Sample Input

One optimal strategy is as follows. The first person to leave can go to the front secure room, passing six people along the way (in seats , and ) for an inconvenience of . The second person to leave can also go to the front secure room, passing only one person (in seat ) and finding one person already in the secure room, for an inconvenience of . The third person can go to the back secure room, passing one person for an inconvenience of . The fourth person can join the first two in the front secure room, passing only one person (as seat is vacant at that point) for an inconvenience of . Finally, the fifth person to leave can go to the back secure room, passing four people and meeting one person in the back secure room for an inconvenience of . The total inconvenience experience by the exam writers with this strategy is .