```
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int n = A.size(), sum = 0, tmp1 = 0, tmp2;
int ans = 0;
for(int i = 0; i < n; i++){
sum += A[i];
tmp1 += i * A[i];
}
ans = tmp1;
for(int i = 1; i < n; i++){
tmp2 = sum + tmp1 - n * A[n - i];
ans = max(ans, tmp2);
tmp1 = tmp2;
}
return ans;
}
};
```

**My derivation:**