cswork / OAOpen practice workspace

AmazonOA #208

Get Maximum Stability

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Choose any non-empty subset of servers; its stability is the minimum availability multiplied by the sum of reliability. First maximize the exact integer stability, then take it modulo 1000000007. The source's second example output 5 came from an unrelated explanation; it is corrected to 456 according to the definition.

Input

The first line contains n, the second line reliability, and the third line availability. 2≤n≤100000; every value is in 1..1000000.

Output

Output a single integer, the answer.

Example 1

Input:

3
1 2 2
1 1 3

Output:

6

Example 2

Input:

6
75 104 72 72 8 125
1 2 2 1 2 1

Output:

456

Example 3

Input:

2
2 3
5 5

Output:

25

Example notes

The answers are 6, 456 and 25. The second example chooses all servers: the reliability sum is 456 and the minimum availability is 1.

Editorial

Loading solution…