Iahub got bored, so he invented a game to be played on paper.
 He writes n integers a1, a2, ..., an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values ak for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 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.
```
@param {Array} line of the input there are n integers: a1, a2, ..., an. 1 ≤ n ≤ 100. It is guaranteed that each of those n values is either 0 or 1
@return {Integer} the maximal number of 1s that can be obtained after exactly one move
```
Examples :
```
[1, 0, 0, 1, 0, 0]  =>  5
[1, 0, 0, 1]  =>  4

```
Note:
 In the first case, flip the segment from 2 to 6 (i = 2, j = 6). That flip changes the sequence, it becomes: [1 1 1 0 1 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1 1].
 In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.

(c)ll931110 & fchirica

Difficulty: introductory

Starter code:

def flipping_game(num):
	

Solutions:
def flipping_game(num):
    current = 0
    biggest = 0
    for i in num:
        current = max(0, current - ( i or -1 ) )
        biggest = max(biggest, current)
        
    return sum(num) + (biggest or -1)

def flipping_game(num):
    return max([sum(num) + num[i: j + 1].count(0) - num[i: j + 1].count(1) for i in range(len(num)) for j in range(i, len(num))])

def flipping_game(a):
    current = best = 0
    for n in a:
        current = max(0, current - (n or -1))
        best = max(best, current)
    return sum(a) + (best or -1)

def flipping_game(num):
    
    # Defining possible start and end points, to reduce complexity away from full 0(n^2)
    poss_start = [i for i,x in enumerate(num) if x==0 and (i==0 or num[i-1]==1)]
    poss_end = [i for i,x in enumerate(num) if x==0 and (i==len(num)-1 or num[i+1]==1)]
    
    # If no start or end available, then every change is for the worse, so one 1 needs to be converted to 0
    if not poss_start or not poss_end: return sum(num) - 1
    
    curmax = 0
    for st in poss_start:
        for en in poss_end:
            curmax = max(curmax,sum(x if (i<st or i>en) else 1-x for i,x in enumerate(num)))
    return curmax

def flipping_game(num):
    if all(num):
        return len(num) - 1
    x = 0
    m = 0
    for n in num:
        if n == 0:
            x += 1
            m = max(x, m)
        else:
            x = max(x-1, 0)
    return num.count(1) + m

from itertools import groupby

def flipping_game(lst):
    split = [ (k, sum(1 for _ in g)) for k,g in groupby(lst)]
    zeroes = [i for i,t in enumerate(split) if not t[0]]
    return max( (sum((k^1 if a<=i<=b else k)*n for i,(k,n) in enumerate(split))
                          for z,a in enumerate(zeroes)
                          for b in zeroes[z:]),
                      default=sum(lst)-1 )

def flipping_game(num):
    all_pairs = ((i, j) for i in range(len(num)) for j in range(i + 1, len(num) + 1))
    ones = (sum(num[:i]) + j - i - sum(num[i:j]) + sum(num[j:]) for i, j in all_pairs)
    return max(ones) 

flipping_game=lambda a:sum(a)+max(j-i-2*sum(a[i:j])for j in range(1,len(a)+1)for i in range(j))

def flipping_game(a):
    m = 0
    for i in range(len(a)):
        for j in range(i,len(a)+1):
            x = sum(a[:i]) + a[i:j+1].count(0) + sum(a[j+1:])
            if x > m:
                m = x
    return m       

def flipping_game(num):
    if 0 not in num:
        return len(num)-1
    elif num==[0]:
        return 1
    m=-1
    for i in range(len(num)-1):
        for j in range(i+1,len(num)+1):
            c=num[:i].count(1)+num[i:j].count(0)+num[j:].count(1)
            if c>m:
                m=c
    return m