Choose a starting fruit, then walk forward eating each fruit that still fits within capacity C, and report the largest count.
Easy3SimulationBrute forceInterviewNo attempts yetTime limit1sMemory limit64 MBMislav likes spending time outdoors, and he likes forests most of all. The clean air and the pleasant sounds are the reason. He is going to the forest this afternoon, and since he is a practical boy, he plans to fill his stomach along the way. His stomach holds fruit of total weight at most C.
While walking through the forest he meets mushrooms, chestnuts, berries and other fruits of nature, one after another. The fruits are all of different kinds, and Mislav wants to eat as many of them as he can without overeating. The total weight of the fruits he eats must not exceed C.
Mislav picks one fruit to start eating at. From that fruit to the last one, he looks at every fruit in the order he meets it: if eating it keeps the total weight at most C, he eats it, otherwise he walks past it. The fruit he picked to start at is also walked past when it alone already exceeds C.
You are given the weights of the N fruits in the order Mislav meets them. Determine the largest number of fruits Mislav can eat.
The first line contains the number of fruits N and the capacity C, separated by a space. (1≤N≤1000, 1≤C≤1000000)
The second line contains the weights w1,w2,…,wN of the fruits in the order Mislav meets them. (1≤wi≤1000)
Print the largest number of fruits Mislav can eat.
Take the first example. If Mislav starts at the first fruit, of weight 3, he eats 3, 1 and 1, which is three fruits. If he starts at the second fruit, of weight 1, he eats 1, 2, 1 and 1, which is four fruits.