**Compilation/Execution Result:**

Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.

Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.

**Input:**

First line consists of test cases T. First line of every test case consists of N, denoting the number of key. Second and Third line consists N spaced elements of keys and frequency respectively.

**Output:**

Print the most minimum optimal cost.

**Constraints:**

1<=T<=100

1<=N<=100

**Example:
Input:**

2

2

10 12

34 50

3

10 12 20

34 8 50

118

142

Author: saksham seth

Rajat Thakur 2 | 251 |

surbhi_7 | 231 |

Anupam Prakash 1 | 215 |

Ibrahim Nash | 205 |

Vikram Sreenivasa | 175 |

Ibrahim Nash | 719 |

ashrepos | 578 |

shi143din | 438 |

RealHorror | 435 |

HABIB_SHEIKH | 369 |

akhayrutdinov | 4059 |

sanjay05 | 3451 |

Quandray | 2475 |

Michael Riegger | 2234 |

Jasleen Kaur 2 | 2012 |