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

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

hide comments
2019-03-22 10:04:43
Any JAVA CODE will never get AC. SPOJ IS NOT FOR JAVA PROGRAMMERS. :(
2019-03-19 10:14:47
can somebody provide edge cases I'm getting WA on 9th.
thanks in advance
2019-03-18 18:25:33
I got O(n) complexity and somehow i still got LTE. Guess i cant use normal DP =))
2019-03-15 22:17:02
AC in One GO
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
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
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
2019-02-09 08:32:16
A must do problem :)
2019-02-02 14:53:31
TLE after 8th test-case in Java, used fast i/o. Any other way to resolve it?

Last edit: 2019-02-02 14:54:25
2019-01-16 14:12:30
Please help for java, getting tle!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.