/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
(String[] args
) { int [] input = {1, 0, 0, 1, 0, 0, 1,1,1,1, 0,1};
System.
out.
println(findMaxNumberOfOnes
(input,
0, input.
length-1)); }
private static int findMaxNumberOfOnes(int[] input, int i, int j) {
if (i==j)
return 1;
int option1 = input[i] + findMaxNumberOfOnes(input, i+1, j);
int option2 = count(input , i , j, true);
int option3 = count(input, i, j, false);
int option4 =findMaxNumberOfOnes(input, i, j-1) +input[j];
return Math.
max(option1,
Math.
max(option2,
Math.
max(option3,option4
))); }
private static int count(int[] input, int i, int j, boolean flipped) {
int a = flipped?0:1;
int count = 0;
while (i<=j){
count += (input[i++]==a)?1:0;
}
return count;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCQlpbnQgW10gaW5wdXQgPSB7MSwgMCwgMCwgMSwgMCwgMCwgMSwxLDEsMSwgMCwxfTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oZmluZE1heE51bWJlck9mT25lcyhpbnB1dCwwLCBpbnB1dC5sZW5ndGgtMSkpOwoJfQoKCXByaXZhdGUgc3RhdGljIGludCBmaW5kTWF4TnVtYmVyT2ZPbmVzKGludFtdIGlucHV0LCBpbnQgaSwgaW50IGopIHsJCQoJCWlmIChpPT1qKQoJCQlyZXR1cm4gMTsKCQlpbnQgb3B0aW9uMSA9IGlucHV0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