Submission #1141861

#TimeUsernameProblemLanguageResultExecution timeMemory
1141861AgageldiMaxcomp (info1cup18_maxcomp)C++17
0 / 100
0 ms580 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 600005
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define sz(s) (int)s.size()
#define pii pair<int,int>

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll T, n, a[2000][2000], t, m, answer = -1, vis[2000][2000], dp[2000][2000];
vector <pair<int,int>> path, ans;
void solve(int x,int y,ll mn,ll mx,int cnt) {
	vis[x][y] = 1;
	if(dp[x][y] >= mx - mn - cnt) return;
	dp[x][y] = mx - mn - cnt;
	if(x+1 <= n && vis[x + 1][y] == 0) {
		path.pb({x + 1, y});
		solve(x + 1, y,min(mn,a[x + 1][y]),max(mx,a[x+1][y]),cnt+1);
		path.pop_back();
	}
	if(x-1 >= 1 && vis[x-1][y]==0){
		path.pb({x-1,y});
		solve(x - 1, y,min(mn,a[x-1][y]),max(mx,a[x-1][y]),cnt+1);
		path.pop_back();
	}
	if(y+1 <= m && vis[x][y+1]==0){
		path.pb({x,y+1});
		solve(x, y + 1,min(mn,a[x][y + 1]),max(mx,a[x][y + 1]),cnt+1);
		path.pop_back();
	}
	if(y-1 >= 1 && vis[x][y-1]==0){
		path.pb({x,y-1});
		solve(x, y - 1,min(mn,a[x][y - 1]),max(mx,a[x][y - 1]),cnt+1);
		path.pop_back();
	}
	vis[x][y] = 0;
}

int main () {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n >> m;
	bool tr = 0;
	for(int i = 1;i <= n;i++) {
		for(int j = 1; j <= m; j++) {
			cin >> a[i][j];
			dp[i][j] = -2;
			if(a[i][j]) tr = 1;
		}
	}
	if(!tr) {
		cout << "-1\n";
		return 0;
	}
	for(int i=1;i<=n;i++){
		for(int j= 1;j<=m;j++) {
			ans.clear();
			solve(i,j,a[i][j],a[i][j],1);
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			answer = max(answer,dp[i][j]);
		}
	}
	cout << answer << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...