Under construction

Still being built. Things may change

Visiting Singapore

Time limit2sMemory limit256 MB

Summary
Choose which days to attend so the events match T in order, paying penalties for skipped events and long idle gaps, to maximize total happiness.
Level

Medium6 of 10

Topics
Dynamic programming, String matching
Solved
No attempts yet

Problem

Singapore holds one event per day. The set of possible events is Σ={1,2,…,K}\Sigma = \{1, 2, \ldots, K\}. Attending event ii increases your happiness by V[i]V[i]. Let S[1],…,S[n]S[1], \ldots, S[n] be the list of events held on the nn days, in order. The same event may appear several times in this list.

You want to attend the events T[1],…,T[m]T[1], \ldots, T[m] in this order. The same event may also appear several times in TT. You may fly to Singapore on day ii and leave on day jj. You may also skip the trip entirely.

Attending T[i]T[i] increases your happiness by V[T[i]]V[T[i]]. Skipping T[p],T[p+1],…,T[q]T[p], T[p+1], \ldots, T[q] reduces your happiness by A+(q−p+1)BA + (q-p+1)B.

If you go a run of dd consecutive days during your stay without attending any event, your happiness is reduced by A+dBA + dB. Formally, if you attend S[p]S[p] and S[q]S[q] with p+2≤qp+2 \le q and attend nothing in between, your happiness is reduced by A+(q−p−1)BA + (q-p-1)B.

Find the maximum happiness you can get.

Input

The first line contains five integers KK, nn, mm, AA, and BB. KK, nn, and mm are positive integers. AA and BB are negative integers.

The second line contains KK positive integers V[1],…,V[K]V[1], \ldots, V[K].

The third line contains nn integers S[1],…,S[n]S[1], \ldots, S[n]. Each is between 1 and KK.

The fourth line contains mm integers T[1],…,T[m]T[1], \ldots, T[m]. Each is between 1 and KK.

Output

Print a single integer on one line: the total happiness of an optimal schedule.

Constraints

  • 1≤K≤10001 \le K \le 1000
  • 1≤n,m≤50001 \le n, m \le 5000
  • −100≤A,B≤0-100 \le A, B \le 0
  • 1≤V[i]≤1001 \le V[i] \le 100 for all ii.

Examples5

  1. Example 1

    Input
    1 5 3 -5 -4
    10
    1 1 1 1 1
    1 1 1
    
    Expected output
    30
    
  2. Example 2

    Input
    1 3 5 -10 -5
    10
    1 1 1
    1 1 1 1 1
    
    Expected output
    10
    
  3. Example 3

    Input
    4 7 4 0 0
    1 2 3 4
    3 1 2 1 4 1 1
    1 2 3 4
    
    Expected output
    7
    
  4. Example 4

    Input
    4 8 4 0 -3
    1 2 3 4
    3 1 2 1 1 4 1 1
    1 2 3 4
    
    Expected output
    -1
    
  5. Example 5

    Input
    4 8 4 -3 0
    1 2 3 4
    3 1 2 1 1 4 1 1
    1 2 3 4
    
    Expected output
    2