eolymp
bolt
Try our new interface for solving problems

MAXCOMP

For a matrix, let’s call a subset of cells, S, connected if there is a path between any two cells of S which consists only of cells from S. A path is a sequence of cells u1, u2, ..., uk where ui and ui+1 are adjacent for any i = 1,k-1

Given a matrix A with N rows and M columns, we define the following formula for a connected subset S of A: weight(S) = max{A(s)|s∈S}-|S|. where |*| represents the cardinality of a set and A(s) represents the value of the cell s in A.

Zaman məhdudiyyəti 0.5 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
2 3
2 4 3
5 7 5
Çıxış verilənləri #1
2