GSS1 - Can you answer these queries I


You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows:
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.
Given M queries, your program must output the results of these queries.

Input

  • The first line of the input file contains the integer N.
  • In the second line, N numbers follow.
  • The third line contains the integer M.
  • M lines follow, where line i contains 2 numbers xi and yi.

Output

Your program should output the results of the M queries, one query per line.

Example

Input:
3 
-1 2 3
1
1 2

Output:
2

hide comments
akshita09: 2019-03-22 12:51:33

i am getting TLE in java

divyansh_15: 2019-03-22 10:04:43

Any JAVA CODE will never get AC. SPOJ IS NOT FOR JAVA PROGRAMMERS. :(

md_meraj1319: 2019-03-19 18:42:38

Last edit: 2019-03-19 18:43:34
revpro: 2019-03-19 10:14:47

can somebody provide edge cases I'm getting WA on 9th.
thanks in advance

letuantam96: 2019-03-18 18:25:33

I got O(n) complexity and somehow i still got LTE. Guess i cant use normal DP =))

ab_biswas09: 2019-03-15 22:17:02

AC in One GO

shawnliang: 2019-03-12 02:09:35

AC in 0.09s with c++ ios_base::sync_with_stdio(false) and std::cin.tie(NULL) and cout.flush().

no need to use the old school scanf and printf.

Last edit: 2019-03-14 03:52:00
trishala_naman: 2019-02-21 01:07:01

Try i/p:
10
-1 9 2 6 -4 3 5 2 -6 1
1
4 7
o/p: 10
*Use scanf , printf

sajalagrawal14: 2019-02-13 08:46:48

very strict time limit . also for c++ users dont use only '/n' it can cause ans wrong , instead use endl

Last edit: 2019-02-13 08:47:07
akj698: 2019-02-09 08:32:16

A must do problem :)


Added by:Nguyen Dinh Tu
Date:2006-11-01
Time limit:0.115s-0.230s
Source limit:5000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET