#agc061e. [agc061_e]Increment or XOR

[agc061_e]Increment or XOR

Problem Statement

There is a non-negative integer XX initially equal to SS. One can perform the following operations, in any order, any number of times:

  • Add 11 to XX. This has a cost of AA.
  • Choose ii between 11 and NN, and replace XX with XoplusYiX \\oplus Y_i. This has a cost of CiC_i. Here, oplus\\oplus denotes bitwise mathrmXOR\\mathrm{XOR}.

Find the smallest total cost to make XX equal to a given non-negative integer TT, or report that this is impossible.

What is bitwise mathrmXOR\\mathrm{XOR}?

The bitwise mathrmXOR\\mathrm{XOR} of non-negative integers AA and BB, AoplusBA \\oplus B, is defined as follows:

  • When AoplusBA \\oplus B is written in base two, the digit in the 2k2^k's place (kgeq0k \\geq 0) is 11 if exactly one of the digits in that place of AA and BB is 11, and 00 otherwise.

For example, we have 3oplus5=63 \\oplus 5 = 6 (in base two: 011oplus101=110011 \\oplus 101 = 110).
Generally, the bitwise mathrmXOR\\mathrm{XOR} of kk non-negative integers p1,p2,p3,dots,pkp_1, p_2, p_3, \\dots, p_k is defined as $(\\dots ((p_1 \\oplus p_2) \\oplus p_3) \\oplus \\dots \\oplus p_k)$. We can prove that this value does not depend on the order of p1,p2,p3,dots,pkp_1, p_2, p_3, \\dots, p_k.

Constraints

  • 1leqNleq81 \\leq N \\leq 8
  • 0leqS,T<2400 \\leq S, T < 2^{40}
  • 0leqAleq1050 \\leq A \\leq 10^5
  • 0leqYi<2400 \\leq Y_i < 2^{40} (1leqileqN1 \\leq i \\leq N)
  • 0leqCileq10160 \\leq C_i \\leq 10^{16} (1leqileqN1 \\leq i \\leq N)
  • All values in the input are integers.

Input

Input is given from Standard Input in the following format:

NN SS TT AA Y1Y_1 C1C_1 vdots\\vdots YNY_N CNC_N

Output

If the task is impossible, print -1. Otherwise, print the smallest total cost.


Sample Input 1

1 15 0 1
8 2

Sample Output 1

5

You can make XX equal to TT in the following manner:

  • Choose i=1i=1 and replace XX with Xoplus8X \\oplus 8 to get X=7X=7. This has a cost of 22.
  • Add 11 to XX to get X=8X=8. This has a cost of 11.
  • Choose i=1i=1 and replace XX with Xoplus8X \\oplus 8 to get X=0X=0. This has a cost of 22.

Sample Input 2

3 21 10 100
30 1
12 1
13 1

Sample Output 2

3

Sample Input 3

1 2 0 1
1 1

Sample Output 3

-1

Sample Input 4

8 352217 670575 84912
239445 2866
537211 16
21812 6904
50574 8842
380870 5047
475646 8924
188204 2273
429397 4854

Sample Output 4

563645