Wooden house - MarisaOJ: Marisa Online Judge

Wooden house

Time limit: 1500 ms
Memory limit: 256 MB
A construction company has recently imported $n$ wooden poles with varying heights, denoted as $A_1, A_2, \ldots, A_n$. The company has $m$ house designs, with the $i$-th design requiring $S_i$ poles. The cost of building a house is determined by the difference between the maximum and minimum pole heights, multiplied by a predefined constant $C$. Let $max$ and $min$ represent the maximum and minimum heights of the poles used, respectively. Upon completing a house, the company receives a payment of $P$. Hence, the profit from constructing a house is given by $P - (max - min)^2 \times C$. It is important to note that this value can be negative. The objective is to find the maximum profit while ensuring that at least one house from each design is built. ### Input - The first line contains four integers $n,m,P,C$. - The second line contains $n$ integers $A_i$. - The third line contains $m$ integers $S_i$. - It is guaranteed that $\sum_{i=1}^{m} S_i \le n$ and $S_i \neq S_j$ for $1 \le i < j \le m$. ### Output - Print an integer, the maximum profit. ### Constraints - $1 \le n \le 10^5$. - $1 \le m \le 6$. - $1 \le P \le 10^9$. - $1 \le C \le 10^6$. - $1 \le A_i \le 10^6$. - $2 \le S_i \le n$. ### Example Input: ``` 10 2 11 1 14 5 6 4 4 4 7 8 9 1 4 2 ``` Output: ``` 30 ```