Exciting Menus
Time limit4sMemory limit512 MB
Given N strings with a joy value per position, maximize over all substrings the product of its length, the joy at its end, and the number of strings having it as a prefix.
- Level
Hard8 of 10
- Topics
- Trie, String, Array, Implementation
- Solved
- No attempts yet
Problem
A restaurant has menus, given as the strings . Each string comes with an array of joy levels of the same length. In this problem you choose a submenu defined by three integers , where is the index of a menu and specify a substring of that menu.
Find the submenu of the highest quality, where the quality is defined by
- = the substring of the string (with one-based and inclusive).
- popularity() = the number of menus among that contain the submenu string as a prefix.
- is the size of the submenu, the length of the substring that represents it.
- is the joy level of the submenu (the -th entry of the -th array). Note that the index is not used here.
Can you find the submenu of the highest quality? Output the maximum value of the function .
Input
The first line of the input contains a single integer , the number of test cases.
Each test case begins with a line containing an integer (), the number of menus in the restaurant.
Then lines follow. The -th line contains a string representing the -th menu. All menus are nonempty strings of lowercase English letters, and the sum of their lengths does not exceed per test case.
Then lines follow. The -th line contains the array , which has the same length as the string , with . The values of each array are space-separated.
Output
For each test case, print a single line containing an integer , the maximum quality over the best submenu .
Hint
The triples corresponding to the answers of the two given cases are and .