The key point is to maintain that:

**sum2=sum1+a0+a1+...+a(n-2)-(n-1)a(n-1)**

```
public int maxRotateFunction(int[] A) {
int n = A.length;
int sum = 0;
int current = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
current += i * A[i];
}
int result = current;
for (int i = n - 1; i > 0; i--) {
current += sum - n * A[i];
result = Math.max(result, current);
}
return result;
}
```