P1109 Student Grouping

Description

There are $n$ groups of students. You are given the initial number of students in each group, and the upper bound $R$ and lower bound $L$ for each group size (with $L \le R$). In one operation, you may select one student from a group and move them to another group. What is the minimum number of operations required to make the sizes of all $n$ groups fall within $[L, R]$?

Input Format

The first line contains an integer $n$, the number of student groups. The second line contains $n$ integers, the number of students in each group. The third line contains two integers $L, R$, the lower and upper bounds.

Output Format

Output a single integer, the minimum number of moves. If it is impossible to satisfy the condition, output -1.

Explanation/Hint

Constraints For all testdata, it is guaranteed that $1 \le n \le 50$. Translated by ChatGPT 5