GUPTA MECHANICAL

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

Sunday, 6 November 2022

[Solution] Maximum Substring Codeforces Solution



B. Maximum Substring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A binary string is a string consisting only of the characters 0 and 1. You are given a binary string s.

For some non-empty substring t of string s containing x characters 0 and y characters 1, define its cost as:

  • xy, if x>0 and y>0;
  • x2, if x>0 and y=0;
  • y2, if x=0 and y>0.

Given a binary string s of length n, find the maximum cost across all its non-empty substrings.

 A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

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

The first line of each test case contains a single integer n (1n2105) — the length of the string s.

The second line of each test case contains a binary string s of length n.

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

Output

For each test case, print a single integer — the maximum cost across all substrings.

Note

In the first test case, we can take a substring 111. It contains 3 characters 1 and 0 characters 0. So a=3b=0 and its cost is 32=9.

In the second test case, we can take the whole string. It contains 4 characters 1 and 3 characters 0. So a=4b=3 and its cost is 43=12.

In the third test case, we can can take a substring 1111 and its cost is 42=16.

In the fourth test case, we can take the whole string and cost is 43=12.

In the fifth test case, we can take a substring 000 and its cost is 33=9.

In the sixth test case, we can only take the substring 0 and its cost is 11=1

No comments:

Post a Comment