All problems

Problem 908

Maximum Performance of a Team

Hard · 30 pts · Array, Greedy, Heap, Sorting

Problem

Engineer i has a speed and an efficiency. The performance of a team is the sum of its members' speeds multiplied by the smallest efficiency in the team. Choose a non-empty team of at most K engineers with maximum performance, and print it modulo 1000000007 (after finding the maximum). Process engineers by efficiency from high to low, so each one is the minimum efficiency so far, and keep the K fastest speeds seen in a min-heap. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

The first line contains N and K. The second line contains the N speeds. The third line contains the N efficiencies.

Output

Print the maximum performance modulo 1000000007.

Constraints

1 <= K <= N <= 100000; 1 <= speed <= 10^5; 1 <= efficiency <= 10^8

Example input

6 2
2 10 3 1 5 8
5 4 3 9 7 2

Example output

60

Explanation

With speeds 2 10 3 1 5 8, efficiencies 5 4 3 9 7 2 and K = 2, the team of engineers 1 and 4 has speed 10 + 5 = 15 and minimum efficiency 4, for a performance of 60, the best possible.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Total Cost to Hire K Workers Next: Binary Tree Paths