#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int mp[ 2001 ][ 2001 ];
int n, m, a[ 2001 ][ 2001 ], now[ 2001 ][ 2001 ];
set <int> st;
map <int, int> mpp;
int f( int x, int y ) {
if ( !x || !y || x > n || y > m )
return -oo;
now[ x ][ y ] = 1;
st.insert( a[ x ][ y ] );
mpp[ a[ x ][ y ] ] ++;
sum = st.size();
mx = *st.rbegin();
mn = *st.begin();
int mxx = mx - mn - sum;
if ( !now[ x + 1 ][ y ] )
mxx = max( mxx, f( x + 1, y ) );
if ( !now[ x ][ y - 1 ] )
mxx = max( mxx, f( x, y - 1 ) );
if ( !now[ x ][ y + 1 ] )
mxx = max( mxx, f( x, y + 1 ) );
if ( !now[ x - 1 ][ y ] )
mxx = max( mxx, f( x - 1, y ) );
now[ x ][ y ] = 0;
mpp[ a[ x ][ y ] ] --;
if ( !mpp[ a[ x ][ y ] ] ){
st.erase( a[ x ][ y ] );
}
return mxx;
}
main () {
cin >> n >> m;
for ( int i = 1; i <= n; i ++ ) {
for ( int j = 1; j <= m; j ++ ) {
cin >> a[ i ][ j ];
mp[ i ][ j ] = -2;
}
}
ans = -oo;
for ( int i = 1; i <= n; i ++ ) {
for ( int j = 1; j <= m; j ++ ) {
ans = max( ans, f( i, j ) );
}
}
cout << ans;
}
/*
4
3 3 4
4 ^ 6 ^ 8 ^ 5 ^ 6 ^ 7 = 14
2 3
2 4 3
5 7 5
*/
Compilation message
maxcomp.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
77 | main () {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
15 ms |
356 KB |
Output is correct |
3 |
Correct |
12 ms |
352 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
520 KB |
Output is correct |
2 |
Incorrect |
379 ms |
524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
15 ms |
356 KB |
Output is correct |
3 |
Correct |
12 ms |
352 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
15 ms |
356 KB |
Output is correct |
3 |
Correct |
12 ms |
352 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |