This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 1e6 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,m,k;
void Gold(){
cin >> n >> m;
int a[n + 2][m + 2] , r[n * m + 1] , c[n * m + 1];
for(int i = 0 ; i <= n + 1 ; i++){
for(int j = 0 ; j <= m + 1 ; j++){
a[i][j] = 0;
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
cin >> a[i][j];
}
}
cin >> k;
int ans = 0;
for(int i = 1 ; i <= k ; ++i){
cin >> r[i] >> c[i];
r[i]++;
c[i]++;
}
bool oknt = 0;
for(int i = 1 ; i <= k ; i++){
for(int j = i + 1 ; j <= k ; j++){
if(abs(r[i] - r[j]) <= 2 && abs(c[i] - c[j]) <= 2){
oknt = 1;
break;
}
}
}
if(!oknt){
for(int i = 1 ; i <= k ; ++i){
int ans1 = 0;
ans1 = max(ans1 , a[r[i] - 1][c[i]] + a[r[i]][c[i] - 1] + a[r[i]][c[i]] + a[r[i]][c[i] + 1]);
ans1 = max(ans1 , a[r[i] + 1][c[i]] + a[r[i]][c[i] - 1] + a[r[i]][c[i]] + a[r[i]][c[i] + 1]);
ans1 = max(ans1 , a[r[i] - 1][c[i]] + a[r[i] + 1][c[i]] + a[r[i]][c[i]] + a[r[i]][c[i] + 1]);
ans1 = max(ans1 , a[r[i] - 1][c[i]] + a[r[i] + 1][c[i]] + a[r[i]][c[i]] + a[r[i]][c[i] - 1]);
ans += ans1;
}
cout << ans;
return;
}
if(k <= 10){
ans = -1;
int x = 1;
int cur = k;
while(cur--){
x *= 4;
}
map <pii , int> p;
for(int mask = 0 ; mask < x ; mask++){
bool ok = 1;
int msk = mask;
int ans1 = 0;
p.clear();
for(int i = 1 ; i <= k ; i++){
if(msk % 4 == 0){
ans1 += a[r[i] - 1][c[i]] + a[r[i]][c[i] - 1] + a[r[i]][c[i]] + a[r[i]][c[i] + 1];
if(r[i] > 1){
p[{r[i] - 1 , c[i]}]++;
if(p[{r[i] - 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
if(c[i] > 1){
p[{r[i] , c[i] - 1}]++;
if(p[{r[i] , c[i] - 1}] >= 2){
ok = 0;
break;
}
}
p[{r[i] , c[i]}]++;
if(p[{r[i] , c[i]}] >= 2){
ok = 0;
break;
}
if(c[i] < m){
p[{r[i] , c[i] + 1}]++;
if(p[{r[i] , c[i] + 1}] >= 2){
ok = 0;
break;
}
}
msk /= 4;
continue;
}
if(msk % 4 == 1){
ans1 += a[r[i] + 1][c[i]] + a[r[i]][c[i] - 1] + a[r[i]][c[i]] + a[r[i]][c[i] + 1];
if(r[i] < n){
p[{r[i] + 1 , c[i]}]++;
if(p[{r[i] + 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
if(c[i] > 1){
p[{r[i] , c[i] - 1}]++;
if(p[{r[i] , c[i] - 1}] >= 2){
ok = 0;
break;
}
}
p[{r[i] , c[i]}]++;
if(p[{r[i] , c[i]}] >= 2){
ok = 0;
break;
}
if(c[i] < m){
p[{r[i] , c[i] + 1}]++;
if(p[{r[i] , c[i] + 1}] >= 2){
ok = 0;
break;
}
}
msk /= 4;
continue;
}
if(msk % 4 == 2){
ans1 += a[r[i] - 1][c[i]] + a[r[i] + 1][c[i]] + a[r[i]][c[i]] + a[r[i]][c[i] + 1];
if(r[i] > 1){
p[{r[i] - 1 , c[i]}]++;
if(p[{r[i] - 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
if(r[i] < n){
p[{r[i] + 1 , c[i]}]++;
if(p[{r[i] + 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
p[{r[i] , c[i]}]++;
if(p[{r[i] , c[i]}] >= 2){
ok = 0;
break;
}
if(c[i] < m){
p[{r[i] , c[i] + 1}]++;
if(p[{r[i] , c[i] + 1}] >= 2){
ok = 0;
break;
}
}
msk /= 4;
continue;
}
if(msk % 4 == 3){
ans1 += a[r[i] - 1][c[i]] + a[r[i] + 1][c[i]] + a[r[i]][c[i]] + a[r[i]][c[i] - 1];
if(r[i] > 1){
p[{r[i] - 1 , c[i]}]++;
if(p[{r[i] - 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
if(r[i] < n){
p[{r[i] + 1 , c[i]}]++;
if(p[{r[i] + 1 , c[i]}] >= 2){
ok = 0;
break;
}
}
p[{r[i] , c[i]}]++;
if(p[{r[i] , c[i]}] >= 2){
ok = 0;
break;
}
if(c[i] > 1){
p[{r[i] , c[i] - 1}]++;
if(p[{r[i] , c[i] - 1}] >= 2){
ok = 0;
break;
}
}
msk /= 4;
continue;
}
}
if(ok){
ans = max(ans , ans1);
}
}
if(ans == -1){
cout << "No";
}
else{
cout << ans;
}
return;
}
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# | 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... |