<a href="https://www.spoj.com/problems/KOPC12A/" target="_blank"><i class="fa fa-external-link" aria-hidden="true"> Link -> Building Construction | SPOJ - KOPC12A - K12</i></a>
Input Format
Check the link
Output Format
Check the link
Constraints
Check the link
Example
Input
1 3 1 2 3 10 100 1000
Output
120