class Solution(object):
    def maxSatisfied(self, customers, grumpy, X):
        """
        :type customers: List[int]
        :type grumpy: List[int]
        :type X: int
        :rtype: int
        """

        res, temp, tMax, que = sum(customers[i] for i in xrange(len(customers)) if grumpy[i] == 0), 0, 0, []

        for i in xrange(len(customers)):
            if len(que) >= X:
                idx = que.pop(0)
                if grumpy[idx]:
                    temp -= customers[idx]

            que.append(i)
            if grumpy[i]:
                temp += customers[i]
            tMax = max(tMax, temp)

        return res + tMax







if __name__ == '__main__':
    print Solution().maxSatisfied([2, 6, 6, 9], [0, 0, 1, 1], 1)
    print Solution().maxSatisfied([1, 0, 1, 2, 1, 1, 7, 5], [0, 1, 0, 1, 0, 1, 0, 1], 3)
    print Solution().maxSatisfied([1, 1, 1], [0, 0, 0], 2)
    print Solution().maxSatisfied([4, 10, 10], [1, 1, 0], 2)
    print Solution().maxSatisfied([1], [0], [1])
