To query at :
While the deque has lines, check if the second line is better than the first at .
If , pop the front. The first line will never be optimal again because queries are increasing.
Repeat until the front is optimal.
Return . Because queries are sorted, once you pop a line, you never need it again. Each line leaves the deque at most once.