In the countryside, you can find a long straight road known as the Rice Way. Along this road there are
Please note that multiple rice fields may share the same coordinate.
We plan to construct a single rice hub as a common place to store as much of the harvest as possible. As with the rice fields, the hub has to be at an integer coordinate between
Each rice field produces exactly
Unfortunately, our budget for this season is tight: we may only spend at most
Your task
Write a procedure besthub(R,L,X,B)
that takes the following parameters:
– the number of rice fields. The fields are numbered through . – the maximum coordinate. – a one-dimensional array of integers sorted from smallest to largest.
For each , field is located at . – the budget.
Your procedure must find an optimal location of the hub and return the maximum number of truckloads of rice that can be transported to the hub within the budget.
Note that the total cost of transporting the rice can be very large. The budget is given as a 64-bit integer, and we recommend that you use 64-bit integers in your computation. In C/C++, use the type long long
; in Pascal, use the type Int64
.
Example
Consider the case where
For this case, there are multiple optimal locations for the hub: you can place it anywhere between locations besthub
should return 3.
Subtasks
Subtask 1 (17 points)
- No two rice fields share the same coordinate (only for this subtask).
Subtask 2 (25 points)
Subtask 3 (26 points)
Subtask 4 (32 points)
Implementation details
Interface (API)
To be implemented by contestant:
int besthub(int R, int L, int X[], long long B);
Comments