#include <bits/stdc++.h>
#define MAXN 1005
typedef long long int ll;
int N, M;
ll A[MAXN][MAXN], cnt[4][MAXN][MAXN];
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
std::cin >> N >> M;
for(int k=0; k<4; k++)
for(int i=0; i<=N+3; i++)
for(int j=0; j<=M+3; j++)
cnt[k][i][j] = INT_MIN;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
std::cin >> A[i][j];
cnt[0][i][j] = std::max(-A[i][j]+i+j, std::max(cnt[0][i-1][j], cnt[0][i][j-1]));
}
}
for(int i=1; i<=N; i++)
for(int j=M; j>0; j--)
cnt[1][i][j] = std::max(-A[i][j]+i-j, std::max(cnt[1][i-1][j], cnt[1][i][j+1]));
for(int i=N; i>0; i--)
for(int j=1; j<=M; j++)
cnt[2][i][j] = std::max(-A[i][j]-i+j, std::max(cnt[2][i+1][j], cnt[2][i][j-1]));
for(int i=N; i>0; i--)
for(int j=M; j>0; j--)
cnt[3][i][j] = std::max(-A[i][j]-i-j, std::max(cnt[3][i+1][j], cnt[3][i][j+1]));
ll ans=INT_MIN;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=M; j++)
{
ans = std::max(ans, A[i][j]-i-j+std::max(cnt[0][i-1][j], cnt[0][i][j-1]));
ans = std::max(ans, A[i][j]-i+j+cnt[1][i][j+1]);
ans = std::max(ans, A[i][j]+i-j+cnt[2][i+1][j]);
ans = std::max(ans, A[i][j]+i+j+cnt[3][i+1][j+1]);
}
}
std::cout << ans-1 << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
1536 KB |
Output is correct |
10 |
Correct |
4 ms |
1536 KB |
Output is correct |
11 |
Correct |
4 ms |
1536 KB |
Output is correct |
12 |
Correct |
4 ms |
1536 KB |
Output is correct |
13 |
Correct |
3 ms |
1536 KB |
Output is correct |
14 |
Correct |
4 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
1536 KB |
Output is correct |
13 |
Correct |
4 ms |
1536 KB |
Output is correct |
14 |
Correct |
4 ms |
1536 KB |
Output is correct |
15 |
Correct |
4 ms |
1536 KB |
Output is correct |
16 |
Correct |
3 ms |
1536 KB |
Output is correct |
17 |
Correct |
4 ms |
1536 KB |
Output is correct |
18 |
Correct |
173 ms |
40312 KB |
Output is correct |
19 |
Correct |
193 ms |
40540 KB |
Output is correct |
20 |
Correct |
200 ms |
40536 KB |
Output is correct |
21 |
Correct |
176 ms |
40568 KB |
Output is correct |
22 |
Correct |
192 ms |
40568 KB |
Output is correct |
23 |
Correct |
169 ms |
40540 KB |
Output is correct |
24 |
Correct |
211 ms |
38620 KB |
Output is correct |