Inheritance
시간 제한1.5초메모리 제한256 MB
K개의 사과를 N명의 자녀에게 나눠 주되, 자녀들이 받은 양의 격차와 손주들이 받은 양의 격차가 모두 최소가 되도록 분배하는 문제이다.
문제
You are the proud owner of apples. You want to leave them as inheritance to your children and grandchildren. You have children. Each of these children has children of their own. The array describes this: its -th value is equal to the number of children your -th child has. So, your total number of grandchildren is equal to . It is guaranteed that for all .
You must decide how many apples you will give to each of your children. You must distribute all apples, none may remain. Also, you many only distribute whole apples. After you distribute the apples among your children, they will redistribute the apples between their children. Because you want to be as fair as possible, you want to find the minimum value such that:
- The difference between the maximum number of apples given to a child and the minimum number of apples given to a child does not exceed .
- The difference between the maximum number of apples given to a grandchild and the minimum number of apples given to a grandchild does not exceed .
Because your children are fair as well, you may assume that they will redistribute the apples given to them in such a way that will be kept as small as possible. It is, however, up to you to decide how many apples you will give to each direct child of yours.
Output the minimum possible value and the quantities of apples given to each child to achieve this value.
입력
The first line of input contains two integers and (, ).
The second line contains integers, the -th of them is . The total number of grandchildren does not exceed .
출력
The first line must contain the value . The second line must contain non-negative integers: the number of apples given to each child. The sum of these integers must be equal to . Any distribution of apples that leads to the correct value of will be accepted.
힌트
If you give 43 apples to your first child and 57 apples to your second child, they will split them in the following way. The first child will give all 43 apples to his only child. The second child will give 28 apples to one of her children and 29 apples to the other.
The maximum difference between apples received by children is . The maximum difference between apples received by grandchildren is .