#include <bits/stdc++.h>
using namespace std;
#define int long long
#define tol(bi) (1LL<<((int)(bi)))
int32_t main(){
int h,w;
cin>>h>>w;
vector<vector<int>> val(h,vector<int>(w));
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; j++){
cin>>val[i][j];
}
}
int n;cin>>n;
vector<vector<int>> arr(h,vector<int>(w,0));
vector<pair<int,int>> spec(n);
for (int i = 0; i < n; i++){
int x,y;cin>>x>>y;
arr[x][y]=i+1;
spec[i]={x,y};
}
int ans = -1;
for (int i = 0; i < tol(n*2); i++){
int crans = 0;
bool boolean=true;
vector<pair<int,int>> tb;
for (int j = 0; j < n; j++){
int a = !!(tol(j)&i);
int b = !!(tol(j*2)&i);
int x = spec[j].first;
int y = spec[j].second;
crans+=val[x][y];
if (a!=0 || b!=0){
if (x==0 || arr[x-1][y]!=0){
boolean=false;
break;
}
arr[x-1][y]=-1;
crans+=val[x-1][y];
tb.push_back({x-1,y});
}
if (a!=0 || b!=1){
if (y==0 || arr[x][y-1]!=0){
boolean=false;
break;
}
arr[x][y-1]=-1;
crans+=val[x][y-1];
tb.push_back({x,y-1});
}
if (a!=1 || b!=0){
if (x==h-1 || arr[x+1][y]!=0){
boolean=false;
break;
}
arr[x+1][y]=-1;
crans+=val[x+1][y];
tb.push_back({x+1,y});
}
if (a!=1 || b!=1){
if (y==w-1 || arr[x][y+1]!=0){
boolean=false;
break;
}
arr[x][y+1]=-1;
crans+=val[x][y+1];
tb.push_back({x,y+1});
}
}
while (tb.size()){
arr[tb.back().first][tb.back().second]=0;
tb.pop_back();
}
if (!boolean) continue;
ans=max(ans,crans);
}
if (ans==-1) cout<<"No"<<endl;
else cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
412 ms |
484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
413 ms |
484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
401 ms |
484 KB |
Output is correct |
2 |
Execution timed out |
1070 ms |
852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
403 ms |
296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
133 ms |
284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
412 ms |
484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
412 ms |
484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |