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.


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.


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


1 1 1 1
2 2 2 2
10 80 37 22 109
6 8 8 20 15
0 Output: 8

hide comments
it_uchi: 2021-01-02 18:29:16

AC in one GO

harry_shit: 2020-05-20 09:35:24

for cpp, take everything in long long

manish_thakur: 2020-05-19 12:55:19

Simple, no optimization req in c++

sanjeev30798: 2020-01-12 17:55:04

i would just say that avoid using cin in C++ for taking long long inputs rather use scanf

dkkv0000: 2019-05-08 11:11:00

very easy sum use unsigned long long int

prabhav_123: 2019-01-07 13:25:37

use long long..unless you don't want an headache!

kranthi_0003: 2018-08-11 16:21:30

getting sigsev in c++...dont know y,but AC in one go in python:)

khoaph: 2018-06-29 05:35:43

Finally got AC after using long long for Ci, Pi

anirudnits: 2018-02-11 10:40:37

@vengatesh15 thanks :)

karthik1997: 2017-08-30 00:04:01

FAST i/o and cpp std:: sort : AC in 0.02s

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