CADYDIST - Candy Distribution

no tags 

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

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

hide comments
Indian Cyber Army (IndiShell): 2015-02-07 12:46:08

My code with insertion sort is giving TLE :(

Anubhav Gupta: 2015-02-01 15:51:44

use unsigned long long int for all variables!!

Ankur Singh: 2015-01-19 05:55:56

use long long

Malinga: 2015-01-08 06:11:31

@Surayansh tiwari: thanks!! your advice saved me from getting multiple WAs..

AlphaDecay: 2015-01-01 20:59:03

Please correct the description!

surayans tiwari( 2014-12-18 04:31:29

wrong problem description ,, use long long everywhere

Rajat (1307086): 2014-12-16 22:49:39

Greed is good.
1 goooooooo

mayank: 2014-12-14 09:13:01

An advice. If you wish to ascertain the datatype size, then use assert(sizeof(datatype)==n), where is a guess for the size. The program will throw, SIGABRT if assert fails. So it shows pyramid has long long->8 bytes, long->4 bytes, int->4 bytes. :)

Utkarsh Rastogi: 2014-10-01 20:47:29

AC in one go..... :)
And that too without debugging....

Shaktiman: 2014-08-25 20:12:42

just keep attention on data types you are taking and their i/o
as for unsigned long long use %llu in scanf and printf