## IOI '11 P3 - Rice Hub

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type

Allowed languages
C , C++

In the countryside, you can find a long straight road known as the Rice Way. Along this road there are rice fields. Each field is located at an integer coordinate between and , inclusive. The rice fields will be presented in non-decreasing order of their coordinates. Formally, for , rice field is at coordinate . You may assume that .

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 and , inclusive. The rice hub can be at any location, including one that already contains one or more rice fields.

Each rice field produces exactly truckload of rice every harvest season. To transport the rice to the hub, the city has to hire a truck driver. The driver charges Baht to transport a truckload of rice per unit of distance towards the hub. In other words, the cost of transporting rice from a given field to the rice hub is numerically equal to the difference between their coordinates.

Unfortunately, our budget for this season is tight: we may only spend at most Baht on transportation. Your task is to help us strategically place the hub to gather as much rice as possible.

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 , , , and .

For this case, there are multiple optimal locations for the hub: you can place it anywhere between locations and , inclusive. The figure above shows one of these optimal locations. You will then be able to transport rice from fields at coordinates , , and . For any optimal hub location, the total cost of this transportation will be at most Baht. Clearly, no hub location will allow us to gather rice from more than three fields, hence this solution is optimal and besthub should return 3.