0396.cpp (442B)
1 class Solution { 2 public: 3 int maxRotateFunction(const vector<int> &nums) const { 4 const int n = size(nums); 5 int sum = 0, crnt = 0; 6 7 for (int i = 0; i < n; i++) { 8 crnt += i * nums[i]; 9 sum += nums[i]; 10 } 11 12 int res = crnt; 13 for (int i = n - 1; i >= 0; i--) { 14 crnt += sum - nums[i] * n; 15 res = max(res, crnt); 16 } 17 18 return res; 19 } 20 };