Thief and Warehouses

4.3

64 votes
Arrays, Data Structures, Medium, One-dimensional, Stacks
Problem

There are N warehouses. The warehouses are located in a straight line and are indexed from 1 to N. Each warehouse contains some number of sacks.

A thief decides to rob these warehouses. Thief figured out that he can escape the police if and only if he follows both the following 2 constraints:

  • He will rob only one continuous segment of warehouses.
  • He will rob same number of sacks from each warehouse.

Thief wants to calculate the maximum number of sacks he can steal without getting caught by the police.

Input Format:

The first line contains an integer T denoting number test cases.

The first line of each test case contains a single integer N denoting number of warehouses.

The second line of each test case contains N space-separated integers :a[1],a[2],a[3]...a[n].a[i] denotes number of sacks in ith warehouse.

Constraints:

  • 1<=T<=5
  • 1<=N<=106
  • 0<=A[i]<=1012

Output Format:

Output exactly T lines.

The ith line should contain a single integer, i.e the answer for ith testcase(maximum number of sacks thief can steal without getting caught).

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In first test case thief will steal 2 sacks from each warehouse from 1 to 4.

Editor Image

?