CADYDIST - Candy Distribution


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

hide comments
Rajat Sharma: 2016-07-17 20:50:51

Java : used long and Buffered reader. figured in hours

vaibhav138: 2016-06-26 03:02:43

Use array of long long int,
otherwise it will give wa

Francky: 2016-06-24 15:10:23

Constraints are correct, if you think they're not, then you need to learn why your WA code got WA !!!
This had already been checked by EB. (see :D's comment, amongst other)
You can read input using int as I did !

vineetpratik: 2016-06-24 14:30:47

the constraints in this question are wrong, YOU HAVE BEEN WARNED!
initially i took all as int and final result as long long int got WA!
then took everything as long long got AC!

suraj: 2016-06-19 10:31:36

use long long int for each type

aru_674: 2016-06-06 18:46:07

why do they give wrong constraints......ci and pi are to be taken long long

mkfeuhrer: 2016-05-30 17:56:17

long long ...... stl :-) AC in one go :-)

rayhan50001: 2016-04-28 20:53:07

use long long it cost me 2 WA,, simple sort as & des & normal multiplication.

pvsmpraveen: 2016-02-02 14:58:48

simple STL :)

farhan764: 2016-01-26 20:33:14

quick sort............


Added by:Paulo Costa
Date:2012-01-19
Time limit:0.301s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:ITA - Brazilian ICPC Training Camp, Jan-Feb/2012