cho.sh
Notes
Loading...

Organizing Books

Time limit

2s

Memory limit

128 MB

Problem

There are N empty boxes in a row, and you want to place M books into them in order. The boxes are numbered from 1 to N, and the books are numbered from 1 to M. At the beginning, you stand in front of box 1 while holding book 1.

The placement process is as follows.

  1. If the current book does not fit in the current box, go to step 3. Otherwise, go to step 2.
  2. Put the current book into the current box. Pick up the next book and return to step 1.
  3. Move the current box aside and seal it with tape. Bring the next box forward and return to step 1.

The capacity of box i is Ai, and the size of book j is Bj. A book fits in the current box if the sum of the sizes of the books already in that box plus the size of the current book does not exceed the box capacity.

After performing the process exactly as described, compute the total wasted capacity over all boxes. The wasted capacity of one box is its capacity minus the total size of the books placed in it.

You may not change the order of the boxes or the books given in the input.

Input

The first line contains the number of boxes N and the number of books M. The second line contains the box capacities A1, A2, ..., AN. The third line contains the book sizes B1, B2, ..., BM.

Output

Print the total wasted capacity over all boxes on the first line.

Constraints

  • 1 <= N, M <= 50
  • 1 <= Ai, Bj <= 1,000
  • The input is guaranteed to allow every book to be placed using the given process.