提交时间:2024-01-11 15:14:16
运行 ID: 36199
#include <bits/stdc++.h> using namespace std; int a[500], b[500]; int main(){ int n; cin >> n; int sum = 0; for(int i = 1; i <= n; i ++ ){ cin >> a[i]; a[i + n] = a[i]; sum += a[i]; } int ave = sum / n; int ans = 1 << 30; for(int i = 1; i <= n; i ++ ){ for(int j = 1; j <= n; j ++ ){ b[j] = a[j + i - 1]; } int res = 0; for(int j = 1; j <= n; j ++ ){ res += abs(b[j] - ave); b[j + 1] += b[j] - ave; } ans = std::min(ans, res); } cout << ans; return 0; }