FASHION - Fashion Shows

A fashion show rates participants according to their level of hotness. Two different fashion shows were organized, one for men and the other for women. A date for the third is yet to be decided ;) .

Now the results of both fashion shows are out. The participants of both the fashion shows have decided to date each other, but as usual they have difficuly in choosing their partners. The Maximum Match dating serive (MMDS) comes to their rescue and matches them in such a way that that maximizes the hotness bonds for all couples.

If a man has been rated at hotness level x and a women at hotness level y, the value of their hotness bond is x*y.

Both fashion shows contain N participants each. MMDS has done its job and your job is to find the sum of hotness bonds for all the couples that MMDS has proposed.


The first line of the input contains an integer t, the number of test cases. t test cases follow.

Each test case consists of 3 lines:

  • The first line contains a single integer N (1 <= N <= 1000).
  • The second line contains N integers separated by single spaces denoting the hotness levels of the men.
  • The third line contains N integers separated by single spaces denoting the hotness levels of the women.

All hotness ratings are on a scale of 0 to 10.


For each test case output a single line containing a single integer denoting the sum of the hotness bonds for all pairs that MMDS has proposed.


1 1
3 2
2 3 2
1 3 2


hide comments
StupidGuy: 2012-07-05 01:34:30

MergeSort worked fine here :)

Aradhya: 2012-04-07 09:19:13

:O what the f.. by bubble sort it gotAC.. and by my heap sort it gives wrong answer

Hélder Moreira: 2012-02-08 23:22:08
I used this but for some reason it says wrong answer. Any help?

Ajay Gupta: 2012-02-04 20:55:50

can i have some more test cases pls...i'm getting it correct on my machine but shows
sigsegv error
help pls!!!

Baskara Nur Patria: 2011-12-06 10:00:18

please give me some test case, runs well on my computer, but still getting WA

Mark Fulbright: 2011-04-04 04:50:52

Yes, you must maximize the sum of the hotness bonds for the couples by changing the pairs if necessary.

adri ramadhan: 2011-03-27 04:09:13

i don't get the output, anybody care to explain?

Alexander: 2010-10-20 15:46:08

On an interesting side note: the maximising principle underlying this problem generalises to almost-everywhere finite functions on sigma-finite measure spaces by a theorem of Hardy and Littlewood, see Theorem II.2.2 in C. Bennett, R. Sharpley, "Interpolation of Operators"

Last edit: 2010-10-20 15:46:41
Bill Black: 2009-07-06 05:24:03

Although MMDS proposes couples, it in no way ensures that the couples match up to produce a maximum hotness. Nay, such work is left up to the programmer in a typical "oh, I thought I mentioned that to you yesterday" style. :P

Added by:Matthew Reeder
Time limit:1s
Source limit:30000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Al-Khawarizm 2006