defDivideAndConquer(lst, left, right): if left == right: if lst[left] > 0: return lst[left] else: return0
center = (left + right) // 2 MaxLeftSum = DivideAndConquer(lst, left, center) MaxRightSum = DivideAndConquer(lst, center + 1, right)
MaxLeftBorderSum = LeftBorderSum = 0 i = center while i >= left: LeftBorderSum += lst[i] if LeftBorderSum > MaxLeftBorderSum: MaxLeftBorderSum = LeftBorderSum i -= 1
MaxRightBorderSum = RightBorderSum = 0 i = center + 1 while i <= right: RightBorderSum += lst[i] if RightBorderSum > MaxRightBorderSum: MaxRightBorderSum = RightBorderSum i += 1
defmain(): n = int(input()) lst = input() lst = [int(e) for e in lst.split()] print(DivideAndConquer(lst, 0, n - 1)) main()
O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defOnlineProcessing(list): # list = map(int, list.split()) # map 可遍历,但是不能根据位置取数据。 list = [int(e) for e inlist.split()] ThisSum = MaxSum = 0 for e inlist: ThisSum += e if ThisSum > MaxSum: MaxSum = ThisSum elif ThisSum < 0: ThisSum = 0 print(MaxSum) defmain(): n = int(input()) if n > 0: list = input() OnlineProcessing(list) main()