C++ sort & sum||88ms Beats 95.92%
Intuition\n Describe your first thoughts on how to solve this problem. \nCompute firstly the arrayA[i]=(s[i]==\'R\')?nums[i]+d:nums[i]-d.\nThen sort the array A
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCompute firstly the array`A[i]=(s[i]==\'R\')?nums[i]+d:nums[i]-d`.\nThen sort the array `A` such that `A[i]<A[j]` for `i<j`.\nCompute the sum of distance \n$$\n\\sum_{i<j}A[j]-A[i]\\\\\n=\\sum_{i=0}^{n-1}(2i-n+1)A[i]\n$$\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLike solving [1503. Last Moment Before All Ants Fall Out of a Plank](https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/solutions/4246032/c-3-lines-python-1-line-physics/) & compute the array `A`.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n\\log n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code 88ms Beats 95.92%\n```\n#pragma GCC optimize("O3")\nclass Solution {\npublic:\n    const int mod=1e9+7;\n    int sumDistance(vector<int>& nums, string s, int d) \n    {\n        int n=nums.size();\n        vector<long long> A(n, 0L);\n        #pragma unroll\n        for(int i=0; i<n; i++){\n            A[i]=(s[i]==\'R\')?(long long)nums[i]+d:(long long)nums[i]-d;\n        }\n        sort(A.begin(), A.end());\n        long long distance=0;\n        #pragma unroll\n        for(int i=0; i<n; i++)\n           distance=(distance+(-n+1+(2L*i))*A[i])%mod;\n        return distance<0?distance+mod:distance;\n    }\n};\n```
['Array', 'Sorting', 'C++']