Miss Sloane
Time limit5sMemory limit1024 MB
Choose senators to lobby, dividing each chosen a_i by a divisor at most k, so that the gcd of all final values is greater than 1, minimizing the total lobbying time.
- Level
Hard8 of 10
- Topics
- Number theory, Greedy, Sorting
- Solved
- No attempts yet
Miss Sloane
In the movie, Elizabeth Sloane, a ruthless lobbyist, and her firm are approached by Bill Sanford, a gun manufacturing representative who leads the opposition to the Heaton-Harris bill. The bill would expand background checks on gun purchases. Sanford proposes to target female voters to overturn their maternal tendencies when they vote. Sloane mocked this idea. She believed that the power of congressmen should be used for the benefit of everyone rather than the congressmen themselves, and that this is the right thing to do.
In this problem you play the other side. Sloane takes Sanford's role in the campaign against the Heaton-Harris bill. Your goal is to lobby the senators so that they cannot reach an agreement on the bill. There are senators in Congress. Each senator has a positive integer that represents his or her general opinions on various public issues. The resistance of each senator to lobbying is a positive integer . If the greatest common divisor of the public-issue indicators is not 1, the senators reach an agreement and the bill passes. Your goal is to prevent this.
Sloane's lobbying ability is an integer . The campaign proceeds as follows. In each round, Sloane may approach one senator, for example the th senator, and influence his or her opinions on public issues of her choice. As a result, she can divide by a natural divisor that is no larger than . The senators are wary, so Sloane can influence each senator at most once. Also, as more senators are lobbied, each further lobbying takes longer. Suppose Sloane has already lobbied senators whose total resistance is . If she lobbies the th senator next, that lobbying takes units of time.
Find the minimum total time needed to complete the campaign. If it is impossible, print -1.
Input
The first line contains two integers and , the number of senators and Sloane's lobbying ability. The second line contains integers , the indicators of the senators. The third line contains integers , the resistances of the senators.
Output
Print the minimum total time needed to complete the campaign, or -1 if it is impossible.
Constraints