CF1846G.Rudolf and CodeVid-23

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

A new virus called "CodeVid-23" has spread among programmers. Rudolf, being a programmer, was not able to avoid it.

There are nn symptoms numbered from 11 to nn that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought mm medicines.

For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known.

After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy.

Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible.

输入格式

The first line contains a single integer tt (1t100)(1 \le t \le 100) — the number of test cases.

Then follow the descriptions of the test cases.

The first line of each test case contains two integers n,mn, m (1n10,1m103)(1 \le n \le 10, 1 \le m \le 10^3) — the number of symptoms and medicines, respectively.

The second line of each test case contains a string of length nn consisting of the characters 00 and 11 — the description of Rudolf's symptoms. If the ii -th character of the string is 11 , Rudolf has the ii -th symptom, otherwise he does not.

Then follow 3m3 \cdot m lines — the description of the medicines.

The first line of each medicine description contains an integer dd (1d103)(1 \le d \le 10^3) — the number of days the medicine needs to be taken.

The next two lines of the medicine description contain two strings of length nn , consisting of the characters 00 and 11 — the description of the symptoms it removes and the description of the side effects.

In the first of the two lines, 11 at position ii means that the medicine removes the ii -th symptom, and 00 otherwise.

In the second of the two lines, 11 at position ii means that the ii -th symptom appears after taking the medicine, and 00 otherwise.

Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects.

The sum of mm over all test cases does not exceed 10310^3 .

输出格式

For each test case, output a single integer on a separate line — the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output 1-1 .

输入输出样例

  • 输入#1

    4
    5 4
    10011
    3
    10000
    00110
    3
    00101
    00000
    3
    01010
    00100
    5
    11010
    00100
    4 1
    0000
    10
    1011
    0100
    2 2
    11
    2
    10
    01
    3
    01
    10
    2 3
    11
    3
    01
    10
    3
    10
    00
    4
    10
    01

    输出#1

    8
    0
    -1
    6

说明/提示

In the first example, we can first apply medicine number 44 , after which the symptoms will look like "00101". After that, medicine number 22 , then all symptoms will disappear, and the number of days will be 5+3=85 + 3 = 8 . Another option is to apply the medicines in the order 1,3,21, 3, 2 . In this case, all symptoms will also disappear, but the number of days will be 3+3+3=93 + 3 + 3 = 9 .

In the second example, there are no symptoms initially, so the treatment will take 00 days.

In the third example, there are no options to remove all symptoms.

首页