Spaceships
InterviewTime limit1sMemory limit512 MB
The program counts triples with one module from each factory whose fuel-weighted range tops half the best possible range.
- Level
Medium6 of 10
- Topics
- Sorting, Binary search
- Solved
- No attempts yet
Problem
The air force generals of Byteland, Bajtek, Kajtek, and Zajtek, are preparing the first great space expedition in the country's history. Each of them has already stockpiled some rocket fuel, and now they are about to buy the modules that make up a rocket. A Byteland rocket flies in stages: at the start only the bottom (first) module provides thrust, and once its fuel is spent that module is jettisoned and the next module takes over, and so on.
Today each general visits his favourite factory and buys exactly one engine module of some efficiency. The three purchased modules are stacked in a fixed order: Bajtek buys the bottom module, Kajtek the middle one, and Zajtek the top one. The rocket's range (the greatest distance it can fly) equals the sum of the ranges of its three modules, where each module's range is the product of its engine efficiency and the volume of fuel loaded into it. Bajtek loads all units of his fuel into his module, Kajtek loads units into his, and Zajtek loads units into his.
Because prices keep rising, the generals cannot always buy the most efficient modules. They want to know how many different triples of modules (one bought by each general from his own factory) form a rocket whose range is strictly greater than half of the maximum possible range, where the maximum range is achieved when every general buys the most efficient module in his factory. Two modules with the same efficiency still count as different modules.
Write a program that:
- reads the module offerings of the factories the generals will visit and the amounts of rocket fuel they possess from standard input,
- computes the number of module triples that satisfy Bajtek, Kajtek, and Zajtek's requirement,
- prints the result to standard output.
Input
The first line contains three integers , , and (), separated by single spaces, giving the fuel volumes owned by Bajtek, Kajtek, and Zajtek respectively.
Then follow the descriptions of the three factories, in the order visited by Bajtek, Kajtek, and Zajtek. Each factory is described by two lines. The first line holds one integer (), the number of modules on offer. The second line holds positive integers, each at most and separated by single spaces, giving the efficiencies of those modules.
Output
Print a single line with the number of rockets whose range is strictly greater than half of the maximum range.
Note
As an illustration, take , , , with the first factory offering efficiencies and , the second offering , , , and the third offering (this is the first test case). The maximum possible range is , so a rocket qualifies when its range exceeds . The four qualifying module triples (written by efficiency) are , , , and , with ranges , , , and . The two triples are counted separately because the middle factory lists the efficiency module twice.