```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define reg register
#define il inline
int n;
ull a[1000010];
ull sum, ans;
int main()
{
while(scanf("%d", &n) != EOF)
{
sum = ans = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%llu", &a[i]);
sum += a[i];
}
sum /= n;
for(int i = 1; i <= n; ++i)
a[i] = a[i] - sum + a[i - 1];
std::sort(a + 1, a + n + 1);
for(int i = 1; i <= n / 2; ++i)
ans += a[n - i + 1] - a[i];
printf("%llu\n", ans);
}
return 0;
}
```
by Kobe303 @ 2021-08-13 11:43:51
```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define reg register
#define il inline
int n;
ll a[1000010];
ll sum, ans;
int main()
{
while(scanf("%d", &n) != EOF)
{
sum = ans = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%lld", &a[i]);
sum += a[i];
}
sum /= n;
for(int i = 1; i <= n; ++i)
a[i] = a[i] - sum + a[i - 1];
std::sort(a + 1, a + n + 1);
for(int i = 1; i <= n / 2; ++i)
ans += a[n - i + 1] - a[i];
printf("%lld\n", ans);
}
return 0;
}
```
by Kobe303 @ 2021-08-13 11:44:17
上面一份是错的,下面一份是对的
by Kobe303 @ 2021-08-13 11:44:53
@[Kobe303](/user/292300) 手写输出试试看qwq
by fjy666 @ 2021-08-13 12:03:53
@[Kobe303](/user/292300) 如果`a[i]`小于`sum`会出现自然溢出(ull 不支持负数) `a[i] = a[i] - sum + a[i - 1];`
by fjy666 @ 2021-08-13 12:05:07
@[fjy666](/user/366338)
酱紫嘛,谢谢啦
by Kobe303 @ 2021-08-13 12:07:33
@[fjy666](/user/366338)
所以题目为什么要求在64位无符号整数范围内
by Kobe303 @ 2021-08-13 12:08:20