Under construction

Still being built. Things may change

Finding the password

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN-digit integer in which each digit from 11 to 99 appears at most 11 time. Each time Busil enters a password, it takes XX seconds. Whenever he enters three passwords in a row and all three fail, he cannot enter a password for YY seconds. After waiting YY 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 NN and the number MM of pieces of information Busil remembers, separated by a space. (3≤N≤93 \le N \le 9, 0≤M≤N0 \le M \le N)

The second line contains the integer XX, the time to enter a password, and the integer YY, the time during which passwords cannot be entered, separated by a space. (1≤X,Y≤101 \le X, Y \le 10)

From the third line onward, the next MM lines each contain integers aa and bb, separated by a space. (0≤a≤N0 \le a \le N, 1≤b≤91 \le b \le 9)

  • If a≠0a \neq 0, the digit at position aa of the password is bb.
  • If a=0a = 0, one of the digits of the password is bb.

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.

Examples2

  1. Example 1

    Input
    4 4
    3 10
    1 2
    0 1
    0 3
    4 8
    
    Expected output
    6
    
  2. Example 2

    Input
    4 0
    3 10
    
    Expected output
    19142