Under construction

Still being built. Things may change

Text Editor

Time limit3sMemory limit256 MB

Summary
Find the cheapest sequence of cursor moves, Shift selections, cuts, and pastes that turns the initial line order into the target order.
Level

Medium6 of 10

Topics
Shortest path, Brute force, Simulation
Solved
No attempts yet

Problem

When using a simple text editor, the user often needs to reorder lines in a specific way. When the lines are long and errors must be kept to a minimum, the easiest method is to move chunks of text from one place to another (the "cut/paste" method). You are an efficient programmer. Determine the minimum time needed to shuffle the lines into the desired order.

The file contains NN lines of text. The editor has a single cursor with N+1N+1 possible positions: before the first line, after the last line, and between any two adjacent lines. Pressing "up" or "down" moves the cursor one line up or down. The cursor cannot move outside the text.

The Shift key selects lines. When Shift is pressed, the editor remembers the current cursor position. When Shift is released, the editor selects all lines between the current cursor position and the remembered one. After releasing Shift, press Ctrl+X to cut the selected lines. They are copied to the clipboard and removed from the text. To paste the cut fragment back, move the cursor to the desired position with "up" and "down", then press Ctrl+V. After pasting, the fragment is removed from the clipboard.

When a fragment is cut, all following lines move up. When it is pasted, they move down. The text never contains empty lines. After a cut, the cursor moves up to the line where the cut fragment began, unless it is already there. After a paste, the cursor moves down and ends up right after the inserted text.

Every action takes a predefined time that depends on user skill. Initially, the cursor is before the first line of the text.

Input

The first line contains one integer NN, the number of lines in the text editor (2≤N≤82 \le N \le 8). The second line contains six integers t1,t2,…,t6t_1, t_2, \ldots, t_6, the time in milliseconds of each action (1≤ti≤1001 \le t_i \le 100). The third line contains NN distinct integers from 1 to NN, the initial order of the lines. The fourth line contains NN distinct integers from 1 to NN, the order in which the lines must be placed at the end.

Output

Print two integers on the first line: TT, the total time of the plan in milliseconds, and KK, the number of actions in the plan. Then print the KK actions in execution order, one per line:

  • Up: press "up" (takes t1t_1 milliseconds).
  • Down: press "down" (takes t2t_2 milliseconds).
  • Shift-Press: press and hold Shift (takes t3t_3 milliseconds).
  • Shift-Release: release Shift (takes t4t_4 milliseconds).
  • Ctrl+X: press the combination Ctrl+X (takes t5t_5 milliseconds).
  • Ctrl+V: press the combination Ctrl+V (takes t6t_6 milliseconds).

Hint

The example is a reversal of six lines. Every action takes 100 milliseconds or a bit less. The optimal plan is shown in an animated gif image, which can be downloaded near these problem statements.

Examples1

  1. Example 1

    Input
    6
    99 98 100 97 99 98
    1 2 3 4 5 6
    6 5 4 3 2 1
    
    Expected output
    3252 33
    Shift-Press
    Down
    Down
    Shift-Release
    Ctrl+X
    Down
    Down
    Ctrl+V
    Shift-Press
    Down
    Shift-Release
    Ctrl+X
    Down
    Ctrl+V
    Shift-Press
    Up
    Up
    Up
    Shift-Release
    Ctrl+X
    Up
    Up
    Ctrl+V
    Down
    Shift-Press
    Up
    Up
    Up
    Shift-Release
    Ctrl+X
    Up
    Up
    Ctrl+V