#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define maxn 1005
using namespace std;
int a[maxn][maxn], val[maxn][maxn];
int n, m;
inline int ab(int a) {
return a > 0 ? a : -a;
}
const int NINF = -1000010000;
/*
struct BIT{
int bit[maxn][maxn];
void init(){
for (int i = 0;i < maxn;i++) {
for (int j = 0;j < maxn;j++) bit[i][j] = -NINF;
}
}
void modify(int x, int y, int val) {
for (;x < maxn;x += x & (-x)) {
for(int c = y;c < maxn;c += c & (-c)) bit[x][c] = max(bit[x][c], val);
}
}
int query(int x, int y) {
int ret = NINF;
for (;x > 0;x -= x & (-x)) {
for (int c = y;c > 0;c -= c & (-c)) ret = max(ret, bit[x][c]);
}
return ret;
}
} bit;
*/
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) cin >> a[i][j];
}
int ans = NINF;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
val[i][j] = max(-a[i][j] + i + j, max(i ? val[i - 1][j] : NINF, j ? val[i][j - 1] : NINF));
//cout << val[i][j] << " ";
ans = max(ans, a[i][j] - i - j - 1 + val[i][j]);
}
//cout << endl;
}
//cout << ans << endl;
for (int i = n - 1;i >= 0;i--) {
for (int j = 0;j < m;j++) {
val[i][j] = max(-a[i][j] - i + j, max(i < n - 1 ? val[i + 1][j] : NINF, j ? val[i][j - 1] : NINF));
ans = max(ans, a[i][j] + i - j - 1 + val[i][j]);
}
}
for (int i = 0;i < n;i++) {
for (int j = m - 1;j >= 0;j--) {
val[i][j] = max(-a[i][j] + i - j, max(i ? val[i - 1][j] : NINF, j < m - 1 ? val[i][j + 1] : NINF));
ans = max(ans, a[i][j] - i + j - 1 + val[i][j]);
}
}
for (int i = n - 1;i >= 0;i--) {
for (int j = m - 1;j >= 0;j--) {
val[i][j] = max(-a[i][j] - i - j, max(i < n - 1 ? val[i + 1][j] : NINF, j < m - 1 ? val[i][j + 1] : NINF));
ans = max(ans, a[i][j] + i + j - 1 + val[i][j]);
}
}
cout << ans << endl;
}
/*
1 5
2 4 5 6 3
2 3
2 4 3
5 7 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
16 |
Correct |
1 ms |
768 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
135 ms |
8184 KB |
Output is correct |
19 |
Correct |
138 ms |
8184 KB |
Output is correct |
20 |
Correct |
130 ms |
8184 KB |
Output is correct |
21 |
Correct |
138 ms |
8312 KB |
Output is correct |
22 |
Correct |
135 ms |
8188 KB |
Output is correct |
23 |
Correct |
140 ms |
8184 KB |
Output is correct |
24 |
Correct |
136 ms |
7800 KB |
Output is correct |