Codeforces Round #191 (Div. 2) --- A. Flipping Game,

Source: Internet
Author: User

Codeforces Round #191 (Div. 2) --- A. Flipping Game,

Flipping Game
Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Iahub got bored, so he has Ted a game to be played on paper.

He writesNIntegersA1, bytes,A2, middle..., middle ,...,AN. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indicesIAndJ(1 digit ≤ DigitILimit ≤ limitJLimit ≤ limitN) And flips all valuesAKFor which their positions are in range [I, Bytes,J] (That isILimit ≤ limitKLimit ≤ limitJ). Flip the valueXMeans to apply operationXBytes = bytes 1-X.

The goal of the game is that after exactly one 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 integerN(1 digit ≤ DigitNLimit ≤ limit 100). In the second line of the input there areNIntegers:A1, bytes,A2, middle..., middle ,...,AN. It is guaranteed that each of thoseNValues 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,JNumbers = numbers 3) will turn all numbers into 1.







Solution: the question is that there is a sequence 0, 1, which allows you to reverse any subsequence and ask how many subsequences can be obtained after the operation.

The data is not big and direct violence. Perform Two-layer cyclic enumeration to retrieve the start and end points of the reverse sequence, count the number of 1 in each case, and save a maximum value.






AC code:

# Include <iostream> # include <cstdio> using namespace std; int a [105]; int main () {// freopen ("in.txt", "r", stdin ); int n; while (cin> n) {for (int I = 0; I <n; I ++) cin> a [I]; int ans = 0; for (int I = 0; I <n; I ++) {for (int j = I; j <n; j ++) {int sum = 0; for (int k = 0; k <n; k ++) {if (k> = I & k <= j) sum + = a [k] ^ 1; // returns the inverse else sum + = a [k];} if (ans <sum) ans = sum ;}} cout <ans <endl ;} return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.