fork download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <stdlib.h>
  8.  
  9. #define rep( i, l, r ) for (int i = l; i <= r; i++)
  10. #define down( i, l, r ) for (int i = l; i >= r; i--)
  11. #define MAX 1000009
  12.  
  13. using namespace std;
  14.  
  15. int n, num[MAX];
  16. long long a;
  17.  
  18. int main()
  19. {
  20. scanf("%d", &n);
  21. rep(i, 1, n) scanf("%d", &num[i]);
  22. a = 0; rep(i, 1, n) a += num[i]; a /= n;
  23. rep(i, 1, n) num[i] = a - num[i] + num[i-1];
  24. sort(num + 1, num + n + 1);
  25. a = num[n / 2 + 1];
  26. rep(i, 1, n) num[i] -= a;
  27. a = 0;
  28. rep(i, 1, n) a += abs(num[i]);
  29. printf("%lld", a);
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 7248KB
stdin
4
1 2 5 4
stdout
4