School Democracy
Time limit2sMemory limit512 MB
Partition the classes into consecutive groups of size between l and r, and maximize the total difference between elected boys and girls, where each group elects the side with more votes or both on a tie.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Sliding window, Greedy
- Solved
- No attempts yet
Problem
School number 932 is holding an election for the school council. The election works as follows. The vice principal takes the list of all classes in the school and splits them into groups. Each group consists of one or more classes that are consecutive in the list, and every class ends up in exactly one group.
Each group of classes nominates two candidates to the school council, one boy and one girl. Then every student votes for one of the two candidates nominated by their group. Boys always vote for the boy candidate, and girls always vote for the girl candidate. Votes are counted independently in each group. The candidate with the most votes in their group is elected to the school council. If the votes are tied, both candidates nominated by that group are elected to the school council.
Suppose the election sends boys and girls to the school council. From past years' experience, the vice principal believes the council works more efficiently the larger the difference between the number of boys and the number of girls. This value can be negative, and the vice principal wants to maximize the value itself, not its absolute value. For example, between , , where , and , , where , the second option is preferable.
The school has classes in total, and the vice principal has already prepared their list. Now he has to split them into groups. A group cannot contain fewer than classes, or the council will be very large. At the same time, a group cannot contain more than classes, or the students will not be able to agree on the candidates to nominate. Recall that each group must consist of classes that are consecutive in the vice principal's list.
Help the vice principal find the optimal split into groups by his judgment.
Input
The first line of the input contains three integers , , and (, ): the number of classes in the school and the minimum and maximum allowed number of classes in one group, respectively. The following lines contain two integers and each (): the number of boys and girls in the -th class, respectively.
Output
In the first line, print the integer , the number of groups in the optimal split by the vice principal's judgment. In the following lines, print two integers and (). This means that the -th group should include classes from the -th to the -th, inclusive. The groups may be printed in any order. Every class must be in exactly one group.
At least one split satisfying all constraints is guaranteed to exist. If there are several optimal answers, print any of them.