#include <fstream>
#include <iostream>
using namespace std;
int myMax(int a, int b)
{
return (a > b ? a : b);
}
int myMax(int a, int b, int c)
{
return myMax(myMax(a, b), c);
}
const int NMAX = 1000;
const int INF = 2e9;
int n, m;
int a[NMAX + 1][NMAX + 1];
int dp[4][NMAX + 1][NMAX + 1];
int ans = -1;
void solve()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];
for(int k = 0; k <= 3; k++)
{
for(int i = 0; i <= n + 1; i++)
dp[k][i][0] = dp[k][i][m + 1] = -INF;
for(int j = 1; j <= m; j++)
dp[k][0][j] = dp[k][n + 1][j] = -INF;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[0][i][j] = myMax(dp[0][i][j - 1], dp[0][i - 1][j], -a[i][j]) - 1;
for(int i = 1; i <= n; i++)
for(int j = m; j >= 1; j--)
dp[1][i][j] = myMax(dp[1][i][j + 1], dp[1][i - 1][j], -a[i][j]) - 1;
for(int i = n; i >= 1; i--)
for(int j = 1; j <= m; j++)
dp[2][i][j] = myMax(dp[2][i][j - 1], dp[2][i + 1][j], -a[i][j]) - 1;
for(int i = n; i >= 1; i--)
for(int j = m; j >= 1; j--)
dp[3][i][j] = myMax(dp[3][i][j + 1], dp[3][i + 1][j], -a[i][j]) - 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(a[i][j] + myMax(myMax(dp[0][i][j], dp[1][i][j], dp[2][i][j]), dp[3][i][j]) > ans)
ans = a[i][j] + myMax(myMax(dp[0][i][j], dp[1][i][j], dp[2][i][j]), dp[3][i][j]);
cout << ans << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1348 KB |
Output is correct |
12 |
Correct |
1 ms |
1376 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1348 KB |
Output is correct |
15 |
Correct |
1 ms |
1376 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1364 KB |
Output is correct |
18 |
Correct |
117 ms |
28420 KB |
Output is correct |
19 |
Correct |
121 ms |
28424 KB |
Output is correct |
20 |
Correct |
121 ms |
28044 KB |
Output is correct |
21 |
Correct |
109 ms |
28424 KB |
Output is correct |
22 |
Correct |
111 ms |
28424 KB |
Output is correct |
23 |
Correct |
125 ms |
28364 KB |
Output is correct |
24 |
Correct |
119 ms |
28276 KB |
Output is correct |