DCOWS - Dancing Cows

It's the spring dance and, in a rare occurrence, the N (1 ≤ N ≤ 5000) bulls have been invited to dance with the M (N < M ≤ 5000) cows (as long as they stay on their very best behavior).

Farmer John, almost obsessive-compulsive in his organization of dances, wants the spectacle to be as visually attractive as possible. Thus, he wants to pair up the N bulls with cow partners such that the total of all the magnitudes of differences in height is minimized. Bulls have heights B_i (1 ≤ B_i ≤ 1,000,000) and cows have height C_i (1 ≤ C_i ≤ 1,000,000). Of course, some cows will be unmatched since N-M of them will have no partners; ignore their heights.

Input

  • Line 1: Two space-separated integers: N and M.
  • Lines 2..N+1: Line i+1 contains a single integer: B_i.
  • Lines N+2..M+N+1: Line i+N+1 contains a single integer: C_i.

Output

  • Line 1: A single integer that is the minimum of the sum of the absolute value of the height differences that can be achieved.

Sample

Input:
5 7
10
16
12
10
13
7
17
12
10
9
6
11

Input:
4

Explanation

There are five bulls + seven cows with various heights:

  • Bulls: 10 10 12 13 16
  • Cows: 6 7 9 10 11 12 17

Here is one way to achieve a total difference of 4:

  • Bulls: 10 10 12 13 16
  • Cows: 9 10 11 12 17, with 6 7 unmatched.

Added by:Se7s
Date:2013-05-15
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ADA95 ASM32 ASM64 GAWK BASH BF CLPS CLOJURE LISP sbcl LISP clisp D ERL FSHARP GO ICON ICK LUA NEM NICE NODEJS OCAML PERL6 PIKE PRLG-swi SCALA SCM guile SCM qobi SED ST TCL WHITESPACE
Resource:USACO 2008 Gold

hide comments
2016-07-14 14:27:38
getting wrong ans after or on case 15. Tried using long and scanf and without scanf as well. Simple O(m*n) approach. Somebody please help.
2016-05-27 08:26:26
do not use scanf .WA
2016-02-17 10:37:59 Liquid_Science
10 NZEC's repeatedly //badly formatted input . Easy question
2015-11-28 16:36:34 kejriwal
nice ques !!!
2015-01-24 14:52:24 Vikneshwar E
@ping_of_death : 3
2014-02-04 14:03:46 vivek malasi
i'm still getting WA can some1 has some other test cases which might be
affecting my answer or any silly mistake
2014-01-26 21:10:45 ping_of_death
Give more test cases...
5 7
8 11 12 15 21
7 10 12 15 18 19 20
what is the op.
3 or 5(21- 20 or 21- 18)
2014-01-26 17:06:39 Devesh Kumar
Any tricky test case , i have tried all kind of combination and i am not able to find the why i am getting WA ? My submission id is 10939580

Last edit: 2014-01-27 07:03:22
2013-11-05 09:56:47 Edelweiss
I think...it should be M-N, not N-M, in the description "Of course, some cows will be unmatched since N-M of them will have no partners" :)
2013-07-21 01:32:11 Hussain Kara Fallah
@encrypted
time limit is 1 second for each testcase not for all testcases
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.