#FT0000051. Dãy con tối ưu – phiên bản khó (Optimal Subsequences (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con tối ưu – phiên bản khó (Optimal Subsequences (Hard Version))
Optimal Subsequences (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A subsequence of selects positions in strictly increasing order; chosen positions need not be consecutive. For every length , an optimal subsequence is one with maximum element sum, and among those with that maximum sum it is lexicographically smallest. Sequences are compared at their first differing element; the smaller element makes the sequence lexicographically smaller. Equal value sequences are equivalent.
Each of independent queries supplies and . Output the value at the one-based position of the optimal length- subsequence. The input sequence remains unchanged.
Input
- Line 1: integer , the length.
- Line 2: exactly integers .
- Line 3: integer , the number of queries.
- Next lines: query contains specifying the subsequence length and the requested position.
Output
Print lines, one integer per query, in the original query order.
Subtasks
Subtask 1 (20 points):
- ; for every ; for every query .
Subtask 2 (30 points):
- ; for every ; for every query .
Subtask 3 (50 points):
- ; for every ; for every query .
Examples
Examples 1
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Explanation
For , the best subsequence is . For , both and total , but is lexicographically smaller. For , the entire sequence is required. Reading the six requested positions yields the shown six answers.
Examples 2
Input
4
10 20 30 20
3
2 1
2 2
4 3
Output
20
30
30
Explanation
The maximum sum for length two is . Among and , choose by the lexicographic tie-break. For length four, the whole sequence is selected, whose third value is .