Finding the password
InterviewTime limit1sMemory limit1024 MB
Given the known digits and positions, find the longest time to open the lock when every candidate is tried once and each run of three failures forces a Y-second wait.
- Level
Easy3 of 10
- Topics
- Brute force, Backtracking, Simulation
- Solved
- No attempts yet
Problem
Busil wants to get into the house, but he has forgotten the password for the digital door lock. The password is an -digit integer in which each digit from to appears at most time. Each time Busil enters a password, it takes seconds. Whenever he enters three passwords in a row and all three fail, he cannot enter a password for seconds. After waiting seconds, the count of failed attempts resets.
Busil wants to get inside as quickly as possible. After some thought, he remembered part of the password. Using this information, he decided to try every possible password once on the door lock, which currently has zero failed attempts. Find the maximum time it takes Busil to get into the house.
Input
The first line contains the password length and the number of pieces of information Busil remembers, separated by a space. (, )
The second line contains the integer , the time to enter a password, and the integer , the time during which passwords cannot be entered, separated by a space. ()
From the third line onward, the next lines each contain integers and , separated by a space. (, )
- If , the digit at position of the password is .
- If , one of the digits of the password is .
No position or digit appears in more than one piece of information.
Output
Print the maximum time, in seconds, that it takes Busil to get into the house using the information he remembers.