CF1733D2.Zero-One (Hard Version)

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

This is the hard version of this problem. In this version, n5000n \le 5000 holds, and this version has no restriction between xx and yy . You can make hacks only if both versions of the problem are solved.

You are given two binary strings aa and bb , both of length nn . You can do the following operation any number of times (possibly zero).

  • Select two indices ll and rr ( l<rl < r ).
  • Change ala_l to (1al)(1 - a_l) , and ara_r to (1ar)(1 - a_r) .
  • If l+1=rl + 1 = r , the cost of the operation is xx . Otherwise, the cost is yy .

You have to find the minimum cost needed to make aa equal to bb or say there is no way to do so.

输入格式

The first line contains one integer tt ( 1t10001 \le t \le 1000 ) — the number of test cases.

Each test case consists of three lines. The first line of each test case contains three integers nn , xx , and yy ( 5n50005 \le n \le 5000 , 1x,y1091 \le x, y \le 10^9 ) — the length of the strings, and the costs per operation.

The second line of each test case contains the string aa of length nn . The string only consists of digits 00 and 11 .

The third line of each test case contains the string bb of length nn . The string only consists of digits 00 and 11 .

It is guaranteed that the sum of nn over all test cases doesn't exceed 50005000 .

输出格式

For each test case, if there is no way to make aa equal to bb , print 1-1 . Otherwise, print the minimum cost needed to make aa equal to bb .

输入输出样例

  • 输入#1

    6
    5 8 9
    01001
    00101
    6 2 11
    000001
    100000
    5 7 2
    01000
    11011
    7 8 3
    0111001
    0100001
    6 3 4
    010001
    101000
    5 10 1
    01100
    01100

    输出#1

    8
    10
    -1
    6
    7
    0

说明/提示

In the first test case, selecting indices 22 and 33 costs 88 , which is the minimum.

In the second test case, we can perform the following operations.

  • Select indices 11 and 22 . It costs 22 , and aa is 110001 now.
  • Select indices 22 and 33 . It costs 22 , and aa is 101001 now.
  • Select indices 33 and 44 . It costs 22 , and aa is 100101 now.
  • Select indices 44 and 55 . It costs 22 , and aa is 100011 now.
  • Select indices 55 and 66 . It costs 22 , and aa is 100000 now.

The total cost is 1010 .

In the third test case, we cannot make aa equal to bb using any number of operations.

In the fourth test case, we can perform the following operations.

  • Select indices 33 and 66 . It costs 33 , and aa is 0101011 now.
  • Select indices 44 and 66 . It costs 33 , and aa is 0100001 now.

The total cost is 66 .

In the fifth test case, we can perform the following operations.

  • Select indices 11 and 66 . It costs 44 , and aa is 110000 now.
  • Select indices 22 and 33 . It costs 33 , and aa is 101000 now.

The total cost is 77 .

In the sixth test case, we don't have to perform any operation.

首页