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 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 |
---|
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... |