#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct pqitem{
int v,x,y;
bool operator<(pqitem b)const
{return v>b.v;}
};
int n;
int w1,h1,x1,y1;
int w2,h2,x2,y2;
int g[2][500][500];
int c[2][250000];
vector<pqitem> fourway(int x,int y,int w,int h)
{
vector<pqitem> ans;
if(x>0)ans.push_back({0,x-1,y});
if(y>0)ans.push_back({0,x,y-1});
if(x<h-1)ans.push_back({0,x+1,y});
if(y<w-1)ans.push_back({0,x,y+1});
return ans;
}
void bfs(int i,int x,int y,int w,int h)
{
bool u[500][500]={};
priority_queue<pqitem> q;
q.push({1,x,y});
u[x][y]=1;
int j=1;
c[i][0]=0;
while(!q.empty())
{
pqitem now=q.top();
q.pop();
c[i][j++]=now.v;
vector<pqitem> fw=fourway(now.x,now.y,w,h);
for(pqitem &t:fw)
if(!u[t.x][t.y])
{
t.v=g[i][t.x][t.y];
q.push(t);
u[t.x][t.y]=1;
}
}
for(int k=1;k<j;k++)
c[i][k]=max(c[i][k],c[i][k-1]);
for(int k=j;k<=n;k++)
c[i][k]=1e9;
}
int main()
{
ios::sync_with_stdio(0);
cin>>n;
cin>>w1>>h1>>x1>>y1;
for(int i=0;i<h1;i++)
for(int j=0;j<w1;j++)
cin>>g[0][i][j];
cin>>w2>>h2>>x2>>y2;
for(int i=0;i<h2;i++)
for(int j=0;j<w2;j++)
cin>>g[1][i][j];
x1--; y1--; x2--; y2--;
swap(x1,y1); swap(x2,y2);
bfs(0,x1,y1,w1,h1);
bfs(1,x2,y2,w2,h2);
int ans=1e9;
for(int i=0;i<=n;i++)
{
ans=min(ans,c[0][i]+c[1][n-i]);
}
cout<<ans<<endl;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cnVjdCBwcWl0ZW17CglpbnQgdix4LHk7Cglib29sIG9wZXJhdG9yPChwcWl0ZW0gYilj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DApOwoJY2luPj5uOwoJY2luPj53MT4+aDE+PngxPj55MTsKCWZvcihpbnQgaT0wO2k8aDE7aSsrKQoJCWZvcihpbnQgaj0wO2o8dzE7aisrKQoJCQljaW4+PmdbMF1baV1bal07CgljaW4+PncyPj5oMj4+eDI+PnkyOwoJZm9yKGludCBpPTA7aTxoMjtpKyspCgkJZm9yKGludCBqPTA7ajx3MjtqKyspCgkJCWNpbj4+Z1sxXVtpXVtqXTsKCXgxLS07IHkxLS07IHgyLS07IHkyLS07Cglzd2FwKHgxLHkxKTsgc3dhcCh4Mix5Mik7CgliZnMoMCx4MSx5MSx3MSxoMSk7CgliZnMoMSx4Mix5Mix3MixoMik7CglpbnQgYW5zPTFlOTsKCWZvcihpbnQgaT0wO2k8PW47aSsrKQoJewoJCWFucz1taW4oYW5zLGNbMF1baV0rY1sxXVtuLWldKTsKCX0KCWNvdXQ8PGFuczw8ZW5kbDsKfQo=
prog.cpp:11:14: error: ‘int y1’ redeclared as different kind of symbol
int w1,h1,x1,y1;
^
In file included from /usr/include/features.h:371:0,
from /usr/include/i386-linux-gnu/c++/4.8/bits/os_defines.h:39,
from /usr/include/i386-linux-gnu/c++/4.8/bits/c++config.h:426,
from /usr/include/c++/4.8/iostream:38,
from prog.cpp:1:
/usr/include/i386-linux-gnu/bits/mathcalls.h:242:1: error: previous declaration of ‘double y1(double)’
__MATHCALL (y1,, (_Mdouble_));
^
prog.cpp: In function ‘int main()’:
prog.cpp:55:13: error: cannot bind ‘std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}’ lvalue to ‘std::basic_istream<char>&&’
cin>>w1>>h1>>x1>>y1;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/4.8/istream:872:5: error: initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = double(double)throw ()]’
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^
prog.cpp:63:10: error: ISO C++ forbids decrementing a pointer of type ‘double (*)(double)throw ()’ [-fpermissive]
x1--; y1--; x2--; y2--;
^
prog.cpp:63:10: error: lvalue required as decrement operand
prog.cpp:64:12: error: no matching function for call to ‘swap(int&, double (&)(double)throw ())’
swap(x1,y1); swap(x2,y2);
^
prog.cpp:64:12: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/bits/stl_algobase.h:64,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1:
/usr/include/c++/4.8/bits/move.h:166:5: note: template<class _Tp> void std::swap(_Tp&, _Tp&)
swap(_Tp& __a, _Tp& __b)
^
/usr/include/c++/4.8/bits/move.h:166:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: deduced conflicting types for parameter ‘_Tp’ (‘int’ and ‘double(double)throw ()’)
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/bits/stl_algobase.h:64,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1:
/usr/include/c++/4.8/bits/move.h:185:5: note: template<class _Tp, unsigned int _Nm> void std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
^
/usr/include/c++/4.8/bits/move.h:185:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘_Tp [_Nm]’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h:254:5: note: template<class _T1, class _T2> void std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&)
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/usr/include/c++/4.8/bits/stl_pair.h:254:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::pair<_T1, _T2>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2717:5: note: template<class _CharT, class _Traits, class _Alloc> void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&)
swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.8/bits/basic_string.h:2717:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/deque:64:0,
from /usr/include/c++/4.8/queue:60,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_deque.h:2011:5: note: template<class _Tp, class _Alloc> void std::swap(std::deque<_Tp, _Alloc>&, std::deque<_Tp, _Alloc>&)
swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
^
/usr/include/c++/4.8/bits/stl_deque.h:2011:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::deque<_Tp, _Alloc>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/vector:64:0,
from /usr/include/c++/4.8/queue:61,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_vector.h:1451:5: note: template<class _Tp, class _Alloc> void std::swap(std::vector<_Tp, _Alloc>&, std::vector<_Tp, _Alloc>&)
swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
^
/usr/include/c++/4.8/bits/stl_vector.h:1451:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::vector<_Tp, _Alloc>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/vector:65:0,
from /usr/include/c++/4.8/queue:61,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_bvector.h:112:3: note: void std::swap(std::_Bit_reference, std::_Bit_reference)
swap(_Bit_reference __x, _Bit_reference __y) noexcept
^
/usr/include/c++/4.8/bits/stl_bvector.h:112:3: note: no known conversion for argument 1 from ‘int’ to ‘std::_Bit_reference’
/usr/include/c++/4.8/bits/stl_bvector.h:120:3: note: void std::swap(std::_Bit_reference, bool&)
swap(_Bit_reference __x, bool& __y) noexcept
^
/usr/include/c++/4.8/bits/stl_bvector.h:120:3: note: no known conversion for argument 1 from ‘int’ to ‘std::_Bit_reference’
/usr/include/c++/4.8/bits/stl_bvector.h:128:3: note: void std::swap(bool&, std::_Bit_reference)
swap(bool& __x, _Bit_reference __y) noexcept
^
/usr/include/c++/4.8/bits/stl_bvector.h:128:3: note: no known conversion for argument 1 from ‘int’ to ‘bool&’
In file included from /usr/include/c++/4.8/queue:64:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_queue.h:316:5: note: template<class _Tp, class _Seq> void std::swap(std::queue<_Tp, _Seq>&, std::queue<_Tp, _Seq>&)
swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y)
^
/usr/include/c++/4.8/bits/stl_queue.h:316:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::queue<_Tp, _Seq>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/queue:64:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_queue.h:555:5: note: template<class _Tp, class _Sequence, class _Compare> void std::swap(std::priority_queue<_Tp, _Sequence, _Compare>&, std::priority_queue<_Tp, _Sequence, _Compare>&)
swap(priority_queue<_Tp, _Sequence, _Compare>& __x,
^
/usr/include/c++/4.8/bits/stl_queue.h:555:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::priority_queue<_Tp, _Sequence, _Compare>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/tuple:39:0,
from /usr/include/c++/4.8/functional:55,
from /usr/include/c++/4.8/bits/stl_algo.h:66,
from /usr/include/c++/4.8/algorithm:62,
from prog.cpp:3:
/usr/include/c++/4.8/array:262:5: note: template<class _Tp, unsigned int _Nm> void std::swap(std::array<_Tp, _Nm>&, std::array<_Tp, _Nm>&)
swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
^
/usr/include/c++/4.8/array:262:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::array<_Tp, _Nm>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/bits/stl_algo.h:66,
from /usr/include/c++/4.8/algorithm:62,
from prog.cpp:3:
/usr/include/c++/4.8/tuple:1048:5: note: template<class ... _Elements> void std::swap(std::tuple<_Elements ...>&, std::tuple<_Elements ...>&)
swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
^
/usr/include/c++/4.8/tuple:1048:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::tuple<_Elements ...>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
In file included from /usr/include/c++/4.8/bits/stl_algo.h:66:0,
from /usr/include/c++/4.8/algorithm:62,
from prog.cpp:3:
/usr/include/c++/4.8/functional:2570:5: note: template<class _Res, class ... _Args> void std::swap(std::function<_Res(_ArgTypes ...)>&, std::function<_Res(_ArgTypes ...)>&)
swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
^
/usr/include/c++/4.8/functional:2570:5: note: template argument deduction/substitution failed:
prog.cpp:64:12: note: mismatched types ‘std::function<_Res(_ArgTypes ...)>’ and ‘int’
swap(x1,y1); swap(x2,y2);
^
prog.cpp:65:19: error: invalid conversion from ‘double (*)(double)throw ()’ to ‘int’ [-fpermissive]
bfs(0,x1,y1,w1,h1);
^
prog.cpp:24:6: error: initializing argument 3 of ‘void bfs(int, int, int, int, int)’ [-fpermissive]
void bfs(int i,int x,int y,int w,int h)
^