Washroom Satisfaction Index
Time limit10sMemory limit512 MB
After each point update to guest washroom times, compute the minimum sum over assignments of wait-plus-service times to W washrooms, where each washroom serves a subset in some order.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Binary search, Dynamic programming
- Solved
- No attempts yet
Problem
Some guests pick an apartment by counting its washrooms first. Waterbnb wants to take the approximate time each guest spends in a washroom and report a washroom satisfaction index (WSI) for every apartment.
The WSI of one guest is the time that guest waits plus the time that guest spends in the washroom. If two guests use one washroom for 2 minutes and 5 minutes, and a third guest uses it for 3 minutes after them, the WSI of the third guest is minutes.
The WSI of an apartment is defined like this. First assign the guests to the washrooms: who uses which washroom, and in which order. Once the assignment is fixed you can compute the WSI of every guest, and their sum is the WSI of that assignment. The smallest WSI over all assignments is the WSI of the apartment. Every washroom is free at time 0, and one washroom serves one guest at a time.
Take three guests A, B and C whose washroom times are 3 minutes, 5 minutes and 8 minutes. In an apartment with three washrooms each guest gets a washroom of their own, so the WSI is . In an apartment with two washrooms, putting A then B in washroom 1 and C in washroom 2 gives , while putting B then C in washroom 1 and A in washroom 2 gives . Other assignments exist, but none goes below 19, so the WSI of the apartment with two washrooms is 19.
The time a guest needs in the washroom changes from day to day. Every time one guest's time is updated, compute the WSI of the apartment again.
Input
The first line contains the number of test cases ().
The first line of each test case contains the number of guests and the number of washrooms (, ).
The second line contains integers. The -th of them is the time guest spends in a washroom, between and .
The third line contains the number of updates (). Each of the following lines contains two integers and (, ), meaning that the time of guest becomes . The updates are applied one after another in the given order.
Output
For each test case, print the test case number on its own line in the form Case k:, where starts at 1. Then print lines holding the WSI of the apartment right after each update, in order. Do not print the WSI of the initial state.