This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int n,m;
set<pair<int,int>>s;
int f(int x,int y){
if(x<0 || x>=n || y<0 || y>=m)return 0;
if(s.count({x,y}))return 0;
return 1;
}
int32_t main(){
cin>>n>>m;
int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)cin>>arr[i][j];
}
int k;
cin>>k;
int ans=0;
for(int i=0;i<k;i++){
int a,b;
cin>>a>>b;
s.insert({a,b});
}
for(auto i:s){
int x=i.ff;
int y=i.ss;
int z=(f(x-1,y)+f(x+1,y)+f(x,y-1)+f(x,y+1));
if(z<=2){
ans=-1;
break;
}
if(z==3){
ans+=arr[x][y];
if(f(x-1,y))ans+=arr[x-1][y];
if(f(x+1,y))ans+=arr[x+1][y];
if(f(x,y-1))ans+=arr[x][y-1];
if(f(x,y+1))ans+=arr[x][y+1];
}
else{
ans+=arr[x][y]+arr[x-1][y]+arr[x+1][y]+arr[x][y-1]+arr[x][y+1];
ans-=min({arr[x-1][y],arr[x+1][y],arr[x][y-1],arr[x][y+1]});
}
}
if(ans==-1)cout<<"No"<<endl;
else cout<<ans<<endl;
}
# | 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... |