#include "bits/stdc++.h"
using namespace std;
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void IOIGold2024_InshAllah() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
const ll inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll mod = 1e9+7, N = 1e3+5;
ll pr[N][N][2], sf[N][N][2], a[N][N];
void kigash() {
ll n, m;
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++) {
pr[i][j][0] = pr[i][j][1] = MN;
sf[i][j][0] = sf[i][j][1] = MN;
}
}
for(ll i=1; i<=n; i++) {
for(ll j=1; j<=m; j++) {
pr[i][j][0] = max({pr[i-1][j][0], pr[i][j-1][0], -a[i][j]+i+j});
}
}
for(ll i=1; i<=n; i++) {
for(ll j=m; j>=1; j--) {
pr[i][j][1] = max({pr[i-1][j][1], pr[i][j+1][1], -a[i][j]+i-j});
}
}
for(ll i=n; i>=1; i--) {
for(ll j=1; j<=m; j++) {
sf[i][j][0] = max({sf[i+1][j][0], sf[i][j-1][0], -a[i][j]-i+j});
}
}
for(ll i=n; i>=1; i--) {
for(ll j=m; j>=1; j--) {
sf[i][j][1] = max({sf[i+1][j][1], sf[i][j+1][1], -a[i][j]-i-j});
}
}
ll ans = MN;
for(ll i=1; i<=n; i++) {
for(ll j=1; j<=m; j++) {
ans = max(ans, a[i][j]-i-j+pr[i][j][0]);
ans = max(ans, a[i][j]-i+j+pr[i][j][1]);
ans = max(ans, a[i][j]+i-j+sf[i][j][0]);
ans = max(ans, a[i][j]+i+j+sf[i][j][1]);
}
}
cout<<ans-1<<"\n";
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
IOIGold2024_InshAllah();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) {
kigash();
}
}
Compilation message
maxcomp.cpp: In function 'void freopen(std::string)':
maxcomp.cpp:17:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
maxcomp.cpp:17:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4564 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4568 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4564 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4568 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
8676 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8588 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4564 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4568 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
8676 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8588 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8792 KB |
Output is correct |
18 |
Correct |
90 ms |
48296 KB |
Output is correct |
19 |
Correct |
89 ms |
48248 KB |
Output is correct |
20 |
Correct |
84 ms |
47956 KB |
Output is correct |
21 |
Correct |
87 ms |
48364 KB |
Output is correct |
22 |
Correct |
87 ms |
48380 KB |
Output is correct |
23 |
Correct |
86 ms |
48376 KB |
Output is correct |
24 |
Correct |
86 ms |
48724 KB |
Output is correct |