Gifts
Difficulty: Hard

Shaina has given to her student, Koga, a bunch of scrolls, some instructions to remember while learning the techniques described in the scrolls, and some stones that will multiply the power of the techniques. The instructions are as follows:  

1.      He has to master all the Bronze, Silver, and Gold Saints techniques.

2.      He cannot learn them in just any random order, but can switch among Bronze, Silver, and Gold. Within each type, only outer techniques will make him powerful enough to learn and master inner techniques. That is, if he sees the scrolls of some type as a list, you could never learn from the $i^{th}$, if neither $(i + 1)^{th}$ nor $(i - 1)^{th}$ scroll has been mastered yet.  

3.      And lastly, every time he learns a technique, whichever type it is, he has to use one of these stones, respecting the order they currently have.
    
You have to help him figure out the specific learning order that will get Koga the largest possible overall power.


Input Format:
Input begins with a line containing three integers, $B$, $S$, and $G$, separated by a single white space, denoting we have $B$, $S$, and $G$ bronze, silver, and gold scrolls, respectively. $B + S + G$ lines follow, each one with some integer saying the power of some technique: first $B$ belong to bronze's, next $S$ belong to silver's and last $G$ belong to gold's. Scrolls for each type are arranged in the same order these integers are given in the input. The input ends with $B + S + G$ lines, each having a single integer describing the multiplication power of some stone. The input order matches the one in which Koga will receive the stones.

**Constraints**  
 
$1 \le B, S, G \le 10$

$1 \le B_i, S_i, G_i \le 10^5$

$1 \le i^{th} stone \le 300$

All input numbers are positive.

Output Format:
Output just one line with a single integer: the largest possible sum you can get from learning the techniques with the restrictions mentioned above. **Remember that you must pick the outer techniques from each type first and, yes, don't forget to properly use the stones :)**

The output will always fit in a **32-bit signed integer**.

Sample Input:
    2 2 2
    4
    1
    2
    3
    5
    6
    1
    2
    3
    4
    5
    6 

Sample Output:
	91

Python 3 starter code:


Reference solution (C++):
	//============================================================================
	// Name        : GIFTS.cpp
	// Author      : Shaka
	// Version     :
	// Copyright   : Your copyright notice
	// Description : Hello World in C++, Ansi-style
	//============================================================================

	#include <cstdio>
	#include <cassert>
	using namespace std;
	const int MAXN = 21;
	int dp[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN];
	int A[MAXN], B[MAXN], C[MAXN];
	int weight[MAXN * 3];
	int N, M, L;
	int solve(int a, int b, int c, int d, int e, int f) {
		if (a > b && c > d && e > f) return 0; //no more scrolls to learn
		if (!dp[a][b][c][d][e][f]) { //all values are positive, so 0 means unvisited state in the dp
			int &rx = dp[a][b][c][d][e][f], tmp;
			int wx = weight[(N - 1 + a - b) + (M - 1 + c - d) + (L - 1 + e - f)];
			//possible transitions in bronze techniques
			if (a <= b) {
				tmp = wx * A[a] + solve(a + 1, b, c, d, e, f);
				if (tmp > rx) rx = tmp;
			}
			if (a < b) {
				tmp = wx * A[b] + solve(a, b - 1, c, d, e, f);
				if (tmp > rx) rx = tmp;
			}

			//possible transitions in silver techniques
			if (c <= d) {
				tmp = wx * B[c] + solve(a, b, c + 1, d, e, f);
				if (tmp > rx) rx = tmp;
			}
			if (c < d) {
				tmp = wx * B[d] + solve(a, b, c, d - 1, e, f);
				if (tmp > rx) rx = tmp;
			}

			//possible transitions in gold techniques
			if (e <= f) {
				tmp = wx * C[e] + solve(a, b, c, d, e + 1, f);
				if (tmp > rx) rx = tmp;
			}
			if (e < f) {
				tmp = wx * C[f] + solve(a, b, c, d, e, f - 1);
				if (tmp > rx) rx = tmp;
			}
		}
		return dp[a][b][c][d][e][f];
	}
	
	int main() {
		register int i;
		scanf("%d%d%d", &N, &M, &L);
		for (i = 0; i < N; ++i) scanf("%d", A + i);
		for (i = 0; i < M; ++i) scanf("%d", B + i);
		for (i = 0; i < L; ++i) scanf("%d", C + i);
		for (i = 0; i < N + M + L; ++i) scanf("%d", weight + i);
		printf("%d\n", solve(0, N - 1, 0, M - 1, 0, L - 1));
		return 0;
	}