This page is still under construction.

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

My Ideal Futon Stack

Time limit8sMemory limit512 MB

Summary
Choose a closet order for N futons so that, using only stack pushes and pops, the daily total warmth on the bed minimizes the sum of |demand - warmth| over M days.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, Brute force, Stack
Solved
No attempts yet

Problem

You bought NN futons to prepare for a new chapter in life. The ii-th futon has warmth supply sis_i. From the forecast for the next MM days, day jj is expected to have warmth demand djd_j. Too little or too much warmth spoils comfort, so define the discomfort of day jj as the absolute difference between djd_j and the total warmth supply of the futons on you that day. You want to make the sum of discomfort over these MM days as small as possible.

Unfortunately, your room is tiny and contains only a bed and a closet. To add one futon to the bed, you must take the futon on top of the closet and put it on top of the bed. Conversely, to remove one futon from the bed, you must take the futon on top of the bed and put it on top of the closet. There is no limit on how many futons you move in a day, but you can move only one futon at a time.

Now you are about to put the futons you just bought into the closet. Only at this moment can you put the futons into the closet in any order you like. How should you store the futons in the closet, and how should you take them out and put them back day by day, to spend each day comfortably? Find the minimum possible sum of discomfort over the MM days. Some futons may never be used, and on some days you may use no futon at all.

Input

The input consists of multiple data sets. Each data set has the following format.

N M
s1 s2 ... sN
d1 d2 ... dM

The first line of a data set contains integers NN, the number of futons, and MM, the number of days with a temperature forecast, separated by a space. The second line contains NN integers s1,s2,…,sNs_1, s_2, \dots, s_N separated by spaces, where sis_i is the warmth supply of the ii-th futon. The third line contains MM integers d1,d2,…,dMd_1, d_2, \dots, d_M separated by spaces, where djd_j is the warmth demand on day jj. These integers satisfy 1≤N≤151 \le N \le 15, 1≤M≤1001 \le M \le 100, and 1≤si,dj≤1,000,0001 \le s_i, d_j \le 1{,}000{,}000.

The end of the input is marked by a data set with N=M=0N = M = 0. Do not produce output for this data set.

Output

For each data set, output the minimum sum of discomfort over the MM days on one line.

Hint

For the fifth case, put the futons into the closet in the order 5, 2, 3, 1 from top to bottom. On day 1 take out 3 futons: ∣10−(5+2+3)∣=0|10 - (5 + 2 + 3)| = 0. On day 2 put back 2 futons: ∣4−5∣=1|4 - 5| = 1. On day 3 take out 1 futon: ∣7−(5+2)∣=0|7 - (5 + 2)| = 0. The total is 0+1+0=10 + 1 + 0 = 1.

Examples1

  1. Example 1

    Input
    1 1
    5
    6
    1 1
    5
    2
    1 1
    20
    5
    4 1
    2 4 5 9
    8
    4 3
    3 5 2 1
    10 4 7
    5 5
    2 2 2 2 2
    1 3 5 7 9
    2 5
    2 5
    2 5 2 5 2
    0 0
    
    Expected output
    1
    2
    5
    1
    1
    5
    4