#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define len(x) (ll)x.size()
const ll inf = 1e9, infl = 1e18;
const ll MOD = 1e9 + 7;
const ll maxn = 1e3 + 5;
ll n, m, k = 0;
ll a[maxn][maxn], dp[maxn][maxn], dp2[maxn][maxn];
void _() {
// elebil mesele bele bir seye gelir ki, a[x1][y1] - a[x2][y2] - |x1 - x2| - |y1 - y2|
// sonuncu modullu hisse 4 cur acilir ona gore 4 hal olur
// amma biz ele goturekki y1 <= y2 onda 2 cur acilir ona gore 2 hal olur:
// (a[x1][y1] - x1 + y1) + (- a[x2][y2] + x2 - y2) x1 solda
// (a[x1][y1] + x1 + y1) + (- a[x2][y2] - x2 - y2) x1 sagda
// hecne iki dene dp-miz olur
cin >> n >> m;
for(ll i = 1; i <= n; i ++){
for(ll j = 1; j <= m; j ++){
cin >> a[i][j];
}
}
for(ll i = 0; i <= n + 1; i ++){
for(ll j = 0; j <= m + 1; j ++){
dp[i][j] = dp2[i][j] = -infl;
}
}
for(ll i = 1; i <= n; i ++){
for(ll j = 1; j <= m; j ++){
dp[i][j] = max({- a[i][j] + i - j, dp[i - 1][j], dp[i][j - 1]});
}
}
for(ll i = 1; i <= n; i ++){
for(ll j = m; j >= 1; j --){
dp2[i][j] = max({- a[i][j] - i - j, dp2[i - 1][j], dp2[i][j + 1]});
}
}
ll cv = 0;
for(ll i = 1; i <= n; i ++){
for(ll j = 1; j <= m; j ++){
cv = max(cv, a[i][j] - i + j + dp[i][j]);
cv = max(cv, a[i][j] + i + j + dp2[i][j]);
}
}
cout << cv - 3 << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
ll t = 1;
// cin >> t;
while(t --) _();
}
| # | 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... |