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

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

hide comments
2014-09-02 17:48:50 abdelkarim
easy 1!
2014-07-29 21:44:24 |RAMSDEN|
nice problem:)
2014-07-02 10:06:55 Archit Jain
very good question..
2014-06-26 00:13:55 JordanBelfort
njyed doin ds one
2014-02-01 20:44:37 GreyHaze
Judge is not fair for java.. java users please take note.. If you use BufferedReader/Scanner, you'll have TLE... use DataInputStream...
2013-06-19 12:43:54 mayank goyal
Can anyone look at my code plz..I am getting TLE.Link to my code is <snip>

Last edit: 2022-09-10 16:52:48
2013-06-19 12:21:35 mayank goyal
getting TLE using segment trees in java
2013-03-13 18:24:58 Swarnaprakash
Sabarinath (npsabari@) and Nikola Jovanovic (randomusername@)

I verified the input constraints and my solution and the number of elements in less than 10^5.

Your solutions failed because you had assumed that the data structure you used will contain atmost twice the number of elements. It will actually contain twice the nearest power of 2 greater than n.
I took your solutions and fixed that and retained 10^5 and it got accepted.
2013-02-28 11:47:45 RandomUsername
@npsabari's comment is true. I had the same problem with N>10^5. Just make the array size 2*10^5 and it will work.

Last edit: 2013-02-28 11:48:30
2012-12-30 18:39:38 Janani
Should this be done using segment tree?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.