█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 4168

Problema 0x94 - Subarreglo de máxima suma

  • Autor: Serivt
  • Fecha: 2021-05-24 17:38:59 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.024 s 3 KBi
#2
Correcto
0.023 s 3 KBi
#3
Correcto
0.019 s 3 KBi
#4
Correcto
0.019 s 3 KBi
#5
Correcto
0.024 s 5 KBi
#6
Correcto
0.021 s 3 KBi
#7
Correcto
0.021 s 3 KBi
#8
Correcto
0.028 s 3 KBi
#9
Correcto
0.021 s 3 KBi
#10
Correcto
0.016 s 3 KBi
#11
Correcto
0.028 s 3 KBi
#12
Correcto
0.021 s 3 KBi
#13
Correcto
0.019 s 3 KBi
#14
Correcto
0.021 s 3 KBi
#15
Correcto
0.03 s 3 KBi
#16
Correcto
0.028 s 4 KBi
#17
Correcto
0.608 s 12 KBi
#18
Correcto
0.651 s 12 KBi
#19
Correcto
0.629 s 12 KBi
#20
Correcto
0.89 s 12 KBi
#21
Correcto
0.566 s 11 KBi
#22
Correcto
0.656 s 12 KBi
#23
Correcto
0.571 s 10 KBi
#24
Correcto
0.565 s 11 KBi
Puntos totales: 100 / 100

Código

def get_max_sum_subarray(arr):
    length = len(arr)
    if length == 1:
        return arr[0]
    half = length // 2
    left = get_max_sum_subarray(arr[:half])
    right = get_max_sum_subarray(arr[half:])
    best = max(left, right)
    # middle
    best_left, sum_left = arr[half - 1], arr[half - 1]
    best_right, sum_right = arr[half], arr[half]
    for i in range(half - 2, -1, -1):
        sum_left += arr[i]
        best_left = max(best_left, sum_left)
    for i in range(half + 1, length):
        sum_right += arr[i]
        best_right = max(best_right, sum_right)
    return max(best, best_left + best_right)

n = int(input())
a = [int(i) for i in input().split()]
print(get_max_sum_subarray(a))