1157: 均分纸牌

文件提交:无需freopen 内存限制:128 MB 时间限制:1.000 S
评测方式:普通裁判 命题人:
提交:2 解决:2

题目描述

有 lns="http://www.w3.org/1998/Math/MathML"> 堆纸牌,编号分别为 lns="http://www.w3.org/1998/Math/MathML">1,2,,。每堆上有若干张,但纸牌总数必为 lns="http://www.w3.org/1998/Math/MathML"> 的倍数。可以在任一堆上取若干张纸牌,然后移动。

移牌规则为:在编号为 lns="http://www.w3.org/1998/Math/MathML">1 堆上取的纸牌,只能移到编号为 lns="http://www.w3.org/1998/Math/MathML">2 的堆上;在编号为 lns="http://www.w3.org/1998/Math/MathML"> 的堆上取的纸牌,只能移到编号为 lns="http://www.w3.org/1998/Math/MathML">1 的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。

现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。

例如 lns="http://www.w3.org/1998/Math/MathML">=4 时,lns="http://www.w3.org/1998/Math/MathML">4 堆纸牌数分别为 lns="http://www.w3.org/1998/Math/MathML">9,8,17,6

移动 lns="http://www.w3.org/1998/Math/MathML">3 次可达到目的:

  • 从第三堆取 lns="http://www.w3.org/1998/Math/MathML">4 张牌放到第四堆,此时每堆纸牌数分别为 lns="http://www.w3.org/1998/Math/MathML">9,8,13,10
  • 从第三堆取 lns="http://www.w3.org/1998/Math/MathML">3 张牌放到第二堆,此时每堆纸牌数分别为 lns="http://www.w3.org/1998/Math/MathML">9,11,10,10
  • 从第二堆取 lns="http://www.w3.org/1998/Math/MathML">1 张牌放到第一堆,此时每堆纸牌数分别为 lns="http://www.w3.org/1998/Math/MathML">10,10,10,10

输入

第一行共一个整数 lns="http://www.w3.org/1998/Math/MathML">,表示纸牌堆数。
第二行共 lns="http://www.w3.org/1998/Math/MathML"> 个整数 lns="http://www.w3.org/1998/Math/MathML">1,2,,,表示每堆纸牌初始时的纸牌数。

输出

共一行,即所有堆均达到相等时的最少移动次数。

样例输入

4
9 8 17 6

样例输出

3

提示

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,lns="http://www.w3.org/1998/Math/MathML">1100lns="http://www.w3.org/1998/Math/MathML">110000

来源/分类