Under construction

Still being built. Things may change

Silk Road

Interview

Time limit1sMemory limit256 MB

Summary
Choose N of M days in order for the eastward moves so the sum of distance times daily badness is smallest.
Level

Medium4 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

Kazakhstan once had a trade route called the Silk Road.

The Silk Road has N+1N + 1 cities. From west to east they are city 00, city 11, ..., city NN. The distance between city i−1i - 1 and city ii (1≤i≤N1 \le i \le N) is DiD_i.

JOI, a trader, starts at city 00 and travels through the cities from west to east until reaching city NN. The trip must finish within MM days. Each day JOI picks one of two actions.

  • Move: spend the day going to the city immediately to the east. From city i−1i - 1 (1≤i≤N1 \le i \le N), JOI arrives at city ii.
  • Wait: stay in the current city for the day.

Moving builds up fatigue. The weather on day jj (1≤j≤M1 \le j \le M) has badness CjC_j, and moving from city i−1i - 1 to city ii on day jj adds Di×CjD_i \times C_j fatigue. A day spent waiting adds no fatigue.

Find the smallest total fatigue with which JOI can reach city NN within MM days.

Input

The first line has two integers NN and MM (1≤N≤M≤10001 \le N \le M \le 1000), separated by a space. The Silk Road has N+1N + 1 cities, and JOI must get from city 00 to city NN within MM days.

Each of the next NN lines has one integer. The ii-th of these lines has DiD_i (1≤Di≤10001 \le D_i \le 1000), the distance between city i−1i - 1 and city ii.

Each of the next MM lines has one integer. The jj-th of these lines has CjC_j (1≤Cj≤10001 \le C_j \le 1000), the weather badness on day jj.

Output

Print the smallest total fatigue with which JOI can reach city NN within MM days, on one line.

Hint

In the first example, this schedule reaches the smallest total fatigue.

  • Day 1: wait.
  • Day 2: move from city 00 to city 11. The fatigue added is 10×30=30010 \times 30 = 300.
  • Day 3: move from city 11 to city 22. The fatigue added is 25×15=37525 \times 15 = 375.
  • Day 4: wait.
  • Day 5: move from city 22 to city 33. The fatigue added is 15×30=45015 \times 30 = 450.

The total fatigue is 300+375+450=1125300 + 375 + 450 = 1125, and no schedule does better.

Examples2

  1. Example 1

    Input
    3 5
    10
    25
    15
    50
    30
    15
    40
    30
    
    Expected output
    1125
    
  2. Example 2

    Input
    2 6
    99
    20
    490
    612
    515
    131
    931
    1000
    
    Expected output
    31589