#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #ifdef LOCAL #define dbg(x) cerr << #x " = " << x << endl; #include "pretty_print.h" #else #define dbg(x) #endif typedef long double ld; typedef long long ll; typedef unsigned long long ull; #define snd second #define fst first template T sqr(T x) { return x * x; } template T abs(T x) { return x < 0? -x : x; } template T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; } #define X first #define ID second #define P second int main(int /* argc */, char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL assert(freopen("inp", "r", stdin)); assert(freopen("out", "w", stdout)); #endif int n; cin >> n; vector < pair > towns(n); for (int i = 0; i < n; ++i) { cin >> towns[i].P; } for (int i = 0; i < n; ++i) { cin >> towns[i].X; } int m; cin >> m; vector y(m); vector < pair > clouds; for (int i = 0; i < m; ++i) { cin >> y[i]; } for (int i = 0; i < m; ++i) { int r; cin >> r; clouds.push_back(make_pair(y[i] - r, i)); clouds.push_back(make_pair(y[i] + r + 1, i)); } sort(towns.begin(), towns.end()); sort(clouds.begin(), clouds.end()); vector saved(m, 0); set open_clouds; ll answer = 0; int j = 0; for (int i = 0; i < (int)clouds.size(); ++i) { auto& cloud = clouds[i]; while (j < (int)towns.size() && towns[j].X < cloud.X) { if (open_clouds.size() == 0) { answer += towns[j].P; } else if (open_clouds.size() == 1) { saved[*open_clouds.begin()] += towns[j].P; } ++j; } auto it = open_clouds.find(cloud.ID); if (it == open_clouds.end()) { open_clouds.insert(cloud.ID); } else { open_clouds.erase(it); } } while (j < (int)towns.size()) { answer += towns[j++].P; } ll opt = *max_element(saved.begin(), saved.end()); cout << answer + opt << endl; cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; return 0; }