#C5181. Maximum Fair Candy Distribution

    ID: 48802 Type: Default 1000ms 256MiB

Maximum Fair Candy Distribution

Maximum Fair Candy Distribution

You are given a list of integers representing the number of candies each guest desires, and an integer T representing the total number of candies available. The task is to determine the maximum number of candies that can be fairly distributed among the guests.

For each guest, candies are distributed in increasing order of their demand. In mathematical terms, assume the sorted list is \(a_1, a_2, \ldots, a_n\). Then, for each guest \(i\), if \(T \geq a_i\) then give \(a_i\) candies and update \(T = T - a_i\). Otherwise, give all the remaining \(T\) candies to the current guest and stop the distribution. The result is the sum of all candies actually distributed.

Note: The distribution process stops as soon as the total candies left is less than the next guest's demand.

inputFormat

The first line of the input contains an integer n representing the number of guests.

The second line contains n space-separated integers representing the number of candies each guest desires.

The third line contains an integer T representing the total number of candies available.

outputFormat

Output a single integer, which is the maximum number of candies that can be fairly distributed among the guests.

## sample
4
2 3 7 5
10
10