import java.util.Scanner;
public class Main {
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in); int t = scanner.nextInt(); // Number of test cases
while (t-- > 0) {
int n = scanner.nextInt(); // Number of contests
int[] a = new int[n]; // Ratings of contests
// Read contest ratings
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// Calculate and print the result for each test case
System.
out.
println(maximizeRating
(n, a
)); }
scanner.close();
}
private static int maximizeRating(int n, int[] a) {
// Step 1: Calculate initial rating x
int x = 0;
for (int i = 0; i < n; i++) {
if (a[i] > x) {
x++;
} else if (a[i] < x) {
x--;
}
}
// Step 2: Calculate the gain array based on the contest ratings
int[] gain = new int[n];
for (int i = 0; i < n; i++) {
if (a[i] > x) {
gain[i] = -1; // Loss (if we consider this contest)
} else if (a[i] < x) {
gain[i] = 1; // Gain (if we consider this contest)
} else {
gain[i] = 0; // No change (if we consider this contest)
}
}
// Step 3: Apply Kadane's algorithm to find the maximum subarray sum (optimal interval to skip)
int maxGain = 0; // Max gain from skipping a subarray
int currentMax = 0; // Current sum while traversing the array
for (int i = 0; i < n; i++) {
currentMax
= Math.
max(gain
[i
], currentMax
+ gain
[i
]); // Kadane's logic maxGain
= Math.
max(maxGain, currentMax
); // Update the maximum gain }
// Step 4: Return the initial rating + the maximum gain from skipping an optimal subarray
return x + maxGain;
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCB0ID0gc2Nhbm5lci5uZXh0SW50KCk7ICAvLyBOdW1iZXIgb2YgdGVzdCBjYXNlcwogICAgICAgIAogICAgICAgIHdoaWxlICh0LS0gPiAwKSB7CiAgICAgICAgICAgIGludCBuID0gc2Nhbm5lci5uZXh0SW50KCk7ICAvLyBOdW1iZXIgb2YgY29udGVzdHMKICAgICAgICAgICAgaW50W10gYSA9IG5ldyBpbnRbbl07ICAvLyBSYXRpbmdzIG9mIGNvbnRlc3RzCiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBSZWFkIGNvbnRlc3QgcmF0aW5ncwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICAgICAgYVtpXSA9IHNjYW5uZXIubmV4dEludCgpOwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBDYWxjdWxhdGUgYW5kIHByaW50IHRoZSByZXN1bHQgZm9yIGVhY2ggdGVzdCBjYXNlCiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihtYXhpbWl6ZVJhdGluZyhuLCBhKSk7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHNjYW5uZXIuY2xvc2UoKTsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBpbnQgbWF4aW1pemVSYXRpbmcoaW50IG4sIGludFtdIGEpIHsKICAgICAgICAvLyBTdGVwIDE6IENhbGN1bGF0ZSBpbml0aWFsIHJhdGluZyB4CiAgICAgICAgaW50IHggPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGlmIChhW2ldID4geCkgewogICAgICAgICAgICAgICAgeCsrOwogICAgICAgICAgICB9IGVsc2UgaWYgKGFbaV0gPCB4KSB7CiAgICAgICAgICAgICAgICB4LS07CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIC8vIFN0ZXAgMjogQ2FsY3VsYXRlIHRoZSBnYWluIGFycmF5IGJhc2VkIG9uIHRoZSBjb250ZXN0IHJhdGluZ3MKICAgICAgICBpbnRbXSBnYWluID0gbmV3IGludFtuXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBpZiAoYVtpXSA+IHgpIHsKICAgICAgICAgICAgICAgIGdha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