Akash has just learned the maximum subarray sum problem. And while thinking about the solution, he came up with a new problem, the maximum frequent subarray sum problem.
In this problem, you will be given an array AA of NN integers. You have to choose a non-empty subarray with the maximum possible score. The score of a subarray is calculated asscore(l,r)=(Al+⋯+Ar)⋅(occurrences)score(l,r)=(Al+⋯+Ar)⋅(occurrences)Here, occurrencesoccurrences is the number of occurrences of that subarray in AA.
Now Akash can’t solve this problem, so please help him solve it.
For the work from home Internship Programs : Click here to apply now
- The first line contains an integer TT, the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer nn, the size of the array.
- The second line contains nn integers A1,…,ANA1,…,AN.
For each test case, output the maximum possible score in a new line.
- The sum of nn over all test cases ≤3⋅105≤3⋅105
- Subtask 1 (10 points): 1≤n≤1001≤n≤100, the sum of nn over all test cases ≤300≤300
- Subtask 2 (15 points): 1≤n≤10001≤n≤1000, the sum of nn over all test cases ≤3000≤3000
- Subtask 3 (75 points): original constraints
2 6 10 8 -20 5 5 5 10 -5 1 7 -1 2 -4 10 0 -11 3
In the first test case, the maximum score is attained by subarray [5,5][5,5], its score is (5+5)⋅2=20(5+5)⋅2=20 since it occurs twice in AA.
In the second test case, the maximum score is attained by both subarrays [1,7,−1,2,−4,10][1,7,−1,2,−4,10] and [1,7,−1,2,−4,10,0][1,7,−1,2,−4,10,0]. Both have sum 1515 and occur once, so their scores are 15⋅1=1515⋅1=15.
Due to copyright issues we won't be able to update the solution here immediately. However you can download the code file from our telegram channel. So join our telegram channel for further updates. Keep tracking, the solution will be updated on this website soon.
- HACKEREARTH IS HIRING FOR INTERN || APPLY NOW
- INTEL IS HIRING FOR SOFTWARE DEVELOPER INTERN || APPLY Now
- DATA SCIENCE INTERNSHIP | SONY RESEARCH INDIA || APPLY NOW
- Cognizant Hiring | Graduate Trainee | APPLY NOW
- Deloitte Hiring | QA- Senior Analyst | APPLY NOW
- IBM Hiring | Technical Support Associate | APPLY NOW
- CRED | Freshers Hiring | Backend developer | APPLY NOW
- Axis bank | Internship Opportunity | Young Bankers Program
Read more post Here