Sphere Online Judge

SPOJ Problem Set (classical)

10442. Candy Distribution

Problem code: CADYDIST

Alice is a teacher that loves her students. As the school year reaches its end, she wants to reward all her students with candies for all their hard work.

Since each of her classes is unique, she decided she’ll give a different kind of candy for each class, and in order to avoid students being mad at others in their class, she wants to make things fair by giving all students in the same class the same kind of candy.

Happily, she went to the candy shop, and fortunately found out that it had N different types of candy, exactly the same number of classes of students she taught!

Looking at the prices and paying close attention to the number of students in each class, Alice noted that she could save some money by assigning the types of candy to certain classes. Because she’s a teacher, her income is not that big and saving money is very important to her, so she asked you to write a program to determine the least amount of money she must spend.

Input

Each test case consists of three lines. The first line contains a positive integer N (1≤N≤100000). The second line contains N integers Ci, the ith integer indicates the number of students in Alice’s i-th class. The third and last line also contains N integers Pi the ith integer indicates the price of the ith type of candy (1 ≤ Ci, Pi  ≤ 100000).

The input ends with a line consisting of a 0, which indicates end of input.

Output

For each test case, output a line containing the least amount of money Alice must spend.

Example

Input:
4
1 1 1 1
2 2 2 2
5
10 80 37 22 109
6 8 8 20 15
0 Output: 8
2120

Added by:Paulo Costa
Date:2012-01-19
Time limit:2s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All
Resource:ITA - Brazilian ICPC Training Camp, Jan-Feb/2012

hide comments
2013-06-07 20:20:35 AA kinky
http://ideone.com/XcvHoT
why i m gettin time limit exceed :(
pls tell me...ihav used long long int also
2013-06-03 12:52:38 Maged Shalaby
Same thing happened here, read all values in long long int.
2013-05-21 09:44:24 ,,l,,
this shouldn't be so .. cout -> printf cin->scanf tle-> ac :P
2013-04-06 00:05:04 যোবায়ের
Whenever stupid people gets WA, the claim something wrong in test data. Test data maintains exactly as given in description, don't look below, use your own calculation about what data type you need to use.
2013-02-25 12:27:17 TLE
WTF!! 3 WA due to wrong constraints :(
2013-02-02 10:04:10 Ouditchya Sinha
@Mukund Kumar thank you for the tip... :)
2013-01-13 18:53:00 SAHIL SAREEN
somehow...
my quick sort<qsort()> solution
takes way more time than the simple sort()solution...
any explainations..
2013-01-09 19:22:05 Saurav Kumar
4 WA because of wrong constraints!
2012-12-27 16:58:09 Philipp Heeg
Description is actually wrong. ci, pi can be > 2^31.
2012-11-16 18:22:23 Mukund Kumar
Got AC with long long int...
i dont actually get y do people give unnecessary advice....had got rte coz of unsigned lon long int
SPOJ © 2013 Sphere Research Labs. All Rights Reserved.