You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P'pairus, who decided to accompany you and give you some puzzles for seemingly unknown reasons.
One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given a string of length and words, none of which is longer than . You are also given an array which designates how much each word is worth – the word is worth points.
Whenever you find one of the words in the string, you are given the corresponding number of points. Each letter in the crossword can be used at most times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you haven't used the letters more than times).
In order to solve the puzzle, you need to tell Sanz what's the maximum achievable number of points in the crossword.
Input Specification
The first line of input will contain one integer – the length of the crossword, and the second line will contain the crossword string. The third line will contain the integer – the number of given words, and the next lines will contain descriptions of words: each line will have a word string and an integer . The last line of the input will contain – the maximal number of times a position in the crossword can be used.
Output Specification
Output a single integer – the maximal number of points you can get.
Constraints
Sample Input
6
abacba
2
aba 6
ba 3
3
Sample Output
12
Explanation
For example, with the string abacba
, words aba
(6 points) and ba
(3 points), and , you can get at most points - the word aba
appears once (abacba
), while ba
appears two times (abacba
). Note that for , you could get at most points, since you wouldn't be able to count both aba
and the first appearance of ba
.
Comments
This comment is hidden due to too much negative feedback. Show it anyway.
OMG THIS IS A REFRENCE TO THE GAME UNDERTALE
who would've known 🤔