This page is still under construction.

Parts of this page are still being built. What you see may change.

Moving 5

Time limit1sMemory limit512 MB

Summary
Find the maximum candy sum along a monotone path from (1,1) to (N,M) in an N by M grid where room (i,j) holds A_i*10^9 + B_j.
Level

Medium6 of 10

Topics
Greedy, Math, Dynamic programming
Solved
No attempts yet

Problem

Jungyu is trapped in an N×M maze. The maze is divided into 1×1 rooms, and each room contains some candy. The number of candies in room (i, j) is Ai×109+BjA_i \times 10^9 + B_j. The top-left room of the maze is (1, 1), and the bottom-right room is (N, M).

Jungyu is currently at (1, 1) and wants to move to (N, M). When Jungyu is at (r, c), he can move to (r+1, c) or (r, c+1), and each time he visits a room, he can take all the candies in it. He cannot leave the maze.

Find the maximum number of candies Jungyu can take when he moves from (1, 1) to (N, M).

Input

The first line gives the maze dimensions N and M. (1 ≤ N, M ≤ 100,000)

The second line gives A1,A2,…,ANA_1, A_2, \dots, A_N, and the third line gives B1,B2,…,BMB_1, B_2, \dots, B_M. (0 ≤ Ai,BjA_i, B_j ≤ 9)

Output

Print the maximum number of candies Jungyu can take when moving to (N, M) on the first line.

Hint

The maze from the sample looks as follows, and for convenience the number of candies in (i, j) is written as Ai,BjA_i, B_j.

0, 40, 10, 90, 7
7, 47, 17, 97, 7
1, 41, 11, 91, 7
7, 47, 17, 97, 7
6, 46, 16, 97, 7
7, 47, 17, 97, 7
6, 46, 16, 96, 7

Examples1

  1. Example 1

    Input
    7 4
    0 7 1 7 6 7 6
    4 1 9 7
    
    Expected output
    55000000068