Under construction

Still being built. Things may change

Algorithm study membership

Time limit2sMemory limit512 MB

Summary
Choose two algorithm types for each of N members in a mentor tree so every team (a node plus its children) can assign distinct types to its members, minimizing total teaching cost.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, Greedy, Bit manipulation
Solved
No attempts yet

Problem

An algorithm study group works through algorithm problems together. The group has N members, numbered 0 to N-1 in the order they joined.

Member 0 is the only member without a mentor. Every other member has exactly one mentor. The mentor of member i is member SiS_i, and S0=−1S_0 = -1 because member 0 has no mentor. A mentor must have joined the group before the member, so 0≤Si≤i−10 \le S_i \le i-1 for every i with 1≤i≤N−11 \le i \le N-1.

The organizer of the group decided to teach algorithms to all members. The group splits the algorithms used in problem solving into M types, numbered 1 to M. Teaching type i to one person costs TiT_i won. Each member has to take two classes, and nobody can take the same type twice. After the classes, a member can solve every problem that belongs to the two types they learned, and cannot solve any problem outside those two types.

Every member has to form one team with themselves as the leader, so N teams are formed. The team led by member i consists of member i and the members whose mentor is member i, that is, every j with Sj=iS_j = i. If Sk=jS_k = j and Sj=iS_j = i, then k does not belong to the team led by member i.

A team qualifies for a programming contest when the conditions below hold.

  • Every member of the team can solve problems.
  • When the team members each pick one algorithm type to solve, a state with no overlap must be possible.

Each team is checked on its own, so one person may pick different types in different teams. A team that qualifies is called a good team.

Write a program that finds the minimum cost of choosing the two types to teach each member so that every team is a good team.

Input

The first line contains N and M. (1≤N≤301 \le N \le 30, 2≤M≤302 \le M \le 30)

The second line contains N integers S0S_0 through SN−1S_{N-1} in order. (S0=−1S_0 = -1, and 0≤Si≤i−10 \le S_i \le i-1 for i with 1≤i≤N−11 \le i \le N-1)

The third line contains M integers T1T_1 through TMT_M in order. (1≤Ti≤1001 \le T_i \le 100)

Output

Print the minimum cost of teaching that makes every team a good team on the first line.

If no plan makes every team a good team, print -1.

Hint

In a team of k members, the k members have to pick k different types. So the types learned by those members must cover at least k different types, and beyond that, the members must be able to take one type each without any overlap.

A team with a single member is always a good team. If the group has only one member, teaching the two cheapest types costs the least.

Examples4

  1. Example 1

    Input
    1 2
    -1
    1 2
    
    Expected output
    3
    
  2. Example 2

    Input
    3 3
    -1 0 0
    1 2 3
    
    Expected output
    10
    
  3. Example 3

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

    Input
    29 15
    -1 0 0 2 2 2 1 1 6 0 5 4 11 10 3 6 11 7 0 2 13 14 2 10 9 11 22 10 3
    4 2 6 6 8 3 3 1 1 5 8 6 8 2 4
    
    Expected output
    71