KGSS - Maximum Sum


You are given a sequence A[1], A[2], ..., A[N] ( 0 ≤ A[i] ≤ 10^8 , 2 ≤ N ≤ 10^5 ). There are two types of operations and they are defined as follows:

Update:

This will be indicated in the input by a 'U' followed by space and then two integers i and x.

U i x, 1 ≤ i ≤ N, and x, 0 ≤ x ≤ 10^8.

This operation sets the value of A[i] to x.

Query:

This will be indicated in the input by a 'Q' followed by a single space and then two integers i and j.

Q x y, 1 ≤ x < y ≤ N.

You must find i and j such that x ≤ i, j ≤ y and i != j, such that the sum A[i]+A[j] is maximized. Print the sum A[i]+A[j].

Input

The first line of input consists of an integer N representing the length of the sequence. Next line consists of N space separated integers A[i]. Next line contains an integer Q, Q ≤ 10^5, representing the number of operations. Next Q lines contain the operations.

Output

Output the maximum sum mentioned above, in a separate line, for each Query.

Example

Input:
5
1 2 3 4 5
6
Q 2 4
Q 2 5
U 1 6
Q 1 5
U 1 7
Q 1 5

Output:
7
9
11
12

Warning: large Input/Output data, be careful with certain languages

hide comments
souvik23: 2017-01-03 06:11:25

@gustavoaca1997 : The max size is 10^5 which is not a power of 2 ...... You have to take the nearest power of 2 greater than 10^5 i.e. 131072 and so your tree size must be 2*131072 which is > 2*10^5.That's why it didn't work:)

lincolnbf: 2016-10-07 01:51:21

gustavoaca the size of seg tree can go upto 4N for an array of size N.

gustavoaca1997: 2016-09-15 07:34:13

got RE because the size of my tree was 2 * 100000 + somethingElse. I chose that size because of theory of Segment Tree ("there are total 2 * n - 1 nodes"). I had to put 3 * 100000. Can someone explain me why 2 * 100000 does not work?

S. M. Shakir Ahsan Romeo: 2016-07-26 20:15:45

Easy

Dilpreet: 2016-07-20 21:39:12

Take care of the space of the array and tree
Costed me 3 WAs

Last edit: 2016-07-22 10:17:52
iharsh234: 2016-07-08 21:14:18

store max and its pos in a range.
must for a beginers.

lt: 2016-07-07 12:13:24

Awesome! Ac in one shot :D

Deepak : 2016-06-30 13:54:19

AC on one go... i used N=100010 and for tree 4*N...easy

Last edit: 2016-06-30 13:54:34
aryan_r: 2016-06-25 16:57:22

There can be at most Q-1 updates and last one might be a Query . So use N=2*10^5 :) else Run time error !

karthik1997: 2016-05-30 06:58:28

Used array of size 2*10^5 and tree of size 520000 and got AC directly . Else you will get Run-time error... Good Luck :D ..Way to go segment trees .....


Added by:Swarnaprakash
Date:2009-01-10
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Kurukshetra 09 OPC