ソースコード
#include <bits/stdc++.h>
using namespace std;
long long H, W, dp[1145][1145], d[1145][1145], x[1145], y[1145], X, Y, L, l, R, r;
bool outside(long long z, long long Z){
return z <= 0 || z > H || Z <= 0 || (L <= z && z <= R && l <= Z && Z <= r) || Z > W;
}
void dijk(){
long long T, Zx, Zy;
priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long>>, greater<tuple<long long, long long, long long>>> pq;
pq.push(make_tuple(0, 1, 1));
dp[1][1] = 0;
//for(int i = 50; i; --i){
while(!pq.empty()){
tie(T, Zx, Zy) = pq.top();
//cout << Zx << " " << Zy << " " << T << endl;
pq.pop();
if(outside(Zx, Zy) || dp[Zx][Zy] < T)continue;
dp[Zx][Zy] = min(dp[Zx][Zy], T);
d[Zx][Zy] = true;
if(!outside(Zx, Zy + 1) && !d[Zx][Zy + 1])pq.push(make_tuple(T + x[Zx - 1], Zx, Zy + 1));
if(!outside(Zx, Zy - 1) && !d[Zx][Zy - 1])pq.push(make_tuple(T + x[Zx - 1], Zx, Zy - 1));
if(!outside(Zx + 1, Zy) && !d[Zx + 1][Zy])pq.push(make_tuple(T + y[Zy - 1], Zx + 1, Zy));
if(!outside(Zx - 1, Zy) && !d[Zx - 1][Zy])pq.push(make_tuple(T + y[Zy - 1], Zx - 1, Zy));
}
}
int main(){
fill(dp[0], dp[1145], 1145141919810LL);
scanf("%lld%lld%lld%lld", &H, &W, &X, &Y);
for(int i = 0; i < H; ++i)scanf("%lld", x + i);
for(int i = 0; i < W; ++i)scanf("%lld", y + i);
scanf("%lld%lld%lld%lld", &L, &R, &l, &r);
if(L <= X && X <= R && l <= Y && Y <= r)return 0 & puts("-1");
dijk();
//for(int i = 0; i <= H + 1; ++i)
// for(int j = 0; j <= W + 1; ++j)
// cout << dp[i][j] << (j == W + 1 ? "\n": " ");
cout << dp[X][Y] << endl;
}