#P2157. [SDOI2009] 学校食堂
[SDOI2009] 学校食堂
Description
Xiao F’s school is in a remote corner of the city, so all students have to eat at school. The school has a canteen which, although simple, always serves dishes that satisfy the students. Of course, people have different tastes, but each person’s taste can be represented by a non-negative integer. Due to limited staff, the canteen can only cook for one person at a time. The time to cook each dish depends on the previous one: if the previous dish has taste and the current dish has taste , then the time needed to cook this dish is , and no time is counted for the first dish. Here, and denote bitwise OR and bitwise AND on integers, corresponding to the C language operators | and &.
There are many students in this school, so cooking often takes a lot of time. Therefore, the canteen may sometimes deviate from the original queue order to shorten the total dining time.
Although students can understand this practice, everyone has a certain tolerance. Specifically, the -th student in the queue allows at most the people immediately behind them to be served before them. If any student beyond those immediately behind is served earlier than the -th student, the -th student will be very angry. Thus, the canteen also needs to take students’ emotions into account. Now, under the premise of satisfying everyone’s tolerance, Xiao F wants to know the minimum total time needed for the school canteen to finish all the dishes.
Input Format
The first line contains a positive integer , the number of test cases.
For each test case, the first line contains a positive integer , the number of students.
Starting from the second line of each test case, there are lines. Each line contains two space-separated non-negative integers and , representing, in queue order from front to back, each student’s required dish taste and their tolerance.
There are no extra blank lines between test cases.
Output Format
Output lines, each containing one integer, the minimum total time for the canteen to finish all dishes for the corresponding test case.
2
5
5 2
4 1
12 0
3 3
2 2
2
5 0
4 0
16
1
Hint
For the first test case:
- Student 1 allows Student 2 or Student 3 to be served before them; Student 2 allows Student 3 to be served before them; Student 3 is strict and must be served before any student behind them.
- One optimal order is to cook for Students 3, 2, 1, 4, 5. The time for each dish is respectively , , , , and .
Constraints:
- For of the testdata, .
- For of the testdata, , , , .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号