Alice and Bob are playing a game. They are given an array as well as an array
.
The game consists of rounds. Players are participating in rounds alternatively. Alice goes first. During the
-th round (for
from
to
) the corresponding player (Alice, if
is odd, and Bob if
is even) has to do exactly one of the following:
- remove all elements from the array
that are divisible by
,
- remove all elements from the array
that are not divisible by
.
Alice wants to minimize the sum of the remaining elements in the array after all
rounds, and Bob wants to maximize the sum of the remaining elements in the array
. Find the sum of the remaining elements in the array
after all
rounds if both Alice and Bob are playing optimally.
Input Specification
The first line contains two integers ,
(
,
), the length of the array
and the number of rounds in the game.
The second line contains integers
(
), the elements of the array
.
The third line contains integers
(
), the elements of the array
.
Output Specification
Output a single integer, the sum of the remaining elements of the array after all
rounds if both players are playing optimally.
Constraints
Subtask | Points | Additional constraints |
---|---|---|
All | ||
No additional constraints |
Sample Input 1
6 2
2 2 5 2 2 7
2 5
Sample Output 1
7
Explanation for Sample 1
One possible move of the game is the following:
- Round 1: Alice removes from
all elements divisible by
.
becomes
.
- Round 2: Bob removes from
all elements divisible by
.
becomes
. If he had removed from
all elements not divisible by
,
would become
, which has a smaller sum of elements and therefore is not desirable for the second player.
Sample Input 2
5 1
-5000111000 -5000222000 -15 5 2
5
Sample Output 2
-10000333010
Comments