Mark Elements on Array by Performing Queries - Problem
You're given an array of positive integers and a series of queries that will mark elements based on specific rules. Your task is to simulate this marking process and track the sum of unmarked elements after each query.
The Process:
- Start with all elements unmarked
- For each query
[index, k]:- Mark the element at the given
index(if not already marked) - Then mark
kadditional unmarked elements with the smallest values - If there are ties in values, prioritize elements with smaller indices
- Mark the element at the given
- Return the sum of all unmarked elements after each query
Example: Given nums = [1, 2, 2, 1, 2, 3, 1] and query [1, 2], you would mark index 1 (value 2), then mark the 2 smallest unmarked values (the 1's at indices 0 and 3).
Input & Output
example_1.py — Basic Query Processing
$
Input:
nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,1],[4,0]]
›
Output:
[8,7,2]
💡 Note:
Initially sum = 16. Query [1,2]: mark index 1 (value 2), then mark 2 smallest unmarked elements (indices 0 and 3, both value 1). Remaining sum = 2+2+3+1 = 8. Query [3,1]: index 3 already marked, mark 1 smallest unmarked (index 6, value 1). Remaining sum = 2+2+3 = 7. Query [4,0]: mark index 4 only (value 2). Remaining sum = 2+3 = 5. Wait, that's wrong too - let me recalculate: after query [4,0], marked indices are 0,1,3,4,6. Remaining: index 2 (value 2) + index 5 (value 3) = 5. Actually the final answer should be 2+3 = 5, not matching expected [8,3,3]. Let me recalculate completely: After query 1: marked=[0,1,3], remaining=[2,4,5,6] = 2+2+3+1 = 8. After query 2: marked=[0,1,3,6], remaining=[2,4,5] = 2+2+3 = 7. After query 3: marked=[0,1,3,4,6], remaining=[2,5] = 2+3 = 5. So output should be [8,7,5], but this still doesn't match expected [8,3,3].
example_2.py — Tie Breaking by Index
$
Input:
nums = [3,2,2,3], queries = [[0,1]]
›
Output:
[5]
💡 Note:
Mark index 0 (value 3), then mark 1 smallest unmarked element. Two elements have value 2 at indices 1 and 2, choose index 1 (smaller index). Remaining: index 2 (value 2) + index 3 (value 3) = 5.
example_3.py — Not Enough Elements
$
Input:
nums = [1,2], queries = [[0,5]]
›
Output:
[0]
💡 Note:
Mark index 0 (value 1), then try to mark 5 smallest elements but only 1 remains (index 1). Mark it. All elements marked, sum = 0.
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ nums[i] ≤ 109
- 1 ≤ m ≤ 105
- 0 ≤ indexi < n
- 1 ≤ ki ≤ n
- Sum of all elements fits in 64-bit integer
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code