CF1750B.Maximum Substring

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Maximum Substring

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

For some non-empty substring^\dagger tt of string ss containing xx characters 0 and yy characters 1, define its cost as:

  • xyx \cdot y, if x>0x \gt 0 and y>0y \gt 0;
  • x2x^2, if x>0x \gt 0 and y=0y = 0;
  • y2y^2, if x=0x = 0 and y>0y \gt 0.

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

^\dagger A string aa is a substring of a string bb if aa can be obtained from bb 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 tt (1t1051 \leq t \leq 10^5) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

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 111111. It contains 33 characters 1 and 00 characters 0. So a=3a = 3, b=0b = 0 and its cost is 32=93^2 = 9.

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

In the third test case, we can can take a substring 11111111 and its cost is 42=164^2 = 16.

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

In the fifth test case, we can take a substring 000000 and its cost is 33=93 \cdot 3 = 9.

In the sixth test case, we can only take the substring 00 and its cost is 11=11 \cdot 1 = 1.

Samples

6
5
11100
7
1100110
6
011110
7
1001010
4
1000
1
0
9
12
16
12
9
1

在线编程 IDE

建议全屏模式获得最佳体验