Under construction

Still being built. Things may change

Game With Numbers

Time limit1sMemory limit1024 MB

Summary
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 a1,a2,…,ana_1, a_2, \dots, a_n and an array b1,b2,…,bmb_1, b_2, \dots, b_m.

The game consists of mm rounds. The players take turns alternately. In round ii (for ii from 11 to mm), the first player moves if ii is odd and the second player moves if ii is even. That player must do exactly one of the following:

  • remove all elements from aa that are divisible by bib_i,
  • remove all elements from aa that are not divisible by bib_i.

The first player wants to minimize the sum of the elements remaining in aa after all mm rounds, and the second player wants to maximize it. Find the sum of the remaining elements of aa after all mm rounds if both players play optimally.

Input

The first line contains two integers nn and mm (1≤n≤2⋅1041 \le n \le 2 \cdot 10^4, 1≤m≤2⋅1051 \le m \le 2 \cdot 10^5), the length of aa and the number of rounds.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−4⋅1014≤ai≤4⋅1014-4 \cdot 10^{14} \le a_i \le 4 \cdot 10^{14}), the elements of aa.

The third line contains mm integers b1,b2,…,bmb_1, b_2, \dots, b_m (1≤bi≤4⋅10141 \le b_i \le 4 \cdot 10^{14}), the elements of bb.

Output

Print a single integer, the sum of the remaining elements of aa after all mm 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 aa divisible by 22. aa becomes (5,7)(5, 7).
  • Round 2: the second player removes all elements of aa divisible by 55. aa becomes (7)(7). If the player had removed all elements not divisible by 55, aa would become (5)(5), which has a smaller sum, and the second player does not want that.

Examples2

  1. Example 1

    Input
    6 2
    2 2 5 2 2 7
    2 5
    
    Expected output
    7
    
  2. Example 2

    Input
    5 1
    -5000111000 -5000222000 -15 5 2
    5
    
    Expected output
    -10000333010