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]; // Performance ratings in the contests
// Read the array input
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// Calculate and output the maximum possible rating
System.
out.
println(maximizeRating
(n, a
)); }
scanner.close();
}
// Function to calculate the maximum possible rating after skipping optimally
private static int maximizeRating(int n, int[] a) {
// Step 1: Calculate initial rating without any skips
int x = 0;
for (int i = 0; i < n; i++) {
if (a[i] > x) {
x++;
} else if (a[i] < x) {
x--;
}
}
// Step 2: Use prefix sum difference to determine optimal gain from skipping
int[] gain = new int[n];
for (int i = 0; i < n; i++) {
if (a[i] > x) {
gain[i] = -1; // Rating drop
} else if (a[i] < x) {
gain[i] = 1; // Rating increase
} else {
gain[i] = 0; // No change
}
}
// Step 3: Apply Kadane's Algorithm to find maximum subarray sum
int maxGain = 0;
int currentMax = 0;
for (int i = 0; i < n; i++) {
currentMax
= Math.
max(gain
[i
], currentMax
+ gain
[i
]); maxGain
= Math.
max(maxGain, currentMax
); }
// Step 4: Return the initial rating + maximum gain found
return x + maxGain;
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCB0ID0gc2Nhbm5lci5uZXh0SW50KCk7IC8vIE51bWJlciBvZiB0ZXN0IGNhc2VzCiAgICAgICAgCiAgICAgICAgd2hpbGUgKHQtLSA+IDApIHsKICAgICAgICAgICAgaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsgLy8gTnVtYmVyIG9mIGNvbnRlc3RzCiAgICAgICAgICAgIGludFtdIGEgPSBuZXcgaW50W25dOyAvLyBQZXJmb3JtYW5jZSByYXRpbmdzIGluIHRoZSBjb250ZXN0cwoKICAgICAgICAgICAgLy8gUmVhZCB0aGUgYXJyYXkgaW5wdXQKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgICAgIGFbaV0gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gQ2FsY3VsYXRlIGFuZCBvdXRwdXQgdGhlIG1heGltdW0gcG9zc2libGUgcmF0aW5nCiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihtYXhpbWl6ZVJhdGluZyhuLCBhKSk7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHNjYW5uZXIuY2xvc2UoKTsKICAgIH0KCiAgICAvLyBGdW5jdGlvbiB0byBjYWxjdWxhdGUgdGhlIG1heGltdW0gcG9zc2libGUgcmF0aW5nIGFmdGVyIHNraXBwaW5nIG9wdGltYWxseQogICAgcHJpdmF0ZSBzdGF0aWMgaW50IG1heGltaXplUmF0aW5nKGludCBuLCBpbnRbXSBhKSB7CiAgICAgICAgLy8gU3RlcCAxOiBDYWxjdWxhdGUgaW5pdGlhbCByYXRpbmcgd2l0aG91dCBhbnkgc2tpcHMKICAgICAgICBpbnQgeCA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaWYgKGFbaV0gPiB4KSB7CiAgICAgICAgICAgICAgICB4Kys7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoYVtpXSA8IHgpIHsKICAgICAgICAgICAgICAgIHgtLTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgLy8gU3RlcCAyOiBVc2UgcHJlZml4IHN1bSBkaWZmZXJlbmNlIHRvIGRldGVybWluZSBvcHRpbWFsIGdhaW4gZnJvbSBza2lwcGluZwogICAgICAgIGludFtdIGdhaW4gPSBuZXcgaW50W25dOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGlmIChhW2ldID4geCkgewogICAgICAgICAgICAgICAgZ2FpbltpXSA9IC0xOyAvLyBSYXRpbmcgZHJvcAogICAgICAgICAgICB9IGVsc2UgaWYgKGFbaV0gPCB4KSB7CiAgICAgICAgICAgICAgICBnYWluW2ldID0gMTsgIC8vIFJhdGluZyBpbmNyZWFzZQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZ2FpbltpXSA9IDA7ICAvLyBObyBjaGFuZ2UKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgLy8gU3RlcCAzOiBBcHBseSBLYWRhbmUncyBBbGdvcml0aG0gdG8gZmluZCBtYXhpbXVtIHN1YmFycmF5IHN1bQogICAgICAgIGludCBtYXhHYWluID0gMDsKICAgICAgICBpbnQgY3VycmVudE1heCA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgY3VycmVudE1heCA9IE1hdGgubWF4KGdhaW5baV0sIGN1cnJlbnRNYXggKyBnYWluW2ldKTsKICAgICAgICAgICAgbWF4R2FpbiA9IE1hdGgubWF4KG1heEdhaW4sIGN1cnJlbnRNYXgpOwogICAgICAgIH0KCiAgICAgICAgLy8gU3RlcCA0OiBSZXR1cm4gdGhlIGluaXRpYWwgcmF0aW5nICsgbWF4aW11bSBnYWluIGZvdW5kCiAgICAgICAgcmV0dXJuIHggKyBtYXhHYWluOwogICAgfQp9Cg==