A Gift for the Lady#10359

A Gift for the Lady

2 seconds
256 megabytes
Hard

Kozak Vus spent a long time deciding on the perfect birthday gift for Lady. He wanted it to be both unique and practical, so he gave her a collection of number pairs (a_i, b_i), with a total of pairs.

Initially, Lady was puzzled by this unusual gift. The original set didn't quite appeal to her, so she decided to select a subset of these pairs that would meet specific criteria.

Lady aims to form a new set, which is a subset of the original one. Formally, this subset consists of pairs (a_{i_1}, b_{i_1}), (a_{i_2}, b_{i_2}), \dots, (a_{i_m}, b_{i_m}), where 0 \leq m \leq n and for all 1 \leq p \leq m-1, it holds that i_p < i_{p+1}. Lady can choose anywhere from to all pairs.

Lady considers a set to be the most beautiful if it satisfies the following conditions:

  1. Lady's favorite number is . Therefore, the bitwise OR of all selected a_{i_p} for 1 \leq p \leq m must not exceed .

  2. The sum of all b_{i_p} for 1 \leq p \leq m should be maximized.

The bitwise OR operation (denoted as ) is performed on each bit of the numbers, resulting in if both bits are , and otherwise. For example, consider the numbers and . Their binary forms are and . Applying the OR operation bit by bit, the result is , which equals in decimal.

Lady is struggling to find the most beautiful set. She realizes this is a challenging task, so she only asks you to determine the maximum possible sum of all b_{i_p} in the most beautiful set.

Input

The first line contains two integers and (1 \leq n \leq 10^6, 0 \leq k < 2^{30}).

Each of the next lines contains two integers a_i and b_i (0 \leq a_i, b_i < 2^{30}).

Output

Output a single number — the maximum possible sum of the selected b_{i_p}.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

In the first example, selecting the first and second pairs results in a bitwise OR of , with a sum of . It's impossible to increase the sum because including all three pairs results in a bitwise OR of , which exceeds .

In the second example, the optimal choice is the second, third, and fourth pairs, yielding a sum of . Including the first pair would limit the selection to only the third pair to keep the bitwise OR within . Thus, the maximum possible sum is .

Scoring

  1. ( points): n \leq 10;

  2. ( points): n \leq 10^4, k < 2^{10};

  3. ( points): k = 2^t -1, where 0 \leq t \leq 30;

  4. ( points): no additional restrictions.