Time limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard Input
Output
Standard output
Iahub got bored, so he has Ted a game to be played on paper.
He writes N IntegersA1, Bytes,A2, Please..., please ,...,AN .
Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices I AndJ ( 1 bytes ≤ bytesILimit ≤ limitJLimit ≤ limitN )
And flips all values AK For
Which their positions are in range [I, Bytes,J] (That isILimit ≤ limitKLimit ≤ limitJ ).
Flip the value X Means to apply operation XLimit = Limit 1 - X .
The goal of the game is that afterExactlyOne move to obtain the maximum number of ones. Write a program to solve the little game of iahub.
Input
The first line of the input contains an integer N ( 1 bytes ≤ bytesNLimit ≤ limit 100 ).
In the second line of the input there are N Integers:A1, Bytes,A2, Please..., please ,...,AN .
It is guaranteed that each of those N Values is either 0 or 1.
Output
Print an integer-the maximal number of 1 s that can be obtained after exactly one move.
Sample test (s) Input
51 0 0 1 0
Output
4
Input
41 0 0 1
Output
4
Note
In the first case, flip the segment from 2 to 5(ILimit = Limit 2, limit,JRequired = required 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains
Four ones. There is no way to make the whole sequence equal to [1 1 1 1].
In the second case, flipping only the second and the third element(ILimit = Limit 2, limit,JToken = token 3)Will turn all numbers into 1.
Explanation: This question determines how to select a range for the 01 transformation so that the number of 1 in the series is the most. You can use the brute force method to find a value for each interval selected, and finally find a maximum value.
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <set> # include <algorithm> using namespace STD; int main () {int n, a [100], I, J, K, c = 0, max = 0, B [100]; scanf ("% d ", & N); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]) ;}for (I = 0; I <n; I ++) {for (j = I; j <n; j ++) {for (k = 0; k <n; k ++) {B [k] = A [k] ;}for (k = I; k <= J; k ++) {B [k] = 1-B [k];} C = 0; For (k = 0; k <n; k ++) {If (B [k] = 1) {C ++ ;}} if (C> MAX) {max = C ;}} printf ("% d", max); return 0 ;}