Under construction

Still being built. Things may change

Moving 3

Interview

Time limit2sMemory limit512 MB

Summary
A path from (0,0) to (N,M) using only down and right steps costs A[r] per down move and B[c] per right move; find the cheapest path.
Level

Medium4 of 10

Topics
Greedy, Math, Prefix sum
Solved
No attempts yet

Problem

Jungyu is trapped in a maze of size (N+1)×(M+1)(N+1)\times(M+1). The maze is divided into rooms of size 1×11\times1. The top-left room is (0,0)(0, 0) and the bottom-right room is (N,M)(N, M).

Jungyu is currently at (0,0)(0, 0) and wants to move to (N,M)(N, M). If Jungyu is at (r,c)(r, c), he can move through a door to (r+1,c)(r+1, c) or (r,c+1)(r, c+1). He cannot leave the maze.

This time there is trash in the gaps between the doors. So when moving from (r,c)(r, c) to (r+1,c)(r+1, c) or from (r,c)(r, c) to (r,c+1)(r, c+1), he must take all the trash in the door gap. Moving from (r,c)(r, c) to (r+1,c)(r+1, c) takes ArA_r pieces of trash, and moving to (r,c+1)(r, c+1) takes BcB_c pieces of trash.

Find the minimum amount of trash Jungyu must take when he moves to (N,M)(N, M).

Input

The first line gives the size of the maze, NN and MM. (1≤N,M≤500,0001 \le N, M \le 500{,}000)

The next line gives NN integers AiA_i. (1≤Ai≤1091 \le A_i \le 10^9)

The next line gives MM integers BiB_i. (1≤Bi≤1091 \le B_i \le 10^9)

Output

On the first line, print the amount of trash Jungyu must take when he moves to (N,M)(N, M).

Examples3

  1. Example 1

    Input
    3 4
    60 20 90
    10 90 80 70
    
    Expected output
    420
    
  2. Example 2

    Input
    1 1
    1
    1
    
    Expected output
    2
    
  3. Example 3

    Input
    2 2
    1000000000 1000000000
    1000000000 1000000000
    
    Expected output
    4000000000