#include #include #include #include struct Town { Town() = default; unsigned long long population; long long location; }; struct Cloud { Cloud() = default; long long location; long long range; unsigned long long ownPeople = 0; }; int main() { int nTowns; std::cin >> nTowns; std::vector towns(nTowns); for (int i = 0; i < nTowns; ++i) std::cin >> towns[i].population; for (int i = 0; i < nTowns; ++i) std::cin >> towns[i].location; std::sort(towns.begin(), towns.end(), [](Town& a, Town& b) { return a.location < b.location; }); int nClouds; std::cin >> nClouds; std::vector clouds(nClouds); for (int i = 0; i < nClouds; ++i) std::cin >> clouds[i].location; for (int i = 0; i < nClouds; ++i) std::cin >> clouds[i].range; std::vector> lefts; std::vector> rights; lefts.reserve(nClouds); rights.reserve(nClouds); for (int i = 0; i < nClouds; ++i) { lefts.push_back(std::make_pair(clouds[i].location - clouds[i].range, i)); rights.push_back(std::make_pair(clouds[i].location + clouds[i].range, i)); } std::sort(lefts.begin(), lefts.end(), [](std::pair a, std::pair b) { return a.first < b.first; }); std::sort(rights.begin(), rights.end(), [](std::pair a, std::pair b) { return a.first < b.first; }); // people that have 0 clouds to begin with. unsigned long long sunnyPeople = 0; unsigned long long maxCloudPeople = 0; int nextLeft = 0; int nextRight = 0; std::unordered_set activeClouds; for (auto& town : towns) { while (nextLeft < lefts.size() && lefts[nextLeft].first <= town.location) { activeClouds.insert(lefts[nextLeft].second); ++nextLeft; } while (nextRight < rights.size() && rights[nextRight].first < town.location) { activeClouds.erase(rights[nextRight].second); ++nextRight; } if (activeClouds.empty()) { sunnyPeople += town.population; } else if (activeClouds.size() == 1) { int onlyId = *activeClouds.begin(); clouds[onlyId].ownPeople += town.population; maxCloudPeople = std::max(maxCloudPeople, clouds[onlyId].ownPeople); } } std::cout << sunnyPeople + maxCloudPeople << '\n'; return 0; }