# USACO 2016 Feb Silver

## Problem 1. Circular Barn

Circular Barn

Being a fan of contemporary architecture, Farmer John has built a new barn in the shape of a perfect circle. Inside, the barn consists of a ring of n rooms, numbered clockwise from 1…n around the perimeter of the barn (3≤n≤1000). Each room has doors to its two neighboring rooms, and also a door opening to the exterior of the barn.
Farmer John owns n cows, and he wants exactly one cow to end up in each room in the barn. However, the cows, being slightly confused, line up at haphazard doors, with possibly multiple cows lining up at the same door. Precisely ci cows line up outside the door to room i, so ∑ci=n.

To manage the process of herding the cows so that one cow ends up in each room, Farmer John wants to use the following approach: each cow enters at the door at which she initially lined up, then walks clockwise through the rooms until she reaches a suitable destination. Given that a cow walking through d doors consumes d2 energy, please determine the minimum amount of energy needed to distribute the cows so one ends up in each room.

INPUT FORMAT (file cbarn.in):
The first line of input contains n. Each of the remaining n lines contain c1…cn.
OUTPUT FORMAT (file cbarn.out):
Please write out the minimum amount of energy consumed by the cows.
SAMPLE INPUT:

10
1
0
0
2
0
0
1
2
2
2


SAMPLE OUTPUT:

33


Problem credits: Brian Dean

Farmer John’s N cows are each standing at distinct locations (x1,y1)…(xn,yn) on his two-dimensional farm (1≤N≤1000, and the xi’s and yi’s are positive odd integers of size at most 1,000,000). FJ wants to partition his field by building a long (effectively infinite-length) north-south fence with equation x=a (a will be an even integer, thus ensuring that he does not build the fence through the position of any cow). He also wants to build a long (effectively infinite-length) east-west fence with equation y=b, where b is an even integer. These two fences cross at the point (a,b), and together they partition his field into four regions.
FJ wants to choose a and b so that the cows appearing in the four resulting regions are reasonably “balanced”, with no region containing too many cows. Letting M be the maximum number of cows appearing in one of the four regions, FJ wants to make M as small as possible. Please help him determine this smallest possible value for M.

INPUT FORMAT (file balancing.in):
The first line of the input contains a single integer, N. The next N lines each contain the location of a single cow, specifying its x and y coordinates.
OUTPUT FORMAT (file balancing.out):
You should output the smallest possible value of M that FJ can achieve by positioning his fences optimally.
SAMPLE INPUT:

7
7 3
5 5
7 13
3 1
11 7
5 3
9 1


SAMPLE OUTPUT:

2


## Problem 3. Milk Pails

Milk Pails

Farmer John has received an order for exactly M units of milk (1≤M≤200) that he needs to fill right away. Unfortunately, his fancy milking machine has just become broken, and all he has are two milk pails of integer sizes X and Y (1≤X,Y≤100) with which he can measure milk. Both pails are initially empty. Using these two pails, he can perform up to K of the following types of operations (1≤K≤100):

• He can fill either pail completely to the top.

• He can empty either pail.

• He can pour the contents of one pail into the other, stopping when the former becomes empty or the latter becomes full (whichever of these happens first).

Although FJ realizes he may not be able to end up with exactly M total units of milk in the two pails, please help him compute the minimum amount of error between M and the total amount of milk in the two pails. That is, please compute the minimum value of M−M′ such that FJ can construct M′ units of milk collectively between the two pails.

INPUT FORMAT (file pails.in):
The first, and only line of input, contains X, Y, K, and M.
OUTPUT FORMAT (file pails.out):
Output the smallest distance from M to an amount of milk FJ can produce.
SAMPLE INPUT:

14 50 2 32


SAMPLE OUTPUT:

18


In two steps FJ can be left with the following quanities in his pails

(0, 0) = 0 units
(14, 0) = 14 units
(0, 50) = 50 units
(0, 14) = 14 units
(14, 36) = 50 units
(14, 50) = 64 units
The closest we can come to 32 units is 14 for a difference of 18. Note that it would require an extra step to pour out the first pail to end up with (0, 36).

Problem credits: Brian Dean