JAVA || TreeMap || SortedMap || Entry
\n\nclass Solution {\n    class Pair{\n        int x,  y;\n        Pair(int x, int y){\n            this.x = x;\n            this.y = y;\n        }\n        pub
\n```\nclass Solution {\n    class Pair{\n        int x,  y;\n        Pair(int x, int y){\n            this.x = x;\n            this.y = y;\n        }\n        public String toString(){\n            return "[ " + this.x + ", " + this.y+ " ]";\n        }\n    }\n    public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) {\n        int n = nums1.length;\n        Pair[] arr = new Pair[n];\n        Integer[] qrr = new Integer[queries.length];\n        for(int i = 0; i < n; i++){\n            arr[i] = new Pair(nums1[i], nums2[i]);\n        }\n        for(int i = 0; i < queries.length; i++){\n            qrr[i] =i;\n        }\n        Arrays.sort(arr,(a,b)->a.x- b.x);\n        Arrays.sort(qrr,(a,b)-> Integer.compare(queries[b][0] , queries[a][0]));\n\n        int i = 0, j = arr.length -1;\n        // System.out.println(Arrays.toString(arr));\n        int[] ans = new int[queries.length];\n        TreeMap<Integer,Integer> map = new TreeMap<>();\n\n        for(i = 0; i < queries.length; i++){\n            for(;j >= 0 && queries[qrr[i]][0] <= arr[j].x; j--){\n                update(map,arr[j].y, arr[j].x + arr[j].y);\n            }\n            // System.out.println(map);\n            ans[qrr[i]] = query(map,queries[qrr[i]][1]);\n        }\n        return ans;\n    }\n\n    static void update(TreeMap<Integer,Integer> map, int y, int sum){\n        Map.Entry<Integer,Integer> ceil = map.ceilingEntry(y);\n        if(ceil != null && ceil.getValue() >= sum){\n            return;\n        }\n        // int val = t;\n        // TreeMap tailmap = map.tailMap(y);\n        while(map.floorEntry(y) != null){\n            Map.Entry<Integer,Integer> floor = map.floorEntry(y);\n            if(floor.getValue() <= sum){\n                map.remove(floor.getKey());\n            }else{\n                break;\n            }\n        }\n        map.put(y,sum);\n    }\n    static int query(TreeMap<Integer,Integer> map , int y){\n        Map.Entry<Integer,Integer> entry = map.ceilingEntry(y);\n        if(entry == null){\n            return -1;\n        }\n        return entry.getValue();\n    }\n}\n```
['Array', 'Ordered Map', 'Sorting', 'Java']