이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb push_back
#define mpr make_pair
#define lb lower_bound
#define ld long double
#define ub upper_bound
const int N = 1000005;
vector<int> a[N];
void solve() {
	int n, m;
	cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int x;
            cin >> x;
            a[i].push_back(x);
        }
    }
    int k;
    cin >> k;
    bool flag = true;
    ll ans = 0;
    while (k--) {
        int x, y;
        cin >> x >> y;
        vector<pair<int, int> > v;
        for (int i = -1; i <= 1; i += 2) {
            if (x + i < n && x + i >= 0) {
                v.push_back(mpr(x + i, y));
            }
            if (y + i < n && y + i >= 0) {
                v.push_back(mpr(x, y + i));
            }
        }
        if (v.size() < 3) {
            flag = false;
        }
        else {
            int mn = 1005, sum = a[x][y];
            for (auto i : v) {
                mn = min(mn, a[i.first][i.second]);
                sum += a[i.first][i.second];
            }
            ans = ans + sum - mn;
        }
    }
    if (!flag) {
        cout << "NO" << endl;
    }
    else {
        cout << ans << endl;
    }
}
int main() {
    /*cout.setf(ios::fixed | ios::showpoint);
    cout.precision(6);*/
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
| # | 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... |