GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Tuesday 6 September 2022

[Solution] Mainak and Array Codeforces Solution



A. Mainak and Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mainak has an array a1,a2,,an of n positive integers. He will do the following operation to this array exactly once:

  • Pick a subsegment of this array and cyclically rotate it by any amount.
Formally, he can do the following exactly once:
  • Pick two integers l and r, such that 1lrn, and any positive integer k.
  • Repeat this k times: set al=al+1,al+1=al+2,,ar1=ar,ar=al (all changes happen at the same time).

Mainak wants to maximize the value of (ana1) after exactly one such operation. Determine the maximum value of (ana1) that he can obtain.

Input

Each test contains multiple test cases. The first line contains a single integer t (1t50) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer n (1n2000).

The second line of each test case contains n integers a1,a2,,an (1ai999).

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

It is guaranteed that the sum of n over all test cases does not exceed 2000.

Output

For each test case, output a single integer — the maximum value of (ana1) that Mainak can obtain by doing the operation exactly once.


Note
  • In the first test case, we can rotate the subarray from index 3 to index 6 by an amount of 2 (i.e. choose l=3r=6 and k=2) to get the optimal array:So the answer is ana1=111=10.
  • In the second testcase, it is optimal to rotate the subarray starting and ending at index 1 and rotating it by an amount of 2.
  • In the fourth testcase, it is optimal to rotate the subarray starting from index 1 to index 4 and rotating it by an amount of 1. So the answer is 81=7.

No comments:

Post a Comment