Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.024 s | 3 KBi |
#2 |
Correcto
|
0.026 s | 3 KBi |
#3 |
Correcto
|
0.027 s | 3 KBi |
#4 |
Correcto
|
0.032 s | 3 KBi |
#5 |
Correcto
|
0.023 s | 3 KBi |
#6 |
Correcto
|
0.024 s | 3 KBi |
#7 |
Correcto
|
0.025 s | 3 KBi |
#8 |
Tiempo límite excedido
|
1.542 s | 3 KBi |
#9 |
Correcto
|
1.311 s | 3 KBi |
#10 |
Correcto
|
1.416 s | 3 KBi |
#11 |
Correcto
|
1.467 s | 3 KBi |
#12 |
Correcto
|
1.23 s | 3 KBi |
#13 |
Tiempo límite excedido
|
1.555 s | 3 KBi |
#14 |
Tiempo límite excedido
|
1.518 s | 3 KBi |
#15 |
Correcto
|
1.388 s | 3 KBi |
#16 |
Correcto
|
1.392 s | 3 KBi |
#17 |
Tiempo límite excedido
|
1.531 s | 3 KBi |
#18 |
Correcto
|
1.153 s | 3 KBi |
import math def smsum(mat): N, M = len(mat), len(mat[0]) for r in range(1, N): mat[r][0] += mat[r - 1][0] for c in range(1, M): mat[0][c] += mat[0][c - 1] for r in range(1, N): for c in range(1, M): mat[r][c] += mat[r - 1][c] mat[r][c] += mat[r][c - 1] mat[r][c] -= mat[r - 1][c - 1] def query(m, r1, c1, r2, c2): if r1 == c1 == 0: return m[r2][c2] if r1 == 0: return m[r2][c2] - m[r2][c1 - 1] if c1 == 0: return m[r2][c2] - m[r1 - 1][c2] return m[r2][c2] - m[r1 - 1][c2] - m[r2][c1 - 1] + m[r1 - 1][c1 - 1] def max_submatrix(m): smsum(m) N, M = len(m), len(m[0]) mx = -math.inf for r in range(N): for c in range(M): for rr in range(r, N): for cc in range(c, M): mx = max(mx, query(m, r, c, rr, cc)) return mx if __name__ == "__main__": N, M = map(int, input().split(" ")) mat = [] for _ in range(N): row = map(int, input().split(" ")) mat.append(list(row)) print(max_submatrix(mat))