Annual Boat Challenge
Difficulty: Hard

The big day is approaching, the finals of the Annual Boat Challenge. 

The challenge consists of going down the river and earning the biggest number of points as possible. The circuit contains N checkpoints. It starts at the checkpoint number 0 and can finish at any checkpoint. At each checkpoint the
competitors have a set of options to take. There are some pathes to the left and others to the right, they lead 
the competitors to a next checkpoint with a bigger number.

The boats are always leaning to some side, to the left or to the right. If the competitor wants to take a next
checkpoint using a path to the right and the boat is currently leaning to the left, he must change the boat's
inclination for taking this way. The same happens if he wants to take the left way and the boat is leaning to the right.
If the sides match, there is no need to change the inclination.

The contest rules state that the competitors are able to change boats inclination at most K times. 

At the beginning, all the boats start leaning to the left.

The judges are interested in knowing how many points a competitor could make by taking wisely his path. 
  

**Input specification:**

The first line contains two integers N(1<=N<=300) and K(0<=K<=100). The second line contains N integers Pi(0<=Pi<=1000),
the points the competitors will earn when they visit checkpoint i. Next, follow 2 * (N-1) lines. Two lines for each
checkpoint i. (0<=i<N-1). The first of these two lines describes the pathes to the left. It starts with a
number L(0<=L<=N-i-1). Next, follow L integers Li(i<Li<N), the checkpoints are going to be reached following these
pathes. The second of these two lines describes the pathes to the right. It starts with a number R(0<=R<=N-i-1).
Next, follow R integers Ri(i<Ri<N), the checkpoints are going to be reached following these pathes.

**Output specification:**

Output just a simple line, the maximum score can be obtained. 


**Sample input**
<br>5 1
<br>421 990 160 473 333
<br>4 2 1 2 3
<br>0
<br>1 2
<br>2 4 4
<br>1 4
<br>2 4 4
<br>1 4
<br>0

**Sample output**
<br>1904

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Vector;


public class Main {
	static int N, K, L, R;
	static int points[];
	static Vector<Integer> G[][];
	static String cad[];
	static int DP[][][];
	
	static int solve(int x, int s, int kk) {
		if(kk > K)
			return 0;
						
		if(DP[x][s][kk] != -1)
			return DP[x][s][kk];
		
		int maxx = 0;
		
		for(int i = 0;i<G[x][0].size();i++) {
			int y = G[x][0].get(i);
			if(s == 0) {
				maxx = Math.max(maxx, solve(y, 0, kk));
			} else {
				maxx = Math.max(maxx, solve(y, 1, kk + 1));
			}
		}
		
		for(int i = 0;i<G[x][1].size();i++) {
			int y = G[x][1].get(i);
			if(s == 1) {
				maxx = Math.max(maxx, solve(y, 1, kk));
			} else {
				maxx = Math.max(maxx, solve(y, 0, kk + 1));
			}
		}
		
		return DP[x][s][kk] = maxx + points[x];
	}
	
	public static void main(String[] args) throws IOException {
		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(sr);
		
		cad = br.readLine().split(" ");
		
		N = Integer.parseInt(cad[0]);
		K = Integer.parseInt(cad[1]);
		
		DP = new int[N+1][2][K+1];
		
		for(int i = 0;i<N;i++)
			for(int j = 0;j<=K;j++) {
				DP[i][0][j] = -1;
				DP[i][1][j] = -1;
			}
				
		points = new int[N+1];
		
		cad = br.readLine().split(" ");
		
		G = new Vector[N+1][2];
		
		for (int i = 0; i < N; i++) {
			points[i] = Integer.parseInt(cad[i]);
			G[i][0] = new Vector<Integer>();
			G[i][1] = new Vector<Integer>();
		}	
		
		for(int i = 0;i<N-1;i++) {
			cad = br.readLine().split(" ");
			L = Integer.parseInt(cad[0]);			
				
			for(int j=1;j<=L;j++) {
				G[i][0].add( Integer.parseInt(cad[j]) );
			}
			
			cad = br.readLine().split(" ");
			R = Integer.parseInt(cad[0]);			
					
			for(int j=1;j<=R;j++) {
				G[i][1].add( Integer.parseInt(cad[j]) );
			}				
		}
		
		System.out.println(solve(0,0,0));
	}
}