#FT0000231. Bài toán những viên kẹo (Many Sweets Problem)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài toán những viên kẹo (Many Sweets Problem)
Many Sweets Problem
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a sequence of positive integers representing the deliciousness of sweets, indexed from .
Process queries in order. Each query consists of five integers c x l r k and executes two consecutive steps:
- Assign . This change affects the current and all subsequent queries.
- Consider the sweets with deliciousness values after the assignment. Choose a subset of these sweets, taking each sweet at most once, to reach a total deliciousness at least . Find the minimum number of sweets needed.
If even eating every sweet in the interval cannot reach , output -1. Choosing sweets does not remove them from the sequence: only the point assignment changes .
Input
- The first line contains integers , the sequence length and number of queries.
- The second line contains exactly integers .
- Each of the next lines contains five integers , specifying the updated position, its new value, the inclusive query interval, and the required deliciousness.
Output
Print lines. Line contains the minimum number of sweets to select after assignment , or exactly -1 if the required total is unreachable.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; .
Examples
Input
7 5
8 2 4 1 7 3 6
1 1 4 7 9
5 2 1 3 8
6 4 1 5 9
6 5 3 5 1
7 9 4 6 4
Output
2
-1
4
1
1
Explanation
The initial sequence is , and every assignment happens before selecting sweets.
- Set . Interval is . Its largest sweet has value , while choosing values and reaches . Answer:
2. - Set . Interval is , whose total is only . Answer:
-1. - Set . Interval is . The three largest values sum to ; four sweets can sum to . Answer:
4. - Set . Interval is . One sweet is enough to reach . Answer:
1. - Set . Interval is . One sweet of value reaches . Answer:
1.
Selecting sweets only computes the answer and never removes values from the array.