This page is still under construction.

Parts of this page are still being built. What you see may change.

Fixing Disks

Time limit2sMemory limit512 MB

Summary
Given a master stack and your own stack of N labeled disks, use three limited reorder moves on the top K disks to remove disks cheaply; minimize total cost under a removal-order constraint.
Level

Hard8 of 10

Topics
Dynamic programming, Stack, Bit manipulation, Brute force
Solved
No attempts yet

Problem

You are playing a game with a stack of NN disks. The goal is to remove every disk from your stack. Each removal has a cost, and you want the total cost to be as small as possible.

Every disk carries a label LL, and 1≤L≤201 \le L \le 20.

You also get a Master stack of NN disks to help you clear your own stack.

There are two ways to remove the disk on top of your stack.

  1. Remove the top disk of your stack on its own. If its label is cc, the removal costs cc.
  2. Remove the top disk of the Master stack together with the top disk of your stack. This is allowed only when the two labels are equal, and it costs nothing.

You may also reorder the top KK disks of your stack. Immediately after each reorder you must remove the top disk, so one removal is preceded by at most one reorder. A reorder applies to a range of disks at the top of your stack, and the size of that range cannot exceed the number of disks still on the stack. Three reorders are allowed.

  1. Reverse. Reverse the order of the top rr disks (2≤r≤K2 \le r \le K). If those disks are d1,d2,…,drd_1, d_2, \ldots, d_r read from the top down, then after the operation they read dr,…,d2,d1d_r, \ldots, d_2, d_1 from the top down. One reverse costs RR.
  2. Cyclic shift up. Shift up by one inside the top uu disks (2≤u≤K2 \le u \le K). If the top four disks read d1,d2,d3,d4d_1, d_2, d_3, d_4 from the top down, an up shift over the top three gives d2,d3,d1,d4d_2, d_3, d_1, d_4, and an up shift over all four gives d2,d3,d4,d1d_2, d_3, d_4, d_1. One up shift costs UU.
  3. Cyclic shift down. Shift down by one inside the top dd disks (2≤d≤K2 \le d \le K). If the top four disks read d1,d2,d3,d4d_1, d_2, d_3, d_4 from the top down, a down shift over the top three gives d3,d1,d2,d4d_3, d_1, d_2, d_4, and a down shift over all four gives d4,d1,d2,d3d_4, d_1, d_2, d_3. One down shift costs DD.

If the reorder leaves the top of the Master stack and the top of your stack carrying the same label, the pair comes off for free. Rule 1 is still available in that case, so you may instead pay the label and remove only your own disk. When the labels differ, rule 1 is the only option.

One more rule constrains the order of removals. Number the levels of your stack starting at 00 for the bottom. When the disk that started at level jj is removed, every disk that started at level j+Mj + M or higher must already be gone.

Find the minimum total cost of emptying your stack.

Input

The first line contains six space separated integers NN, KK, MM, DD, UU, RR.

  • NN (1≤N≤1001 \le N \le 100) is the number of disks in each stack.
  • KK (1≤K≤41 \le K \le 4) is the greatest depth a reorder can reach.
  • MM (1≤M≤51 \le M \le 5) is the threshold used by the removal order rule.
  • DD (1≤D≤1061 \le D \le 10^6) is the cost of a down shift, which brings the bottom disk of the chosen range to the top.
  • UU (1≤U≤1061 \le U \le 10^6) is the cost of an up shift, which sends the top disk of the chosen range to the bottom of that range.
  • RR (1≤R≤1061 \le R \le 10^6) is the cost of reversing the chosen range.

Then 2N2N lines follow, each with one label LL (1≤L≤201 \le L \le 20). The first NN of them give the labels of the Master stack from top to bottom, and the remaining NN give the labels of your stack from top to bottom.

Output

Print one integer on a single line, the minimum cost of removing every disk from your stack.

Examples1

  1. Example 1

    Input
    7 3 3 4 4 3
    5
    6
    3
    5
    4
    1
    2
    3
    5
    6
    5
    1
    4
    1
    
    Expected output
    5