CF237E.Build String

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You desperately need to build some string tt . For that you've got nn more strings s1,s2,...,sns_{1},s_{2},...,s_{n} . To build string tt , you are allowed to perform exactly t|t| ( t|t| is the length of string tt ) operations on these strings. Each operation looks like that:

  1. choose any non-empty string from strings s1,s2,...,sns_{1},s_{2},...,s_{n} ;
  2. choose an arbitrary character from the chosen string and write it on a piece of paper;
  3. remove the chosen character from the chosen string.

Note that after you perform the described operation, the total number of characters in strings s1,s2,...,sns_{1},s_{2},...,s_{n} decreases by 1. We are assumed to build string tt , if the characters, written on the piece of paper, in the order of performed operations form string tt .

There are other limitations, though. For each string sis_{i} you know number aia_{i} — the maximum number of characters you are allowed to delete from string sis_{i} . You also know that each operation that results in deleting a character from string sis_{i} , costs ii rubles. That is, an operation on string s1s_{1} is the cheapest (it costs 11 ruble), and the operation on string sns_{n} is the most expensive one (it costs nn rubles).

Your task is to count the minimum amount of money (in rubles) you will need to build string tt by the given rules. Consider the cost of building string tt to be the sum of prices of the operations you use.

输入格式

The first line of the input contains string tt — the string that you need to build.

The second line contains a single integer nn (1<=n<=100)(1<=n<=100) — the number of strings to which you are allowed to apply the described operation. Each of the next nn lines contains a string and an integer. The ii -th line contains space-separated string sis_{i} and integer aia_{i} (0<=ai<=100)(0<=a_{i}<=100) . Number aia_{i} represents the maximum number of characters that can be deleted from string sis_{i} .

All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100100 characters.

输出格式

Print a single number — the minimum money (in rubles) you need in order to build string tt . If there is no solution, print -1.

输入输出样例

  • 输入#1

    bbaze
    3
    bzb 2
    aeb 3
    ba 10
    

    输出#1

    8
    
  • 输入#2

    abacaba
    4
    aba 2
    bcc 1
    caa 2
    bbb 5
    

    输出#2

    18
    
  • 输入#3

    xyz
    4
    axx 8
    za 1
    efg 4
    t 1
    

    输出#3

    -1
    

说明/提示

Notes to the samples:

In the first sample from the first string you should take characters "b" and "z" with price 11 ruble, from the second string characters "a", "e" и "b" with price 22 rubles. The price of the string tt in this case is 21+32=82·1+3·2=8 .

In the second sample from the first string you should take two characters "a" with price 11 ruble, from the second string character "c" with price 22 rubles, from the third string two characters "a" with price 33 rubles, from the fourth string two characters "b" with price 44 rubles. The price of the string tt in this case is 21+12+23+24=182·1+1·2+2·3+2·4=18 .

In the third sample the solution doesn't exist because there is no character "y" in given strings.

首页