/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
	{
		 List<Integer> numbers = Arrays.asList(5,10,4,-1);
        Ideone mm = new Ideone();
        int maxi = mm.maximumm(numbers);
        System.out.println(maxi);
	}
	
	 public int maximumm(List<Integer> numbers) {
       int toIndex = 3, fromIndex = 0;
        List<Integer> result = new ArrayList<>();
        while (toIndex < numbers.size()) {
            Map<Integer, Integer> map = IntStream
                    .range(fromIndex, toIndex)
                    .filter(i->numbers.get(i)>0)
                    .mapToObj(i -> new AbstractMap.SimpleEntry<>(i, numbers.get(i)))
                    .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey,(a,b)->a));
            // find max of sublist
            int maxOfSub = numbers.subList(fromIndex, toIndex).stream().max(Integer::compareTo).get();
            //update indexes
            fromIndex = map.getOrDefault(maxOfSub,toIndex-1) + 2;
            toIndex += fromIndex;

            if (maxOfSub > 0)
                result.add(maxOfSub);
        }
        int lastMax = numbers.subList(fromIndex, numbers.size()).stream().max(Integer::compareTo).get();
        if (lastMax > 0)
            result.add(lastMax);
        System.out.println(result);
		return result.stream().reduce(0,Integer::sum);
    }
}