Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.022 s | 3 KBi |
#2 |
Correcto
|
0.022 s | 3 KBi |
#3 |
Correcto
|
0.024 s | 3 KBi |
#4 |
Correcto
|
0.024 s | 3 KBi |
#5 |
Correcto
|
0.023 s | 3 KBi |
#6 |
Correcto
|
0.025 s | 3 KBi |
#7 |
Correcto
|
0.021 s | 3 KBi |
#8 |
Correcto
|
0.053 s | 3 KBi |
#9 |
Correcto
|
0.052 s | 3 KBi |
#10 |
Correcto
|
0.05 s | 3 KBi |
#11 |
Correcto
|
0.057 s | 3 KBi |
#12 |
Correcto
|
0.055 s | 3 KBi |
#13 |
Correcto
|
0.055 s | 3 KBi |
#14 |
Correcto
|
0.049 s | 3 KBi |
#15 |
Correcto
|
0.056 s | 3 KBi |
#16 |
Correcto
|
0.057 s | 3 KBi |
#17 |
Correcto
|
0.055 s | 3 KBi |
#18 |
Correcto
|
0.052 s | 3 KBi |
def kadane(arr): """ Kadane's algorithm to find the max-sum sub-array """ max_sum = last_sum = arr[0] for i in arr[1:]: last_sum = max(last_sum + i, i) # branchless assignment (faster than the IF statement): # decision = int(last_sum > max_sum) # max_sum = decision * last_sum + (1 - decision) * max_sum # # branched assignment (slower but easier to read): 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, upper_row in enumerate(mat): sum_cols = [0] * ncols # running sum along columns for lo_i, lower_row in enumerate(mat[up_i:], start=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))