fork download
  1. #include<stdio.h>
  2. int a[121212],b[121212];
  3. int main() {
  4. int n,i ,j;
  5. int s = -1, e;
  6. long long ans = 0;
  7. scanf("%d", &n);
  8. for (i = 0; i < n; i++) scanf("%d", &b[i]);
  9. a[0] = b[0];
  10. for (i = 1, j = n - 1; i < n; i++, j--)a[i] = b[j];
  11. for (i = 0; i < n; i++)if (!a[i])s = e = i;
  12. if (s == -1) { printf("0"); return 0; }
  13. int now = 1;
  14. while (s != (e + 1) % n) {
  15. if (now + 1 - a[e + 1] < 0)s=(s-1+n)%n,now += 1 - a[s];
  16. else e=(e+1)%n,now += 1 - a[e];
  17. }
  18. now = s;
  19. for (i = 0; i < n; i++)b[i] = a[now], now = (now + 1)%n;
  20. now = 0;
  21. for (i = 0; i < n; i++) {
  22. while (!b[now])now++;
  23. ans += (now - i)*(now - i);
  24. b[now]--;
  25. }
  26. printf("%lld", ans);
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 4496KB
stdin
10
1
0
0
2
0
0
1
2
2
2
stdout
33