# e-olymp 2906. Can you answer these queries — 1

You are given an integer sequence.

$a_1, a_2, \ldots, a_n (|a_i| \le 15007, 1 \le n \le 50000)$.

A query is defined as follows:

$Query(x, y) = MAX (a_i + a_{i+1} + \ldots + a_j, x \le i \le j \le y)$

Given $m$  queries, your program must output the results of these queries.

### Input

The first line contains the integer $n$. In the second line $n$ integers of the sequence are given. The third line contains the integer $m$. Then $m$ lines follow, where line $i$ contains two numbers $x_i$ and $y_i$.

### Output

Print the results of the $m$ queries, one query per line.

### Tests

 № Input Output 1 3 -1 2 3 1 1 2 2 2 5 1 2 3 4 5 1 1 5 15 3 5 -1 -2 -3 -4 -5 1 1 5 -1 4 8 1 2 -3 -2 10 1 -15 7 4 1 1 3 4 3 6 1 8 1 -2 11 11 5 9 1 -2 3 -4 5 -6 7 -8 9 3 1 9 2 8 2 4 9 7 3

### Algorithm

After analyzing the condition of the problem, we can understand that we need on a given segments of array to find subsegment with a maximum amount. We can solve this task by using segment tree, but before this we should modify it a bit. Create a structure of which our tree consists:

That is, in each vertex of tree we store $4$ values:
• the maximum amount among all prefixes
• the maximum amount among all suffixes
• the sum on the segment

First we need to get the values in the leaves. It’s simple enough all $4$ fields take the value of the number, that corresponds to this leaf. Now, having values in the leaves, we need to learn how to get the values in parent. For this we use the function Node mergeNodes(Node leftChild, Node rightChild), which assign to parent’s fields such values:

• Answer in the parent equal to the answer in the right or the left child (means that the best subsegment of the current node is entirely in the left or right child), or the maximum amount of the maximum suffix in the left child and the maximum prefix in the right child (which means that it is partially is in the left child and partially is in the right child).
• The maximum amount of the parent on prefix is equal to the maximum prefix of left child or sum on segment of left child and maximum prefix of right child.
• The maximum amount of the parent on suffix is equal to the maximum suffix of right child or sum on segment of right child and maximum suffix of right child.
• Amount on segment of the parent is equal to the sum on segment of left child and sum on segment of right child.

Now, with all the auxiliary functions for working with a built data structure, we need only to construct a tree and learn to get an answer on it.
We need two more functions:

• void build(int *base, Node *tree, int currentNode, int left, int right) — recursive construction of a tree from the leaves to the root by initial sequence numbers.
• Node answer(Node *tree, int currentNode, int left, int right, int leftBorder, int rightBorder) — as in the usual tree segments we get an answer going down by the tree, but instead of any associative function (eg sum or maximum) we execute the merger of nodes described earlier. Resulting node will store an answer corresponding to a given in the query segment.

Rest details of the implementation can be found in the code of the program or in the sources of information listed at the end of the article.