64602023-12-02 14:36:11111Jegesmedve (50)cpp17Accepted 50/50237ms5436 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

#define MOD 20200111

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N, M;
	cin >> N >> M;
	int i0, j0;
	int a[100][100];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> a[i][j];
			if (a[i][j] == -1) {
				a[i][j] = INT_MAX;
				i0 = i;
				j0 = j;
			}
		}
	}
	int b[100][100];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> b[i][j];
		}
	}
	int v[100][100] {};
	auto dfs = [&](auto dfs, int i, int j, int x) {
		if (i < 0 || i >= N || j < 0 || j >= M || v[i][j] >= x) {
			return;
		}
		v[i][j] = min(x, a[i][j]);
		dfs(dfs, i + 1, j, v[i][j]);
		dfs(dfs, i - 1, j, v[i][j]);
		dfs(dfs, i, j + 1, v[i][j]);
		dfs(dfs, i, j - 1, v[i][j]);
	};
	dfs(dfs, i0, j0, INT_MAX);
	map<int, int> m;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			m[v[i][j]] += b[i][j];
		}
	}
	int ans = 0;
	for (auto p : m) {
		ans = min(p.first, ans + p.second);
	}
	cout << ans + 1 << endl;
	return 0;
}

SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2316 KiB
2Accepted0/03ms2584 KiB
3Accepted3/33ms2728 KiB
4Accepted3/33ms3092 KiB
5Accepted3/33ms3228 KiB
6Accepted4/44ms3316 KiB
7Accepted3/33ms3552 KiB
8Accepted3/33ms3636 KiB
9Accepted3/33ms3696 KiB
10Accepted4/43ms3672 KiB
11Accepted4/414ms4064 KiB
12Accepted4/48ms4096 KiB
13Accepted4/44ms4000 KiB
14Accepted4/4100ms4812 KiB
15Accepted4/4158ms5252 KiB
16Accepted4/4237ms5436 KiB