#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n, m; cin >> n >> m;
vector<vector<int>> els(n, vector<int>(m));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> els[i][j];
}
}
int k; cin >> k;
vector<pair<int, int>> xs(k);
for (int i = 0; i < k; i ++){
int x, y; cin >> x >> y;
xs[i] = {x, y};
}
sort(xs.begin(), xs.end());
bool so = true;
vector<set<pair<int, int>>> components;
for (auto& pr : xs){
int x = pr.first, y = pr.second;
bool est = false;
for (int i = 0; i < components.size(); i++){
set<pair<int, int>> st = components[i];
if (st.find({x-1, y-1}) != st.end()) est = true;
else if (st.find({x+1, y+1}) != st.end()) est = true;
else if (st.find({x-1, y}) != st.end()) est = true;
else if (st.find({x, y-1}) != st.end()) est = true;
else if (st.find({x+1, y}) != st.end()) est = true;
else if (st.find({x, y+1}) != st.end()) est = true;
else if (st.find({x, y+2}) != st.end()) est = true;
else if (st.find({x, y-2}) != st.end()) est = true;
else if (st.find({x+2, y}) != st.end()) est = true;
else if (st.find({x-2, y}) != st.end()) est = true;
if (est) {
// cout << x << ' ' << y << endl;
components[i].insert({x, y});
break;
}
}
if (!est) {
components.push_back({{x, y}});
}
}
int ans = 0;
// auto [l, r] = xs[0];
for (auto smth : components){
set<vector<int>> sosedi;
int cnt = 0;
for (pair<int, int> pr : smth) {
int x = pr.first, y = pr.second;
// cout << x << ' ' << y << " ";
cnt += els[x][y];
if (x-1 >= 0 && sosedi.find({els[x-1][y], x-1, y}) == sosedi.end() && smth.find({x-1, y}) == smth.end()) {
sosedi.insert({els[x-1][y], x-1, y});
cnt += els[x-1][y];
}
if (x+1 < n && sosedi.find({els[x+1][y], x+1, y}) == sosedi.end() && smth.find({x+1, y}) == smth.end()) {
sosedi.insert({els[x+1][y], x+1, y});
cnt += els[x+1][y];
}
if (y-1 >= 0 && sosedi.find({els[x][y-1], x, y-1}) == sosedi.end() && smth.find({x, y-1}) == smth.end()) {
sosedi.insert({els[x][y-1], x, y-1});
cnt += els[x][y-1];
}
if (y+1 < m && sosedi.find({els[x][y+1], x, y+1}) == sosedi.end() && smth.find({x, y+1}) == smth.end()) {
sosedi.insert({els[x][y+1], x, y+1});
cnt += els[x][y+1];
}
}
// cout << endl;
if (sosedi.size() < 3*smth.size()) {cout << "No" << endl; return;}
if (sosedi.size() == 3*smth.size()) ans += cnt;
if (sosedi.size() == 3*smth.size() + 1) ans += cnt - (*sosedi.begin())[0];
}
cout << ans << endl;
// cout << "YYAY" << endl;
}
signed main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t = 1;
// cin >> t;
while (t--)solve();
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |