#include <bits/stdc++.h>
using namespace std;
const long long MX=1e6+10;
vector < long long > mas[MX];
long long vis[MX];
long long sm[MX];
long long a[MX];
long long n,m;
long long corx[4]={0,0,1,-1};
long long cory[4]={1,-1,0,0};
vector < long long > ver;
long long kver;
long long prd(long long x,long long y)
{
return (x*m+y);
}
void DFS(long long zar)
{
if(sm[zar])
{
kver++;
}
else
{
ver.push_back(a[zar]);
}
vis[zar]=1;
for(auto u:mas[zar])
{
if(!vis[u])
{
DFS(u);
}
}
return;
}
bool can_go(long long x,long long y)
{
return (0<=x && x<n && 0<=y && y<m);
}
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>m;
long long zn;
for(long long i=0;i<n;i++)
{
for(long long j=0;j<m;j++)
{
cin>>zn;
a[prd(i,j)]=zn;
}
}
long long q,x,y;
cin>>q;
long long res=0;
for(long long i=0;i<q;i++)
{
cin>>x>>y;
res+=a[prd(x,y)];
sm[prd(x,y)]=1;
for(long long j=0;j<4;j++)
{
if(can_go(x+corx[j],y+cory[j]))
{
mas[prd(x,y)].push_back(prd(x+corx[j],y+cory[j]));
mas[prd(x+corx[j],y+cory[j])].push_back(prd(x,y));
}
}
}
for(long long i=0;i<n*m;i++)
{
if(!vis[i] && sm[i])
{
ver.clear();
kver=0;
DFS(i);
if(ver.size()<(3*kver))
{
cout<<"No\n";
return 0;
}
sort(ver.begin(),ver.end());
reverse(ver.begin(),ver.end());
kver*=3;
for(long long i=0;i<kver;i++)
{
res+=ver[i];
}
}
}
cout<<res<<"\n";
return 0;
}
Compilation message
covering.cpp: In function 'int main()':
covering.cpp:101:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
101 | if(ver.size()<(3*kver))
| ~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
26716 KB |
Output is correct |
2 |
Correct |
8 ms |
29016 KB |
Output is correct |
3 |
Correct |
12 ms |
32088 KB |
Output is correct |
4 |
Correct |
24 ms |
36232 KB |
Output is correct |
5 |
Correct |
58 ms |
49932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
26712 KB |
Output is correct |
2 |
Correct |
8 ms |
29388 KB |
Output is correct |
3 |
Correct |
13 ms |
32092 KB |
Output is correct |
4 |
Correct |
22 ms |
36232 KB |
Output is correct |
5 |
Correct |
58 ms |
50036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
26716 KB |
Output is correct |
2 |
Correct |
9 ms |
29152 KB |
Output is correct |
3 |
Correct |
12 ms |
31836 KB |
Output is correct |
4 |
Correct |
23 ms |
36364 KB |
Output is correct |
5 |
Correct |
57 ms |
49908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26716 KB |
Output is correct |
2 |
Correct |
6 ms |
26716 KB |
Output is correct |
3 |
Correct |
10 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29020 KB |
Output is correct |
5 |
Correct |
13 ms |
29928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26716 KB |
Output is correct |
2 |
Correct |
6 ms |
26716 KB |
Output is correct |
3 |
Correct |
6 ms |
26716 KB |
Output is correct |
4 |
Correct |
6 ms |
26580 KB |
Output is correct |
5 |
Correct |
6 ms |
26716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
26716 KB |
Output is correct |
2 |
Correct |
8 ms |
29016 KB |
Output is correct |
3 |
Correct |
12 ms |
32088 KB |
Output is correct |
4 |
Correct |
24 ms |
36232 KB |
Output is correct |
5 |
Correct |
58 ms |
49932 KB |
Output is correct |
6 |
Correct |
7 ms |
26712 KB |
Output is correct |
7 |
Correct |
8 ms |
29388 KB |
Output is correct |
8 |
Correct |
13 ms |
32092 KB |
Output is correct |
9 |
Correct |
22 ms |
36232 KB |
Output is correct |
10 |
Correct |
58 ms |
50036 KB |
Output is correct |
11 |
Correct |
8 ms |
26716 KB |
Output is correct |
12 |
Correct |
9 ms |
29152 KB |
Output is correct |
13 |
Correct |
12 ms |
31836 KB |
Output is correct |
14 |
Correct |
23 ms |
36364 KB |
Output is correct |
15 |
Correct |
57 ms |
49908 KB |
Output is correct |
16 |
Correct |
6 ms |
26716 KB |
Output is correct |
17 |
Correct |
6 ms |
26716 KB |
Output is correct |
18 |
Correct |
10 ms |
29276 KB |
Output is correct |
19 |
Correct |
9 ms |
29020 KB |
Output is correct |
20 |
Correct |
13 ms |
29928 KB |
Output is correct |
21 |
Correct |
6 ms |
26716 KB |
Output is correct |
22 |
Correct |
9 ms |
29532 KB |
Output is correct |
23 |
Correct |
15 ms |
32092 KB |
Output is correct |
24 |
Correct |
23 ms |
36240 KB |
Output is correct |
25 |
Correct |
59 ms |
49828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
26716 KB |
Output is correct |
2 |
Correct |
8 ms |
29016 KB |
Output is correct |
3 |
Correct |
12 ms |
32088 KB |
Output is correct |
4 |
Correct |
24 ms |
36232 KB |
Output is correct |
5 |
Correct |
58 ms |
49932 KB |
Output is correct |
6 |
Correct |
7 ms |
26712 KB |
Output is correct |
7 |
Correct |
8 ms |
29388 KB |
Output is correct |
8 |
Correct |
13 ms |
32092 KB |
Output is correct |
9 |
Correct |
22 ms |
36232 KB |
Output is correct |
10 |
Correct |
58 ms |
50036 KB |
Output is correct |
11 |
Correct |
8 ms |
26716 KB |
Output is correct |
12 |
Correct |
9 ms |
29152 KB |
Output is correct |
13 |
Correct |
12 ms |
31836 KB |
Output is correct |
14 |
Correct |
23 ms |
36364 KB |
Output is correct |
15 |
Correct |
57 ms |
49908 KB |
Output is correct |
16 |
Correct |
6 ms |
26716 KB |
Output is correct |
17 |
Correct |
6 ms |
26716 KB |
Output is correct |
18 |
Correct |
10 ms |
29276 KB |
Output is correct |
19 |
Correct |
9 ms |
29020 KB |
Output is correct |
20 |
Correct |
13 ms |
29928 KB |
Output is correct |
21 |
Correct |
6 ms |
26716 KB |
Output is correct |
22 |
Correct |
6 ms |
26716 KB |
Output is correct |
23 |
Correct |
6 ms |
26716 KB |
Output is correct |
24 |
Correct |
6 ms |
26580 KB |
Output is correct |
25 |
Correct |
6 ms |
26716 KB |
Output is correct |
26 |
Correct |
6 ms |
26716 KB |
Output is correct |
27 |
Correct |
9 ms |
29532 KB |
Output is correct |
28 |
Correct |
15 ms |
32092 KB |
Output is correct |
29 |
Correct |
23 ms |
36240 KB |
Output is correct |
30 |
Correct |
59 ms |
49828 KB |
Output is correct |
31 |
Correct |
235 ms |
77416 KB |
Output is correct |
32 |
Correct |
59 ms |
47696 KB |
Output is correct |
33 |
Correct |
74 ms |
53620 KB |
Output is correct |
34 |
Correct |
60 ms |
47480 KB |
Output is correct |
35 |
Correct |
132 ms |
63608 KB |
Output is correct |