Submit | All submissions | Best solutions | Back to list |
KOPC12A - K12 - Building Construction |
Given N buildings of height h1, h2, h3 ... hn, the objective is to make every building has equal height. This can be done by removing bricks from a building or adding some bricks to a building. Removing a brick or adding a brick is done at certain cost which will be given along with the heights of the buildings. Find the minimal cost at which you can make the buildings look beautiful by re-constructing the buildings such that the N buildings satisfy
h1 = h2 = h3 = ... = hn = k (k can be any number).
For convenience, all buildings are considered to be vertical piles of bricks, which are of same dimensions.
Input
The first line of input contains an integer T which denotes number of test cases .This will be followed by 3 * T lines, 3 lines per test case. The first line of each test case contains an integer n and the second line contains n integers which denotes the heights of the buildings [h1, h2, h3 ... hn] and the third line contains n integers [c1, c2, c3 ... cn] which denotes the cost of adding or removing one unit of brick from the corresponding building.
T <= 15; n <= 10000; 0 <= Hi <= 10000; 0 <= Ci <= 10000;
Output
The output must contain T lines each line corresponding to a testcase.
Example
Input: 1 3 1 2 3 10 100 1000 Output: 120
Added by: | Radhakrishnan Venkataramani |
Date: | 2012-01-31 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Own |
hide comments
|
||||||||
2019-07-25 11:05:14
badihi hast:) |
||||||||
2019-07-25 06:36:33
1 3 1 3 2 10 100 1000 answer --> 110 |
||||||||
2019-04-04 06:27:48
AC in one go . :) modified binary search for unimodal function. |
||||||||
2019-02-26 12:01:43
can anyone prove that the function is unimodal ? |
||||||||
2018-12-10 20:31:48
I am using prefix sum but getting WA .Any hint or test case? |
||||||||
2018-10-01 21:29:22
trivial upper and lower bound btw, enjoyed it. |
||||||||
2018-05-25 10:47:49
I don't understand how ternary search is applicable here! Can anyone give me proof that the function will be unimodal? Anyways better solution is possible. Hint: Prefix sums . Enjoy ;) |
||||||||
2018-02-22 06:49:35
badihi nis :) |
||||||||
2017-07-05 18:02:53 geeta
Amazing Ques !! Enjoyed solving it :) |
||||||||
2017-04-18 21:55:52
doing without ternary search is a challenge...... |