#P6173. [USACO16FEB] Circular Barn P

    ID: 6947 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2016USACO分治动态规划优化斜率优化四边形不等式

[USACO16FEB] Circular Barn P

Problem Description

As a fan of modern architecture, Farmer John built a new circular barn. Inside the barn, there are nn rooms arranged in a ring (3n10003 \leq n \leq 1000), numbered 1n1 \ldots n in clockwise order. Each room has doors to its left and right neighboring rooms, and also a door leading outside.

FJ wants room ii to contain exactly rir_i cows (1ri1061 \le r_i \le {10}^6). To let the cows enter the barn in an orderly way, he plans to unlock kk doors from the outside into the barn (1k71 \le k \le 7). 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 n,kn, k.

The next nn lines each contain an integer rir_i on line ii.

Output Format

Output the minimum total distance walked by all cows.

6 2
2
5
4
2
6
2
14

Hint

FJ opens doors 22 and 55. 1111 cows enter through door 22 and go to rooms 2,3,42, 3, 4, with a total distance of 88. 1010 cows enter through door 55 and go to rooms 5,6,15, 6, 1, with a total distance of 66.

Translated by ChatGPT 5