This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e3 + 2;
const int inf = 1e9;
int a[N][N], p[N][N], q[N][N], r[N][N], s[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
p[i][j] = -a[i][j] + i + j;
if (i > 0) smax(p[i][j], p[i - 1][j]);
if (j > 0) smax(p[i][j], p[i][j - 1]);
}
}
for (int i = 0; i < n; i++) {
for (int j = m - 1; j >= 0; j--) {
q[i][j] = -a[i][j] + i - j;
if (i > 0) smax(q[i][j], q[i - 1][j]);
if (j < m - 1) smax(q[i][j], q[i][j + 1]);
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
r[i][j] = -a[i][j] - i + j;
if (i < n - 1) smax(r[i][j], r[i + 1][j]);
if (j > 0) smax(r[i][j], r[i][j - 1]);
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
s[i][j] = -a[i][j] - i - j;
if (i < n - 1) smax(s[i][j], s[i + 1][j]);
if (j < m - 1) smax(s[i][j], s[i][j + 1]);
}
}
int ans = -inf;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
smax(ans, a[i][j] - i - j + p[i][j] - 1);
smax(ans, a[i][j] - i + j + q[i][j] - 1);
smax(ans, a[i][j] + i - j + r[i][j] - 1);
smax(ans, a[i][j] + i + j + s[i][j] - 1);
}
}
cout << ans << en;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |