Game With Numbers
Time limit1sMemory limit1024 MB
Two players alternately delete all array elements divisible by b_i or all not divisible by b_i, and the task is the final sum under optimal play.
- Level
Hard8 of 10
- Topics
- Game theory, Number theory, Math
- Solved
- No attempts yet
Statement
Two players are playing a game. They are given an array and an array .
The game consists of rounds. The players take turns alternately. In round (for from to ), the first player moves if is odd and the second player moves if is even. That player must do exactly one of the following:
- remove all elements from that are divisible by ,
- remove all elements from that are not divisible by .
The first player wants to minimize the sum of the elements remaining in after all rounds, and the second player wants to maximize it. Find the sum of the remaining elements of after all rounds if both players play optimally.
Input
The first line contains two integers and (, ), the length of and the number of rounds.
The second line contains integers (), the elements of .
The third line contains integers (), the elements of .
Output
Print a single integer, the sum of the remaining elements of after all rounds if both players play optimally.
Hint
In the first sample, one possible course of the game is the following:
- Round 1: the first player removes all elements of divisible by . becomes .
- Round 2: the second player removes all elements of divisible by . becomes . If the player had removed all elements not divisible by , would become , which has a smaller sum, and the second player does not want that.