#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 1013;
int N, M;
int grid[MAXN][MAXN];
int ans;
int stor[MAXN][MAXN];
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
FOR(i, 0, N)
{
FOR(j, 0, M)
{
cin >> grid[i][j];
}
}
//x-x0 + y-y0 + grid[x][y] - grid[x0][y0]
FOR(i, 0, N)
{
FOR(j, 0, M)
{
stor[i][j] = -grid[i][j] + i + j;
}
}
FOR(i, 0, N)
{
FOR(j, 0, M)
{
if (i) ckmax(stor[i][j], stor[i - 1][j]);
if (j) ckmax(stor[i][j], stor[i][j - 1]);
ckmax(ans, -i - j + grid[i][j] + stor[i][j]);
}
}
// cerr << "ANS " << ans << endl;
//x-x0 + y-y0 - grid[x][y] + grid[x0][y0]
FOR(i, 0, N)
{
FOR(j, 0, M)
{
stor[i][j] = grid[i][j] + i + j;
}
}
FOR(i, 0, N)
{
FOR(j, 0, M)
{
if (i) ckmax(stor[i][j], stor[i - 1][j]);
if (j) ckmax(stor[i][j], stor[i][j - 1]);
ckmax(ans, -i - j - grid[i][j] + stor[i][j]);
}
}
// cerr << "ANS " << ans << endl;
//x-x0 + y0-y + grid[x][y0] - grid[x0][y]
FOR(i, 0, N)
{
FORD(j, M, 0)
{
stor[i][j] = grid[i][j] + i - j;
}
}
FOR(i, 0, N)
{
FORD(j, M, 0)
{
if (i) ckmax(stor[i][j], stor[i - 1][j]);
if (j != M - 1) ckmax(stor[i][j], stor[i][j + 1]);
ckmax(ans, -i + j - grid[i][j] + stor[i][j]);
}
}
// cerr << "ANS " << ans << endl;
FOR(i, 0, N)
{
FORD(j, M, 0)
{
stor[i][j] = -grid[i][j] + i - j;
}
}
FOR(i, 0, N)
{
FORD(j, M, 0)
{
if (i) ckmax(stor[i][j], stor[i - 1][j]);
if (j != M - 1) ckmax(stor[i][j], stor[i][j + 1]);
ckmax(ans, -i + j + grid[i][j] + stor[i][j]);
}
}
// cerr << "ANS " << ans << endl;
cout << ans - 1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
896 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
768 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
896 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
768 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
5 ms |
768 KB |
Output is correct |
18 |
Correct |
147 ms |
16760 KB |
Output is correct |
19 |
Correct |
153 ms |
17016 KB |
Output is correct |
20 |
Correct |
140 ms |
16504 KB |
Output is correct |
21 |
Correct |
143 ms |
16760 KB |
Output is correct |
22 |
Correct |
163 ms |
16764 KB |
Output is correct |
23 |
Correct |
145 ms |
16760 KB |
Output is correct |
24 |
Correct |
152 ms |
17144 KB |
Output is correct |