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>
using namespace std;
const int N = 1005;
const long long INF = 1e18;
int n, m;
long long a[N][N];
long long f1[N][N], f2[N][N];
long long res;
void reset() {
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= m + 1; ++j) {
f1[i][j] = f2[i][j] = -INF;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
res = -INF;
reset();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
f1[i][j] = max(max(f1[i - 1][j], f1[i][j - 1]), -a[i][j] + i + j);
f2[i][j] = max(max(f2[i - 1][j], f2[i][j - 1]), +a[i][j] + i + j);
res = max(res, f1[i][j] + a[i][j] - i - j);
res = max(res, f2[i][j] - a[i][j] - i - j);
}
}
reset();
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 1; --j) {
f1[i][j] = max(max(f1[i - 1][j], f1[i][j + 1]), -a[i][j] + i - j);
f2[i][j] = max(max(f2[i - 1][j], f2[i][j + 1]), +a[i][j] + i - j);
res = max(res, f1[i][j] + a[i][j] - i + j);
res = max(res, f2[i][j] - a[i][j] - i + j);
}
}
reset();
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= m; ++j) {
f1[i][j] = max(max(f1[i + 1][j], f1[i][j - 1]), -a[i][j] - i + j);
f2[i][j] = max(max(f2[i + 1][j], f2[i][j - 1]), +a[i][j] - i + j);
res = max(res, f1[i][j] + a[i][j] + i - j);
res = max(res, f2[i][j] - a[i][j] + i - j);
}
}
reset();
for (int i = n; i >= 1; --i) {
for (int j = m; j >= 1; --j) {
f1[i][j] = max(max(f1[i + 1][j], f1[i][j + 1]), -a[i][j] - i - j);
f2[i][j] = max(max(f2[i + 1][j], f2[i][j + 1]), +a[i][j] - i - j);
res = max(res, f1[i][j] + a[i][j] + i + j);
res = max(res, f2[i][j] - a[i][j] + i + j);
}
}
cout << res - 1;
}
# | 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... |