#P6173. [USACO16FEB] Circular Barn P
[USACO16FEB] Circular Barn P
Problem Description
As a fan of modern architecture, Farmer John built a new circular barn. Inside the barn, there are rooms arranged in a ring (), numbered in clockwise order. Each room has doors to its left and right neighboring rooms, and also a door leading outside.
FJ wants room to contain exactly cows (). To let the cows enter the barn in an orderly way, he plans to unlock doors from the outside into the barn (). Then, each cow walks clockwise until it reaches its destination. FJ's goal is to minimize the total distance walked by all cows (the entry door for each cow can be chosen freely; the distance only counts walking after entering the barn). Please find this minimum total distance.
Input Format
The first line contains two integers .
The next lines each contain an integer on line .
Output Format
Output the minimum total distance walked by all cows.
6 2
2
5
4
2
6
2
14
Hint
FJ opens doors and . cows enter through door and go to rooms , with a total distance of . cows enter through door and go to rooms , with a total distance of .
Translated by ChatGPT 5