#FT0000151. Truy vấn tổng lớn nhất (Maximum Sum Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Truy vấn tổng lớn nhất (Maximum Sum Queries)
Maximum Sum Queries
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two arrays of positive integers and . Index defines a pair and score .
There are independent queries . For a query you may select only an index meeting both and . If at least one eligible index exists, output the maximum among eligible indices; otherwise output -1.
Queries never remove or update array elements. Output answers in original query order.
Input
The first line contains . The second line contains all and the third contains all . The next lines contain thresholds .
Output
Print lines. Line gives the maximum qualifying sum or -1 if no index is eligible.
Subtasks
- Subtask 1 (20 points): ; for all array positions and queries.
- Subtask 2 (30 points): ; for all array positions and queries.
- Subtask 3 (50 points): ; for all array positions and queries.
Examples
Example 1
Input
4 3
4 3 1 2
2 4 9 5
4 1
1 3
2 5
Output
6
10
7
Explanation
For , only qualifies, with sum 6. For , qualified sums are , and , so the maximum is 10. For , only qualifies, giving 7.
Example 2
Input
2 3
2 1
2 3
3 3
1 3
2 2
Output
-1
4
4
Explanation
Query has no index with , hence -1. For only pair qualifies, giving 4; for only qualifies, also giving 4.