Easy Explanation and just by sorting
\n# Code\n\nclass Solution {\npublic:\n    vector<int> maximumSumQueries(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& q) {\n        int n = nums
\n# Code\n```\nclass Solution {\npublic:\n    vector<int> maximumSumQueries(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& q) {\n        int n = nums1.size();\n\n        // Step 1: Create a vector of pairs and sort it in reverse order based on the first element (nums1)\n        vector<pair<int, int>> vec;\n        for (int i = 0; i < n; i++) {\n            vec.emplace_back(nums1[i], nums2[i]);\n        }\n        sort(vec.rbegin(), vec.rend());\n        assert(vec.size() == n);\n\n        // Step 2: Create a vector of indices and sort it based on the queries in descending order\n        vector<int> ord(q.size());\n        iota(ord.begin(), ord.end(), 0);\n        sort(ord.begin(), ord.end(), [&](auto& x, auto& y) {\n            return q[x] > q[y];\n        });\n\n        // Step 3: Create a map to store the maximum sum for each value of x\n        map<int, int> val;\n        int l = 0;\n        vector<int> ans(q.size());\n\n        // Step 4: Process the queries and find the maximum sums\n        for (int& i : ord) {\n            // Process the remaining elements in the vector of pairs\n            while (l < n && vec[l].first >= q[i][0]) {\n                auto [y, x] = vec[l++];\n\n                // Check if there is a higher value of x already present in the map\n                if (val.lower_bound(x) != val.end() && val.lower_bound(x)->second >= x + y)\n                    continue;\n\n                auto it = val.lower_bound(x);\n                while (it != val.begin()) {\n                    it--;\n                    if (it->second < x + y) {\n                        val.erase(it);\n                    } else {\n                        break;\n                    }\n                    it = val.lower_bound(x);\n                }\n\n                // Store the maximum sum for this value of x\n                val[x] = x + y;\n            }\n\n            // Find the answer for the current query\n            if (val.lower_bound(q[i][1]) == val.end()) {\n                ans[i] = -1;\n            } else {\n                ans[i] = val.lower_bound(q[i][1])->second;\n            }\n        }\n\n        return ans;\n    }\n};\n\n```
['C++']