Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.023 s | 3 KBi |
#2 |
Correcto
|
0.023 s | 3 KBi |
#3 |
Correcto
|
0.021 s | 3 KBi |
#4 |
Correcto
|
0.022 s | 3 KBi |
#5 |
Correcto
|
0.024 s | 3 KBi |
#6 |
Correcto
|
0.026 s | 3 KBi |
#7 |
Correcto
|
0.023 s | 3 KBi |
#8 |
Correcto
|
0.052 s | 3 KBi |
#9 |
Correcto
|
0.048 s | 3 KBi |
#10 |
Correcto
|
0.051 s | 3 KBi |
#11 |
Correcto
|
0.047 s | 3 KBi |
#12 |
Correcto
|
0.046 s | 3 KBi |
#13 |
Correcto
|
0.051 s | 3 KBi |
#14 |
Correcto
|
0.052 s | 5 KBi |
#15 |
Correcto
|
0.052 s | 3 KBi |
#16 |
Correcto
|
0.05 s | 3 KBi |
#17 |
Correcto
|
0.055 s | 3 KBi |
#18 |
Correcto
|
0.045 s | 3 KBi |
# Cleaned up version def kadane(arr): """ Kadane's algorithm to find the max-sum sub-array in 1D in O(n) """ max_sum = last_sum = arr[0] for i in arr[1:]: last_sum = max(last_sum + i, i) if last_sum > max_sum: max_sum = last_sum return max_sum def get_max_sum(mat): nrows, ncols = len(mat), len(mat[0]) max_sum = mat[0][0] for up_i in range(nrows): sum_cols = [0] * ncols # running sum along columns for lower_row in mat[up_i:]: sum_cols = list(map(lambda x, y: x + y, sum_cols, lower_row)) # O(ncols) max_subcol = kadane(sum_cols) if max_subcol > max_sum: max_sum = max_subcol return max_sum if __name__ == "__main__": nrows, ncols = list(map(int, input().split())) mat = [list(map(int, input().split())) for _ in range(nrows)] print(get_max_sum(mat))