AlgoUniversity
  • Go Back
Discussion
Damaged Roads :

Author

admin

Difficulty Level : Hard

Submissions : 819

Asked In : Medianet

Marks :10

: 10 | : 4

You are the Prime Minister of a country and once you went for a world tour.

After 5 years, when you returned to your country you were shocked to see the condition of the roads between the cities. So, you plan to repair them, but you cannot afford to spend a lot of money.

The country can be represented as a N*M grid, where Country[i,j] is a city.

The cost of repairing a repairing a road between [i,j] and [i+1,j] is A[i].

The cost of repairing a road between [i,j] and [i,j+1] is B[i].

Return the minimum cost of repairing roads such that all cities become a connected network.

As the cost can be large, return the cost modulo $$$10^9+7$$$.

Input

First line contains 2 space separated values N , M.

Second line contains N-1 values corresponding to content of A.

Third line contains M-1 values corresponding to content of B.

Constraints

$$$2 \le N,M \le 1e5$$$

$$$0 \le A[i],B[i] \le 1e3$$$

Output

Return an integer representing the minimum possible cost.

Examples

Input
4 4
1 1 1
1 1 2
Output
16
Input
4 4
1 2 3
4 5 6
Output
39

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.