BDOI Knapsack (En)
BDOI Knapsack (En)
Knapsack
Tasmeem the magician has N items. The cost of ith item is C[i] taka, and the weight of ith item is
W [i] kg. Tasmeem also has a sack that can contain any number of items as long as their total
weight is at most X kg. An emerging magician Pepe has challenged Tasmeem to play a game with
him.
First, Tasmeem will give Pepe the original costs of all N items. Then Pepe can do the following
operation at most 32 times:
Pepe will change the cost of each item. After that, the cost of ith item will be R[i] (0 ≤
R[i] ≤ 107 ). He will give the new costs to Tasmeem with a non-negative integer K .
Tasmeem will try to find the optimal set of items that he can put into the sack such that the
sum of changed costs (according to R) are maximised. Suppose the optimal total cost is K ′
. He will tell Pepe the value of min(K, K ′ )
After that, Pepe need to guess the maximum total original cost (according to C ) for the optimal
set. Since Tasmeem is very generous, Pepe will get points if the cost he guessed is in between 99%
and 100% of the actual cost (inclusive). However, there are some rules:
Implementation Details
The above procedure can make calls to the following procedure to interact with Tasmeem.
knapsack (1 of 3)
R: an array of N non-negative integers, describing the changed costs. Values of R must not
exceed 107 .
K : a non-negative integer described above.
This procedure returns an integer representing Tasmeem’s reply.
SK should not exceed 106 over all calls, where SK is the sum of K over all calls in a single
test.
This procedure can be called at most 32 times.
Your code must scan anything from the standard input (i.e. use
scanfor
cin) or print anything to
the standard output (i.e. use printfor
cout
). However, you can print debugging information to
stderr. For communication, use the functions described above.
The grader is not adaptive, that is, the values of W and X are fixed before a call to
guess_knapsackis made.
Constraints
1 ≤ N ≤ 100
1 ≤ C[i] ≤ 107 for (1 ≤ i ≤ N )
0 ≤ X ≤ 5000
1 ≤ W [i] ≤ 5000 for (1 ≤ i ≤ N )
X and W are hidden from contestants and only used for internal processes.
Subtasks
For the last subtask, your score is determined using a formula. Let’s assume SK is the maximum
sum of K over all operations in a test case. Your final score will be calculated using the following
table:
SK Score
SK > 106
0
2
106 − SK
300N < SK ≤ 106 ( 6 ) × 80
10 − 300N
SK ≤ 300N
80
knapsack (2 of 3)
Example
N =5
W = {1, 1, 10, 2, 1}
X=2
In this case, the maximum total cost of items that Tasmeem can put in his sack together is 500 (by
taking the first and the last items). However, this procedure will return 100 since K = 100 < 500.
The solution using original cost is 300 (by taking the first two items). In order to get points,
guess_knapsackmust return any integer in between 99% ⋅ 300 = 297 and 300 (inclusive).
Sample Grader
Input format:
line 1: N X
line 2: C[1] C[2] … C[N ]
line 3: W [1] W [2] … W [N ]
If you violate any rule, you will get an output message accordingly. Otherwise, the grader will
output in the following format:
line 1:
Contestant:A
line 2:
Original:B
line 3:
Sum of Ks:SK
all calls to
try_knapsack.
Note that the grader used for actual evaluation is different from the sample grader.
knapsack (3 of 3)